Back to the snippets index

Resize an image ( Anthony 14/6/2008 )

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.

GD code:

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

Link for more information

Change a png to a jpg
Create some text as an image
Crop an image
Display image without saving
Grayscale image
Resize an image
Rotate an image
Watermark an image