A tumblelog CMS built on AJAX, PHP and MySQL.

imgsize.php 2.4KB

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