0

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:

// This creates a thumbnail 30% size of the original
<img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&pct=30">

// This creates a thumbnail with a maximum width of 150 pixels
<img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&maxw=150">

// This creates a thumbnail with a maximum height of 100 pixels.
<img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&maxh=100">

// The default creates a thumbnail 25% of it's original size preserving its original aspect ratio
<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 
// The file you are resizing 
$file = $_GET['filename'];

$sizeset = false;
list($width, $height) = getimagesize($file); 

//This will set our output size depending on input

if($_GET['pct']) {               		// First type - percentage of original size
	$size = $_GET['pct']/100; 
	$modwidth = $width * $size; 
	$modheight = $height * $size; 
	$sizeset = true;
} 

if($_GET['maxw']) {               		// Second type - Determine Max Width of thumbnails
	$wpct = $_GET['maxw'] / $width;
	$modwidth = $_GET['maxw'];
	$modheight = round($height*$wpct);
	$sizeset = true;
} 

if($_GET['maxh']) {               		// Third type - Determine Max Height of thumbnails
	$hpct = $_GET['maxh'] / $height;
	$modheight = $_GET['maxh'];
	$modwidth = round($width*$hpct);
	$sizeset = true;
} 

if(!$sizeset) {					// Fourth type - If nothing else specified, default to this percentage
	$size = .25;				// This is set for 25%
	$modwidth = $width * $size; 
	$modheight = $height * $size; 
} 

// This sets it to a .jpg, but you can change this to png or gif 
header('Content-type: image/jpeg'); 

// Resizing the Image 
$tn = imagecreatetruecolor($modwidth, $modheight); 
$image = imagecreatefromjpeg($file); 
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); 

// Outputting a .jpg, you can make this gif or png if you want 
//notice we set the quality (third value) to 100 
imagejpeg($tn, null, 100); 
?>
5
Contributors
5
Replies
8
Views
7 Years
Discussion Span
Last Post by Web Dev Rob
0

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.

0

@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! :)

1

thank you for sharing...it could be a great help for me especially for the learning of the students like me.....

0

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:

// This creates a thumbnail 30% size of the original
<img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&pct=30">

// This creates a thumbnail with a maximum width of 150 pixels
<img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&maxw=150">

// This creates a thumbnail with a maximum height of 100 pixels.
<img src="http://www.mysite.com/theabovecode.php?filename=images/yourimage.jpg&maxh=100">

// The default creates a thumbnail 25% of it's original size preserving its original aspect ratio
<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.

0

Things like this are good for learning, but I really wouldn't recommend it on a live site as thumbs are being recreated each time the script is called - which is bad practice and unecessary.

You should really be caching the generated images. There's quite a good one that caches your thumbnails at http://shiftingpixel.com/2008/03/03/smart-image-resizer/

I'd also advise using htaccess to rewrite your image locations as they are very messy with such scripts. E.g. turn image.php/coffee-bean.jpg?width=150&amp;height=150&amp;cropratio=1:1 into images/cached/xl/coffee-bean.jpg
(Note - you need to use the full image path on your rewrite rule - even if you have set a rewritebase!

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.