Ad:
 
  • PHP Code Snippet
  • Views: 1933
  • PHP RSS
0

Quick 'n Dirty Thumbnails

by Stan Kilgore on Feb 20th, 2010
This code is for a stand-alone file, named "thumbnail.php" or something similar. Once uploaded to your server, it can be called from inside an IMG tag.

There are four options for this script without modifying anything.
  1. First, you can resize the image to a percentage of the original, as to maintain aspect ratio.
  2. Second, you can set a maximum width for the thumbnails.
  3. Third, you can set a maximum height for the thumbnails.
  4. Lastly, if you do not specify a way to create the thumbnail, then the script defaults to creating a thumbnail 25% of the size of the original preserving its original aspect ratio.

So, when you need the thumbnail, you can call it like these examples, depending on what you need:

PHP Syntax (Toggle Plain Text)
  1. // This creates a thumbnail 30% size of the original
  2. <img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&pct=30">
  3.  
  4. // This creates a thumbnail with a maximum width of 150 pixels
  5. <img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&maxw=150">
  6.  
  7. // This creates a thumbnail with a maximum height of 100 pixels.
  8. <img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&maxh=100">
  9.  
  10. // The default creates a thumbnail 25% of it's original size preserving its original aspect ratio
  11. <img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg">

I hope this helps someone out there looking for a Quick 'n Dirty thumbnail script.
PHP Code Snippet (Toggle Plain Text)
  1. <?php
  2. // The file you are resizing
  3. $file = $_GET['filename'];
  4.  
  5. $sizeset = false;
  6. list($width, $height) = getimagesize($file);
  7.  
  8. //This will set our output size depending on input
  9.  
  10. if($_GET['pct']) { // First type - percentage of original size
  11. $size = $_GET['pct']/100;
  12. $modwidth = $width * $size;
  13. $modheight = $height * $size;
  14. $sizeset = true;
  15. }
  16.  
  17. if($_GET['maxw']) { // Second type - Determine Max Width of thumbnails
  18. $wpct = $_GET['maxw'] / $width;
  19. $modwidth = $_GET['maxw'];
  20. $modheight = round($height*$wpct);
  21. $sizeset = true;
  22. }
  23.  
  24. if($_GET['maxh']) { // Third type - Determine Max Height of thumbnails
  25. $hpct = $_GET['maxh'] / $height;
  26. $modheight = $_GET['maxh'];
  27. $modwidth = round($width*$hpct);
  28. $sizeset = true;
  29. }
  30.  
  31. if(!$sizeset) { // Fourth type - If nothing else specified, default to this percentage
  32. $size = .25; // This is set for 25%
  33. $modwidth = $width * $size;
  34. $modheight = $height * $size;
  35. }
  36.  
  37. // This sets it to a .jpg, but you can change this to png or gif
  38. header('Content-type: image/jpeg');
  39.  
  40. // Resizing the Image
  41. $tn = imagecreatetruecolor($modwidth, $modheight);
  42. $image = imagecreatefromjpeg($file);
  43. imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
  44.  
  45. // Outputting a .jpg, you can make this gif or png if you want
  46. //notice we set the quality (third value) to 100
  47. imagejpeg($tn, null, 100);
  48. ?>
Comments on this Code Snippet
Feb 20th, 2010
0

Re: Quick 'n Dirty Thumbnails

This could be useful. Thanks for sharing. My only concern would be with the stress put on the server...I wonder if there'd be a way to cache the thumbnails so they wouldn't have to be re-converted every single time the page is loaded.
Light Poster
Jerail is offline Offline
39 posts
since Feb 2010
Feb 20th, 2010
0

Re: Quick 'n Dirty Thumbnails

@Jerail: I can see that. I use this script a lot, but I usually do not have a ton of images to be thumbnailed. I imagine that, if one was inclined, it could be modified so that a user could upload the image and, once uploaded, the thumbnail created automatically. Great point to bring up!
Junior Poster
Wraithmanilian is offline Offline
141 posts
since Dec 2009
Mar 5th, 2010
1

Re: Quick 'n Dirty Thumbnails

thank you for sharing...it could be a great help for me especially for the learning of the students like me.....
Newbie Poster
chumsie is offline Offline
3 posts
since Mar 2010
Jul 6th, 2010
0

Set a Max Width/Height and Resize Img to Fit AND Retain Aspect Ratio

Wraithmanilian:

I want to set a max width and height then resize my images (of different aspect ratios) to fit inside that "box" (resizing up/down as necessary) while retaining its original aspect ratio. Can I (and if so how) can I do this with your code?

This code is for a stand-alone file, named "thumbnail.php" or something similar. Once uploaded to your server, it can be called from inside an IMG tag.

There are four options for this script without modifying anything.
  1. First, you can resize the image to a percentage of the original, as to maintain aspect ratio.
  2. Second, you can set a maximum width for the thumbnails.
  3. Third, you can set a maximum height for the thumbnails.
  4. Lastly, if you do not specify a way to create the thumbnail, then the script defaults to creating a thumbnail 25% of the size of the original preserving its original aspect ratio.

So, when you need the thumbnail, you can call it like these examples, depending on what you need:

PHP Syntax (Toggle Plain Text)
  1. // This creates a thumbnail 30% size of the original
  2. <img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&pct=30">
  3.  
  4. // This creates a thumbnail with a maximum width of 150 pixels
  5. <img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&maxw=150">
  6.  
  7. // This creates a thumbnail with a maximum height of 100 pixels.
  8. <img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&maxh=100">
  9.  
  10. // The default creates a thumbnail 25% of it's original size preserving its original aspect ratio
  11. <img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg">

I hope this helps someone out there looking for a Quick 'n Dirty thumbnail script.
Light Poster
ms_sws is offline Offline
31 posts
since Mar 2010
Message:
Previous Thread in PHP Forum Timeline: include function not working URGENT!
Next Thread in PHP Forum Timeline: [split] project help





About Us | Contact Us | Advertise | Acceptable Use Policy
Build Custom RSS Feed


Follow us on Twitter


© 2010 DaniWeb® LLC