A tumblelog CMS built on AJAX, PHP and MySQL.

imgsize.php 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. $percent = $_GET['percent'];
  24. $constrain = $_GET['constrain'];
  25. $w = $_GET['w'];
  26. $h = $_GET['h'];
  27. // get image size of img
  28. $x = @getimagesize($img);
  29. // image width
  30. $sw = $x[0];
  31. // image height
  32. $sh = $x[1];
  33. if ($percent > 0) {
  34. // calculate resized height and width if percent is defined
  35. $percent = $percent * 0.01;
  36. $w = $sw * $percent;
  37. $h = $sh * $percent;
  38. } else {
  39. if (isset ($w) AND !isset ($h)) {
  40. // autocompute height if only width is set
  41. $h = (100 / ($sw / $w)) * .01;
  42. $h = @round ($sh * $h);
  43. } elseif (isset ($h) AND !isset ($w)) {
  44. // autocompute width if only height is set
  45. $w = (100 / ($sh / $h)) * .01;
  46. $w = @round ($sw * $w);
  47. } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) {
  48. // get the smaller resulting image dimension if both height
  49. // and width are set and $constrain is also set
  50. $hx = (100 / ($sw / $w)) * .01;
  51. $hx = @round ($sh * $hx);
  52. $wx = (100 / ($sh / $h)) * .01;
  53. $wx = @round ($sw * $wx);
  54. if ($hx < $h) {
  55. $h = (100 / ($sw / $w)) * .01;
  56. $h = @round ($sh * $h);
  57. } else {
  58. $w = (100 / ($sh / $h)) * .01;
  59. $w = @round ($sw * $w);
  60. }
  61. }
  62. }
  63. $im = @ImageCreateFromJPEG ($img) or // Read JPEG Image
  64. $im = @ImageCreateFromPNG ($img) or // or PNG Image
  65. $im = @ImageCreateFromGIF ($img) or // or GIF Image
  66. $im = false; // If image is not JPEG, PNG, or GIF
  67. if (!$im) {
  68. // We get errors from PHP's ImageCreate functions...
  69. // So let's echo back the contents of the actual image.
  70. readfile ($img);
  71. } else {
  72. // Create the resized image destination
  73. $thumb = @ImageCreateTrueColor ($w, $h);
  74. // Copy from image source, resize it, and paste to image destination
  75. @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh);
  76. // Output resized image
  77. @ImageJPEG ($thumb);
  78. }
  79. ?>