Posted By

Tamedo on 05/16/08


Tagged


Versions (?)

Who likes this?

10 people have marked this snippet as a favorite

luman
brent-man
digiloper
Jenhale
jamesming
vali29
llbbl
silentbob84
laurentzziu
wirenaught


GD RESIZE IMAGES


 / Published in: PHP
 

To display image use following

image_resize.php?img=image.jpg&w=150&h=150&constrain=1

  1. <?php
  2. header ("Content-type: image/jpeg");
  3. /*
  4. JPEG / PNG Image Resizer
  5. Parameters (passed via URL):
  6.  
  7. img = path / url of jpeg or png image file
  8.  
  9. percent = if this is defined, image is resized by it's
  10.   value in percent (i.e. 50 to divide by 50 percent)
  11.  
  12. w = image width
  13.  
  14. h = image height
  15.  
  16. constrain = if this is parameter is passed and w and h are set
  17.   to a size value then the size of the resulting image
  18.   is constrained by whichever dimension is smaller
  19.  
  20. Requires the PHP GD Extension
  21.  
  22. Outputs the resulting image in JPEG Format
  23.  
  24. By: Michael John G. Lopez - www.sydel.net
  25. Filename : imgsize.php
  26. */
  27.  
  28. $img = $_GET['img'];
  29. $percent = $_GET['percent'];
  30. $constrain = $_GET['constrain'];
  31. $w = $_GET['w'];
  32. $h = $_GET['h'];
  33.  
  34. // get image size of img
  35. $x = @getimagesize($img);
  36. // image width
  37. $sw = $x[0];
  38. // image height
  39. $sh = $x[1];
  40.  
  41. if ($percent > 0) {
  42. // calculate resized height and width if percent is defined
  43. $percent = $percent * 0.01;
  44. $w = $sw * $percent;
  45. $h = $sh * $percent;
  46. } else {
  47. if (isset ($w) AND !isset ($h)) {
  48. // autocompute height if only width is set
  49. $h = (100 / ($sw / $w)) * .01;
  50. $h = @round ($sh * $h);
  51. } elseif (isset ($h) AND !isset ($w)) {
  52. // autocompute width if only height is set
  53. $w = (100 / ($sh / $h)) * .01;
  54. $w = @round ($sw * $w);
  55. } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) {
  56. // get the smaller resulting image dimension if both height
  57. // and width are set and $constrain is also set
  58. $hx = (100 / ($sw / $w)) * .01;
  59. $hx = @round ($sh * $hx);
  60.  
  61. $wx = (100 / ($sh / $h)) * .01;
  62. $wx = @round ($sw * $wx);
  63.  
  64. if ($hx < $h) {
  65. $h = (100 / ($sw / $w)) * .01;
  66. $h = @round ($sh * $h);
  67. } else {
  68. $w = (100 / ($sh / $h)) * .01;
  69. $w = @round ($sw * $w);
  70. }
  71. }
  72. }
  73.  
  74. $im = @ImageCreateFromJPEG ($img) or // Read JPEG Image
  75. $im = @ImageCreateFromPNG ($img) or // or PNG Image
  76. $im = @ImageCreateFromGIF ($img) or // or GIF Image
  77. $im = false; // If image is not JPEG, PNG, or GIF
  78.  
  79. if (!$im) {
  80. // We get errors from PHP's ImageCreate functions...
  81. // So let's echo back the contents of the actual image.
  82. readfile ($img);
  83. } else {
  84. // Create the resized image destination
  85. $thumb = @ImageCreateTrueColor ($w, $h);
  86. // Copy from image source, resize it, and paste to image destination
  87. @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
  88. // Output resized image
  89. @ImageJPEG ($thumb);
  90. }
  91. ?>

Report this snippet  

You need to login to post a comment.