A tumblelog CMS built on AJAX, PHP and MySQL.

imgsize.php 2.8KB

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