Thumbs on fly


 / Published in: PHP
 

Only JPG

  1. <?php
  2.  
  3. /*
  4. File: thumbs.php
  5. Example: <img src="thumbs.php?filename=photo.jpg&amp;width=100&amp;height=100">
  6. */
  7.  
  8. $filename= $_GET['filename'];
  9. $width = $_GET['width'];
  10. $height = $_GET['height'];
  11. $path="http://localhost/images/"; //finish in "/"
  12.  
  13.  
  14. // Content type
  15. header('Content-type: image/jpeg');
  16.  
  17. // Get new dimensions
  18. list($width_orig, $height_orig) = getimagesize($path.$filename);
  19.  
  20. if ($width && ($width_orig < $height_orig)) {
  21. $width = ($height / $height_orig) * $width_orig;
  22. } else {
  23. $height = ($width / $width_orig) * $height_orig;
  24. }
  25.  
  26. // Resample
  27. $image_p = imagecreatetruecolor($width, $height);
  28. $image = imagecreatefromjpeg($path.$filename);
  29. imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
  30.  
  31. // Output
  32. imagejpeg($image_p, null, 100);
  33.  
  34. // Imagedestroy
  35. imagedestroy ($image_p);
  36. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Shocker on January 30, 2008

looks nice, but not very useful without caching.. Error handling is missing too and it won't hurt to support PNG and GIF too...

Posted By: kurokikaze on March 3, 2009

ImageMagick version should be nicer, i suppose. And this can be "reduced" version, if IM or MagicWand is not installed.

Posted By: oggame on April 6, 2009

you could also use http://freelogic.pl/thumbnailer/

Posted By: sb_01 on September 19, 2010

Shocker: right :) ... not really useful without caching / error handling

Posted By: Steffen82 on November 3, 2012

A nice OOP-PHP5-Class for crating Thumbnails: https://github.com/SteffenHagdorn/Pixlie

You need to login to post a comment.