We Recommend

Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.


Posted By

luman on 06/29/06


Tagged

image thumbs


Versions (?)


Who likes this?

31 people have marked this snippet as a favorite

tylerhall
jleblanc
nex
dangerouslyawesome
luxuryluke
24baud
kawikak
yuconner
dojob
Ernstladen
visualAesthetic
blakeb
demods
noname
koncept
bitcrumb
fael
pagetoscreen
vali29
hudge
adix
raptrex
cristianciofu
SpinZ
localhorst
Steffen82
gafsveno
digiloper
unabatedshagie
coylOne
oriolfb


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...

You need to login to post a comment.