A tumblelog CMS built on AJAX, PHP and MySQL.

imgsize.php 2.3KB

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