Snippets - GD

Snippets index page

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.

  1. <?php
  2. // Set the path to the image to resize
  3. $input_image "House.jpg";
  4. // Get the size of the original image into an array
  5. $size getimagesize$input_image );
  6. // Set the new width of the image
  7. $thumb_width "200";
  8. // Calculate the height of the new image to keep the aspect ratio
  9. $thumb_height = ( int )(( $thumb_width/$size[0] )*$size[1] );
  10. // Create a new true color image in the memory
  11. $thumbnail ImageCreateTrueColor$thumb_width$thumb_height );
  12. // Create a new image from file 
  13. $src_img ImageCreateFromJPEG$input_image );
  14. // Create the resized image
  15. ImageCopyResampled$thumbnail$src_img0000$thumb_width$thumb_height$size[0], $size[1] );
  16. // Save the image as resized.jpg
  17. ImageJPEG$thumbnail"resized.jpg" );
  18. // Clear the memory of the tempory image 
  19. ImageDestroy$thumbnail );
  20. ?>
www.rubblewebs.co.uk/imagemagick/snippets/Resize_snippets_f_52.html

For more examples and information check out the main Imagemagick section.

Change a png to a jpg View

Create some text as an image View

Crop an image View

Display image without saving View

Grayscale image View

Resize an image View

Rotate an image View

Watermark an image View