A tumblelog CMS built on AJAX, PHP and MySQL.

imgsize.php 2.7KB

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