GD Thumbnailing Probs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

GD Thumbnailing Probs

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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.
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post 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
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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
pootergeist
Forum Contributor
Posts: 273
Joined: Thu Feb 27, 2003 7:22 am
Location: UK

Post 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 :)
User avatar
Pointybeard
Forum Commoner
Posts: 71
Joined: Wed Sep 03, 2003 7:23 pm
Location: Brisbane, AUS
Contact:

Post by Pointybeard »

getting GD to manipulate a 500KB string
eep!

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