Generating "thumbnail" image problems

Hi guys!

I’m a total loss, here. I’ve been bashing my head all morning, trying to get this script to generate a smaller version of the supplied image, and here’s the code:

function get_image ($image) {

		// load image and get image size
		$image_source = imagecreatefromjpeg( $image );
		$width = imagesx( $image_source );
		$height = imagesy( $image_source );

		// calculate thumbnail size
		$width_new = '170';
		$height_new = floor( $height * ( '170' / $width ) );

		// create a new temporary image
		$image_temporary = imagecreatetruecolor( $width_new, $height_new );

		// copy and resize old image into new image 
		imagecopyresized( $image_temporary, $image_source, 0, 0, 0, 0, $width_new, $height_new, $width, $height );

		$image = $image_temporary;

		imagejpeg($image, '', 85);

	} // end function get_image


I’ve tried no end of variations (far too many to list here), but I just cannot get it to stop burping out garbled code.

I’ve tried using the header() function, but that just gives me warnings.

I’ve tried moving the code into a separate file and passing the file name as a name-value pair in the URL of an image source, but that does nothing.

I know for a fact GDlib is working on my computer because I have similar tools working elsewhere, but I simply cannot get any code examples I find to work.

This is driving mad!

So any ideas would be thoroughly appreciated.

Some old code of mine that should work:

// Set the path to the image to resize
$input_image = "House.jpg";
// Get the size of the original image into an array
$size = getimagesize( $input_image );
// Set the new width of the image
$thumb_width = "200";
// Calculate the height of the new image to keep the aspect ratio
$thumb_height = ( int )(( $thumb_width/$size[0] )*$size[1] );
// Create a new true color image in the memory
$thumbnail = ImageCreateTrueColor( $thumb_width, $thumb_height );
// Create a new image from file 
$src_img = ImageCreateFromJPEG( $input_image );
// Create the resized image
ImageCopyResampled( $thumbnail, $src_img, 0, 0, 0, 0, $thumb_width, $thumb_height, $size[0], $size[1] );
// Save the image as resized.jpg
ImageJPEG( $thumbnail, "resized.jpg" );
// Clear the memory of the tempory image 
ImageDestroy( $thumbnail );

Note this will save the image and you just want to display it?

Hi Rubble, and thanks for the reply!

Yeah, I just want to display the image in an image tag.

I just tried the above and got an error:

Warning: imagejpeg() [function.imagejpeg]: Unable to open ‘resized.jpg’ for writing: Permission denied in /path/file.php on line 29

When I uploaded the code onto the web, I got nothing.

To just display the image you need to have the code on a different page e.g. thumbnail.php and call it using:

<img src="thumbnail.php">

You need to pass your image name so from memory this should work as long as you read the variable posted into the function:

<img src="thumbnail.php?image=wayne-smallman.jpg">

The resized jpg is the image being saved try removing the name and leave the “”

I had to hack and slash at things for a while, but I got there eventually!

Thanks for the assist…