Posted By

deadmoon on 10/19/06


Tagged


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

luman
blakeb
dmarten
hudge
willcodeforfood
oli964
vali29


make tumbnails on the fly


 / Published in: PHP
 

  1. <?php
  2. function shutdown() {
  3. global $orig_image, $cropped_image, $resized_image;
  4. if($orig_image) { imagedestroy($orig_image); }
  5. if($cropped_image) { imagedestroy($cropped_image); }
  6. if($resized_image) { imagedestroy($resized_image); }
  7. }
  8. list($old_width, $old_height) = getimagesize("./". $_GET['image']);
  9. if(($_GET['width'] / $_GET['height']) > ($old_width / $old_height)) {
  10. $new_width = $_GET['width'];
  11. $new_height = $old_height * ($_GET['width'] / $old_width);
  12. } else {
  13. $new_width = $old_width * ($_GET['height'] / $old_height);
  14. $new_height = $_GET['height'];
  15. }
  16. $resized_image = imagecreatetruecolor($new_width, $new_height);
  17. $orig_image = imagecreatefromgif("./". $_GET['image']);
  18. imagecopyresampled($resized_image, $orig_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
  19. $cropped_image = imagecreatetruecolor($_GET['width'], $_GET['height']);
  20. imagecopy($cropped_image, $resized_image, 0, 0, 0, 0, imagesx($resized_image), imagesy($resized_image));
  21. $black = imagecolorallocate($cropped_image, 0, 0, 0);
  22. imagerectangle($cropped_image, 0, 0, $_GET['width'] - 1, $_GET['height'] - 1, $black);
  23. header("Content-type: image/png");
  24. imagepng($cropped_image);
  25. ?>
  26.  
  27. To create a thumbnail with it:
  28.  
  29. <img src="path/to/thumbnailer.php?image=image.gif&width=50&height=50" alt="" />

Report this snippet  

You need to login to post a comment.