Resize an image without distorting proportions (JPEG, GIF or PNG)


/ Published in: PHP
Save to your folder(s)

A function that resizes an image to desired width and height, but does not distort the image proportions. Works for JPEG, GIF and PNG.


Copy this code and paste it in your HTML
  1. <?php
  2. /**
  3.  * This function resizes an image to $fitInWidth and $fitInHeight, but doesn't distort the image proportions
  4.  *
  5.  * $path: The full path to the image. Ex.: "/images/my_image.jpg"
  6.  *
  7.  * $newName: If you don't want to override the original image, you can specify a new name.
  8.  *
  9.  * $fitInWidth:The height in pixels that you want to fit the image in
  10.  *
  11.  * $fitInHeight: The height in pixels that you want to fit the image in
  12.  *
  13.  * $jpegQuality: If the image is a jpeg, the function will save
  14.  * the resized jpeg with the specified quality from 1 to 100
  15.  */
  16. function resize_image($path, $fitInWidth, $fitInHeight, $newName = '', $jpegQuality = 100)
  17. {
  18. list($width, $height, $type) = getimagesize($path);//Getting image information
  19.  
  20. $scaleW = $fitInWidth/$width;
  21. $scaleH = $fitInHeight/$height;
  22. if($scaleH > $scaleW)
  23. {
  24. $new_width = $fitInWidth;
  25. $new_height = floor($height * $scaleW);
  26. }
  27. else
  28. {
  29. $new_height = $fitInHeight;
  30. $new_width = floor($width * $scaleH);
  31. }
  32. $new_path = $newName == '' ? $path : dirname($path) . '/' . $newName;
  33.  
  34. if($type == IMAGETYPE_JPEG)//If image is jpeg
  35. {
  36. $image_now = imagecreatefromjpeg($path);//Get image from path
  37. $image_new = imagecreatetruecolor($new_width, $new_height);//Create new image from scratch
  38. //Copy image from path into new image ($image_new) with new sizes
  39. imagecopyresampled($image_new, $image_now, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  40. imagejpeg($image_new, $new_path, $jpegQuality);
  41. }
  42. else if($type == IMAGETYPE_GIF)//If image is gif
  43. {
  44. $image_now = imagecreatefromgif($path);
  45. $image_new = imagecreatetruecolor($new_width, $new_height);
  46. imagecopyresampled($image_new, $image_now, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  47. imagegif($image_new, $new_path);
  48. }
  49. else if($type == IMAGETYPE_PNG)//If image is png
  50. {
  51. $image_now = imagecreatefrompng($path);
  52. $image_new = imagecreatetruecolor($new_width, $new_height);
  53. //Setting black color as transparent because image is png
  54. imagecolortransparent($image_new, imagecolorallocate($image_new, 0, 0, 0));
  55. imagecopyresampled($image_new, $image_now, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
  56. imagepng($image_new, $new_path);
  57. }
  58. else
  59. {
  60. //Image type is not jpeg, gif or png.
  61. }
  62. imagedestroy($image_now);
  63. imagedestroy($image_new);
  64. }
  65.  
  66. //How to use
  67. $img_path = 'images/my_image.jpg';//The path to the image you want to resize
  68. //Here i'm giving a new name to the resized image so that it doesn't override the original image. If you want to override the original image change: $new_name = '';
  69. $new_name = 'resized_image.jpg';
  70. resize_image($img_path, 200, 200, $new_name);
  71. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.