Page 1 of 1

Caching GD created thumbnails

Posted: Tue May 25, 2004 8:14 am
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

Posted: Tue May 25, 2004 8:25 am
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

Posted: Tue May 25, 2004 8:27 am
by JayBird
yeah, something like that.

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

Mark

Posted: Tue May 25, 2004 8:44 am
by magicrobotmonkey
whoa i thought you were talking to yourself for a second

Posted: Tue May 25, 2004 8:45 am
by JayBird
magicrobotmonkey wrote:whoa i thought you were talking to yourself for a second
ROFL :lol:

Posted: Tue May 25, 2004 8:46 am
by malcolmboston
:lol: :lol:

Posted: Tue May 25, 2004 8:54 am
by magicrobotmonkey
but, couldnt you use output buffering to write to a file? or something like that?

Posted: Tue May 25, 2004 9:21 am
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