This code snippet will allow you to generate thumbnails of images by visiting a URL such as yoursite.com/thumbs/images/myimage.100x100.jpg. This will create a 100px * 100px thumbnail of yoursite.com/images/myimage.jpg. The image will be stored on your server in yoursite.com/thumbs/images/myimage.100x100.jpg so the next request for the same image will be loaded without loading php. This will give you a very fast image caching system.
Upload phpThumb to yoursite.com/phpthumb
Create yoursite.com/thumbs/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?thumb=$1 [L,QSA]
</IfModule>Create yoursite.com/thumbs/index.php
<?php
$thumb = $_GET['thumb'];
if (!$thumb) {
exit;
}
$thumb_array = explode('.',$thumb);
$image = '../';
foreach($thumb_array as $k=>$thumb_part){
if ($k != count($thumb_array)-2) {
$image .= $thumb_part . '.';
}
}
$image = substr($image,0,-1);
list($width,$height) = explode('x',$thumb_array[count($thumb_array)-2]);
if (file_exists($image)) {
require('../phpthumb/phpthumb.class.php');
$phpThumb = new phpThumb();
$phpThumb->setSourceFilename($image);
$phpThumb->setParameter('w',$width);
$phpThumb->setParameter('h',$height);
//$phpThumb->setParameter('far','C'); // scale outside
//$phpThumb->setParameter('bg','FFFFFF'); // scale outside
if ($phpThumb->GenerateThumbnail()) {
mkdir(dirname($thumb),0777,true);
if ($phpThumb->RenderToFile($thumb)) {
header('Location: /thumbs/'.$thumb);
exit;
}
}
}
?>