Caching GD created thumbnails

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Caching GD created thumbnails

Post by JayBird »

I am using the follwing code to generate thumbnails on the fly

Code: Select all

$BasePath = str_repeat("../", substr_count(dirname($_SERVER["PHP_SELF"]), "/"));

	require ($BasePath."includes/config.php");

	define(thumbnailWidth, "50");    
	header("Content-type: image/jpeg");
	$filename = $server_path.$HTTP_GET_VARS["filename"];      
	$source = imagecreatefromjpeg($filename);  
	$thumbX = thumbnailWidth;    
	$imageX = imagesx($source);
	$imageY = imagesy($source);    
	$thumbY = (int)(($thumbX*$imageY) / $imageX );        
	$dest  = imagecreatetruecolor($thumbX, $thumbY);
	imagecopyresampled ($dest, $source, 0, 0, 0, 0, $thumbX, $thumbY, $imageX, $imageY);        
	imagejpeg($dest);
	imagedestroy($dest);
	imagedestroy($source);
To, reduce sever load, i would like the write the thumbnail to the file system, and next time i want to load the thumbnail, check if one has been created, if it has, display it, otherwise generate the thumbnails.

I am calling the script like this

Code: Select all

echo "<img src="/includes/admin/thumbnail.php?filename=".$result['path']."" alt="what you want"/>";
Mark
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

couldnt you just automatically save it into a directory on the web server (for eg tn_cache) run an if statement when generating to see if its already there, you could set a cron job every 12 hours or something to automatically delete everything in that directory
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

yeah, something like that.

Not sure how to write the created image to the file system tho.

Mark
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

whoa i thought you were talking to yourself for a second
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

magicrobotmonkey wrote:whoa i thought you were talking to yourself for a second
ROFL :lol:
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

:lol: :lol:
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

but, couldnt you use output buffering to write to a file? or something like that?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

You know what they say, if you want something doing, do it yourself....so i have. Here is the solution.

If you change the original image, the thumbnail won't get updated, you'll need to delete the thumbnail manually for the it to be created again, but it works for my situation

Code: Select all

$BasePath = str_repeat("../", substr_count(dirname($_SERVER["PHP_SELF"]), "/"));

// Require our config file
require ($BasePath."includes/config.php");

// Define width of thumbnails
define(thumbnailWidth, "50");   

// Build full server path to image
$filename = $server_path.$HTTP_GET_VARS["filename"];  

// Remove .jpg from the full path to large image
$thumbnailDestination = substr($filename, 0, -4);

// Add new extension to the path, creating our thumbnail path
$thumbnailDestination = $thumbnailDestination."_tn.jpg";

// If thumbnal doesn't exist, create one
if (!file_exists($thumbnailDestination)) {
	// Output our headers 
	header("Content-type: image/jpeg");
	$source = imagecreatefromjpeg($filename);  
	$thumbX = thumbnailWidth;    
	$imageX = imagesx($source);
	$imageY = imagesy($source);    
	$thumbY = (int)(($thumbX*$imageY) / $imageX );        
	$dest  = imagecreatetruecolor($thumbX, $thumbY);
	imagecopyresampled ($dest, $source, 0, 0, 0, 0, $thumbX, $thumbY, $imageX, $imageY);        
	imagejpeg($dest);

	// Write the thumbnail to the file system
	imagejpeg($dest, $thumbnailDestination);

	imagedestroy($dest);
	imagedestroy($source);
} else { // Just read and output the pre-generated thumnail
	header('Content-type: image/jpeg');
	header('Content-transfer-encoding: binary');
	header('Content-length: '.filesize($thumbnailDestination));
	readfile($thumbnailDestination);
}
Mark
Post Reply