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 );
imagedestroy($image_source);
$image = $image_temporary;
imagejpeg($image, '', 85);
} // end function get_image
get_image('wayne-smallman.jpg');
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.
<?php
// 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?