Page 1 of 1

GD Thumbnailing Probs

Posted: Wed Sep 03, 2003 7:23 pm
by Pointybeard
Hi,
I am writing an eCommerce site for a client and when showing products i wish to show a thumbnail which is generated on the fly from a picture. It works, but not totally correctly. This is my code (I borrowed it from somewhere and tweaked it slightly)

Code: Select all

<?php

	$error = False;
	
	$system = explode(".", $image);
	
	// check if the 'image' parameter has been given
	if (!isset($image)) // if not generate an error
		$error = True;
		
	// check if the 'img_size' parameter has been given
	if (!isset($img_size) && ($resize != 'no')) // if not generate an error
		$error = True;
	
	if($resize != 'no')	&#123;
		// ensure that $img_size is an integer 
		$img_size = (integer) $img_size;
		
		// check that the size asked for is sensible
		if ($img_size < 20)// if not generate an error
			$error = True;	
	&#125;
	
	if(!$bigimage=@imagecreatefromjpeg($image))
	$error = true;		

	
	if($error)&#123; // generate an error image
		$img = ImageCreate(123, 100);				
		$bg = ImageColorAllocate($img, 255, 255, 255);
		$white = ImageColorAllocate($img, 255, 0, 0);
		ImageString($img, 5, 30, 20, "Picture", $white);
		ImageString($img, 5, 45, 40, "Not", $white);
		ImageString($img, 5, 25, 60, "Available", $white);
		ImagePNG($img);
		ImageDestroy($img);
		exit();
	&#125;
	
	$x=imageSX($bigimage);
	$y=imageSY($bigimage);		
	
	if($resize == 'no' || (($x > $y) && ($x < $img_size)) || (($y > $x) && ($y < $img_size)))&#123;

		imagejpeg($bigimage);
		exit();
	&#125;
	
	// find the larger dimension
	if ($x>$y) &#123;	// if it is the width then
		$dx = 0;					// the left side of the new image
		$w = $img_size;				// the width of the new image
		$h = ($y / $x) * $img_size;	// the height of the new image
		$dy = 0;					// the top of the new image
	&#125; else &#123;	// if the height is larger then
		$dy = 0;					// the top of the new image
		$h = $img_size;				// the height of the new image
		$w = ($x / $y) * $img_size;	// the width of the new image
		$dx = 0;	// the left edgeof the new image
	&#125;
	
	$tnimage = ImageCreate($w, $h);	

	ImageCreateTrueColor($w, $h);
	ImageCopyResampled($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h, $x, $y); 
		
	// copy the resized version into the thumbnal image
	//ImageCopyResized($tnimage, $bigimage, $dx, $dy, 0, 0, $w, $h, $x, $y);	

	imagejpeg($tnimage);	

	// release the memory used by the thumbnail image
	ImageDestroy($tnimage);
	
	// release the memory used by the original big image
	ImageDestroy($bigimage);
	

?>

The code is called like this:

Code: Select all

<img border=0  src="_scripts/image.php?image=../_images/products/$rec&#1111;ID].jpg&img_size=123" />

And this is the result:

Image

This is what it should look like:

Image

Any ideas where i might be going wrong?
Thanks for the help

-Pointybeard

Posted: Wed Sep 03, 2003 7:57 pm
by volka
$tnimage = ImageCreate($w, $h);

ImageCreateTrueColor($w, $h);
should be

Code: Select all

$tnimage = ImageCreateTrueColor($w, $h);
otherwise you're creating a truecolor-image but using the ImageCreate-resource.
For non-IE-browsers you should set header('Content-type: image/png'); resp. header('Content-type: image/jpeg'); before sending the image.

Posted: Wed Sep 03, 2003 10:45 pm
by McGruff
You mentioned you are doing this on the fly: I might be tempted to run a script to create thumbnails at the same time as the full size images are added to the site.

Posted: Thu Sep 04, 2003 2:31 am
by Pointybeard
Thanks volka! that worked perfectly!!!
... tempted to run a script to create thumbnails at the same time as the full size images are added ...
I am still undecided on which way i want to do it. My concern with the on the fly method is that the server has to resize the image everytime the page is loaded which might dramatically slow things down......see how it goes i guess. The website is 98% complete. The bad image thumbnailing was on the list of "Things to fix up" :p Anyway, thanks for the help

-Pointybeard

Posted: Thu Sep 04, 2003 6:58 am
by pootergeist
using GD for on the fly stuff is not reccommended

the .gd format (string format that GD uses for manipulating) runs at about ten times the size of a low compressed jpeg - so if you imagecreatefromjpeg with a 50KB resource you are getting GD to manipulate a 500KB string

- for every image
- for every page load

Posted: Thu Sep 04, 2003 7:10 am
by pootergeist
subnote: a 404 handler in the thumbnail dir could also automate it nicely -

.htaccess in _images/products/thumbs/

ErrorDocument 404 /_images/products/thumbs/create_thumb.php


<img border=0 src="_images/products/thumbs/$rec[ID].jpg" />

create_thumb.php
// this would run only if the requested jpeg not found

$img_url = explode('/',$_SERVER['REQUEST_URI]);
$img_ref = $img_url[(count($img_url)-1)];
$im = create a thumb from the image /home/www/_images/products/$img_ref
ImageJpeg($im, '/home/www/_images/products/thumbs/'.$img_ref, 90);
header("Content-type............);
ImageJpeg($im);

so basically, if a thumbnail isn't found the 404 activates the create script. This then creates a thumb from the same named product picture and saves it in /thumbs/ it also outputs to browser -
only runs once the first time a thumb is not found - problem solved :)

Posted: Thu Sep 04, 2003 6:16 pm
by Pointybeard
getting GD to manipulate a 500KB string
eep!

Thanks for the help there pootergeist. I think i will do what your suggesting. ;)