Posted By

Cory on 01/14/08


Tagged

php resize image thumb easy


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

Abe
kiliksun
falp
jeff
jamesming
titosemi


Resize Image


 / Published in: PHP
 

  1. <?php
  2. /*
  3.   ImageThumb - Creates a thumbnail image from another based on specified sizes
  4.  
  5.   SourceImage - The location of the image in which you want to resize
  6.   DestImage - The location to save the thumb, use null if you want to just display to browser
  7.   Width - The resized width of the image
  8.   Height - The resized height of the image
  9.   Type - The image type in which you want to save
  10.  
  11.   Note: The thumb is resized while keeping the aspect ratio, so width and height are not the
  12.   absolute width and height.
  13.   */
  14.  
  15. function ImageThumb($sourceImage, $destImage, $width, $height, $type = "png") {
  16. $type = strtolower($type);
  17. $imageSize = getimagesize($sourceImage);
  18.  
  19. if($imageSize[0] > $imageSize[1]) {
  20. $newWidth = $width;
  21. $newHeight = $imageSize[1] * ($newWidth / $imageSize[0]);
  22. }
  23. else {
  24. $newHeight = $height;
  25. $newWidth = $imageSize[0] * ($newHeight / $imageSize[1]);
  26. }
  27.  
  28. switch(image_type_to_mime_type($imageSize[2])) {
  29. case "image/jpeg":
  30. $image = imagecreatefromjpeg($sourceImage);
  31. break;
  32. case "image/gif":
  33. $image = imagecreatefromgif($sourceImage);
  34. break;
  35. case "image/png":
  36. $image = imagecreatefrompng($sourceImage);
  37. break;
  38. default:
  39. $t = image_type_to_mime_type($imageSize[2]);
  40. echo "The file type {$t} is not supported, please use either jpeg, gif, or png";
  41. break;
  42. }
  43.  
  44. $thumb = imagecreatetruecolor($newWidth, $newHeight);
  45. imagecopyresampled($thumb, $image, 0, 0, 0, 0, $newWidth, $newHeight, $imageSize[0], $imageSize[1]);
  46.  
  47. switch($type) {
  48. case "jpg":
  49. case "jpeg":
  50. header("Content-type: image/jpeg");
  51. imagejpeg($thumb, $destImage);
  52. break;
  53. case "gif":
  54. header("Content-type: image/gif");
  55. imagegif($thumb, $destImage);
  56. break;
  57. case "png":
  58. header("Content-type: image/png");
  59. imagepng($thumb, $destImage);
  60. break;
  61. default:
  62. echo "The image type {$type} is not supported, please choose another.";
  63. break;
  64. }
  65.  
  66. imagedestroy($image);
  67. imagedestroy($thumb);
  68. }
  69. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Abe on January 14, 2008

Nice, for all the users watch this script how can make a thumb of every kind of image jpg/gif/png.

Posted By: kazal on June 27, 2009

Thanks a lot. This code works in a great way. Please provide these type of efficient codes later times.

Posted By: verostudios on April 2, 2010

Hopefully this makes its way to the author of this snippet, great chunk of code but you have an error on line 16. Your missing a semi-colon which will cause the script to error out.

Posted By: Cory on April 13, 2010

@verostudios Thanks, I fixed the error that you found.

You need to login to post a comment.