Resize an image


<?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 );
?>

This will resize an image keeping its aspect ratio and save it.
You will notice it is a lot more complicated than using Imagemagick - check out the link.


More information about this snippet