/    Sign up×
Community /Pin to ProfileBookmark

"On demand" thumbnails

Inspired by [url=http://www.webdeveloper.com/forum/showthread.php?t=109807]this thread[/url], I created a script which creates thumbnails “on demand” as opposed to storing them to disk. Feel free to plagiarize and modify to your heart’s content.

[code=php]
<?php
/*
FILE: thumb.php
PURPOSE: create thumbnail image “on demand” for image file in same directory
CREATED: June 2006 by Charles Reace (www.charles-reace.com)
USAGE: <img src=”images/thumb.php?file=image.png” alt=””>
*/

// change these values to desired max dimensions in pixels:
$maxWidth = 100;
$maxHeight = 75;

if(!empty($_GET[‘file’]))
{
$file = basename($_GET[‘file’]); // will only work on files in this directory
if(is_readable($file))
{
$sizeInfo = getimagesize($file);
if($sizeInfo !== FALSE)
{
$imgTypes = array( // IMAGETYPE_* for getimagesize() index 2
IMAGETYPE_GIF => IMG_GIF, // IMG_* for imagetype() check
IMAGETYPE_JPEG => IMG_JPG,
IMAGETYPE_PNG => IMG_PNG,
IMAGETYPE_WBMP => IMG_WBMP
);
if(isset($imgTypes[$sizeInfo[2]]) and
(imagetypes() & $imgTypes[$sizeInfo[2]]))
{
$ratio = (($sizeInfo[0]/$sizeInfo[1]) > ($maxWidth/$maxHeight)) ?
$maxWidth / $sizeInfo[0] :
$maxHeight / $sizeInfo[1];
$newWidth = $sizeInfo[0] * $ratio;
$newHeight = $sizeInfo[1] * $ratio;
$thumb = imagecreatetruecolor($newWidth, $newHeight);
switch($sizeInfo[2]) // image type
{
case IMAGETYPE_GIF:
$source = imagecreatefromgif($file);
break;
case IMAGETYPE_JPEG:
$source = imagecreatefromjpeg($file);
break;
case IMAGETYPE_PNG:
$source = imagecreatefrompng($file);
break;
case IMAGETYPE_WBMP:
$source = imagecreatefromwbmp($file);
break;
default:
header(“HTTP/1.0 404 Not Found”);
exit;
}
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newWidth, $newHeight, $sizeInfo[0], $sizeInfo[1]);
header(“Content-type: image/jpeg”);
imagejpeg($thumb);
// All done
exit;
}
}
}
}
// For simplicity’s sake, just send 404 if any errors
header(“HTTP/1.0 404 Not Found”);
?>
[/code]

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@bokehJun 14.2006 — There's something magical about GD functions and the creations you can make with them, anyway just a couple of minor things:

[I]imagecopyresampled()[/I] produces noticeably superior quality images than [I]imagecopyresized()[/I]. The argument list is identical for both. Maybe there is a performance penalty, I'm not sure, but the difference is obvious.

Both these functions expect interger values for the coordinates not floats.

[I]Content-Type[/I] should have a capital "T".

The type testing is an interesting exercise but not really necessary if every image will be output as a jpeg. [I]Imagecreatefromstring()[/I] will open all image types; this makes prior tests pretty redundant if this function is used.

My personal preference is to output the resized image in the same format as the source as this allows any unique features of each format (true colour, palette, alpha channel, transparency, etc) to be retained in the resized image but doing so makes the code a bit more complex.

[upl-file uuid=d57d6bf7-9113-4194-9c57-a672d73c49d2 size=2kB]imagecopyresized.jpg[/upl-file]

[upl-file uuid=e424cfc9-9a4c-4b48-b020-b5e8fd2553aa size=2kB]imagecopyresampled.jpg[/upl-file]
Copy linkTweet thisAlerts:
@bajanboostAug 17.2006 — How about if I wanted the program to create thumbnails as soon as new files where uploaded to the server image folder? Rather than manually executing the code.
Copy linkTweet thisAlerts:
@littlenedAug 17.2006 — In the past I've used phpThumb.

Basically, rather that putting the filename of the image in the IMG tag, you make a call to the phpThumb script. i.e. <img src="phpThumb.php?file=image.gif&w=100&h=100">

It takes the image, resizes it to the dimensions you require, returns the image to your page and keeps a cache. Next time you call the image it takes it from the cache, unless the image has changed, then it resizes it again.

It's a great little script

http://phpthumb.sourceforge.net/
Copy linkTweet thisAlerts:
@bokehAug 17.2006 — How about if I wanted the program to create thumbnails as soon as new files where uploaded to the server image folder? Rather than manually executing the code.[/QUOTE]Just give image[I]jpeg[/I]() a second argument:[code=php]imagejpeg($resource, $filename); [/code]
Copy linkTweet thisAlerts:
@themartyAug 17.2006 — in my benchmarks imagecopyresampled is roughly 4x slower
Copy linkTweet thisAlerts:
@bokehAug 17.2006 — in my benchmarks imagecopyresampled is roughly 4x slower[/QUOTE]That's cos it produces a superior image.
Copy linkTweet thisAlerts:
@themartyAug 17.2006 — i know. but i was just responding to what you said previously:
Maybe there is a performance penalty, I'm not sure [...][/quote]
now you can be sure ?
Copy linkTweet thisAlerts:
@bokehAug 17.2006 — i know. but i was just responding to what you said previously:

now you can be sure ?[/QUOTE]
How did you do the test?
Copy linkTweet thisAlerts:
@themartyAug 18.2006 — i still had a piece of code that created thumbnails of all the images it could find in a certain directory and measured the time from the start of the loop till the end. With imagecopyresize it took around 1.4 seconds, with imagecopyresample it took around 6.2

Not an ideal comparison as it measures also the other code, but that should actually work in favour of the slower function.
×

Success!

Help @NogDog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...