Thumbnails with Mod Rewrite
Original posted by: Admin on 30-01-2007
In the previous tutorial there was a function that created thumbnails and printed a little bit of information about the image at the bottom.
This function can be quiet handy, in particular when you are wanting to create a lot of thumbnails with little effort.
I'm going to slightly modify the previous tutorial so that it doesn't print the image dimensions and extra information on the bottom.
It's also going to take a few different parameters, it will accept the thumbnail size and address.
<?php
function create_quick_thumb($image_address, $thumb) {
//Lets change the image address to the new name
$image_address = str_replace('/'.$thumb.'-thumb/', '/main/', $image_address);
//Lets get the image address
$image_explode = explode('/', $image_address);
//Check to see if the image exists
if(file_exists($image_address)) {
//Get the image dimensions
@list($width, $height, $extension) = getimagesize($image_address);
//We now have all the required variables to create the thumbnail
//Lets find the dimensions of the new image
if($width > $height)
if($width <= $thumb) {
$thumb_width = $width;
$thumb_height = $height;
} else {
$thumb_width = $thumb;
$thumb_height = $height * ($thumb / $width);
}
elseif($width < $height)
if($height <= $thumb) {
$thumb_width = $width;
$thumb_height = $height;
} else {
$thumb_width = $width * ($thumb / $height);
$thumb_height = $thumb;
}
elseif($width == $height)
if($width < $thumb) {
$thumb_width = $width;
$thumb_height = $height;
} else {
$thumb_width = $thumb;
$thumb_height = $thumb;
}
//Lets create the frame of the image
$thumb_frame = imagecreatetruecolor($thumb_width, $thumb_height);
//Choose the background colour. I'm going to choose black
$thumb_background = imagecolorallocate($thumb_frame, 0, 0, 0);
//Lets fill the frame
imagefill($thumb_frame, 0, 0, $thumb_background);
//We now need to merge the images together
switch($extension) {
//Image is a GIF
case '1':
$image_file = imagecreatefromgif($image_address);
//Create the new image
imagecopyresampled($thumb_frame, $image_file, 0, 0, 0, 0,
$thumb_width, $thumb_height, $width, $height);
//Generate the image
header('Content-type: image/gif');
imagejpeg($thumb_frame);
exit();
break;
//Image is a JPG
case '2':
$image_file = imagecreatefromjpeg($image_address);
//Create the new image
imagecopyresampled($thumb_frame, $image_file, 0, 0, 0, 0,
$thumb_width, $thumb_height, $width, $height);
//Generate the image
header('Content-type: image/jpeg');
imagejpeg($thumb_frame);
exit();
break;
//Image is a PNG
case '3':
$image_file = imagecreatefrompng($image_address);
//Create the new image
imagecopyresampled($thumb_frame, $image_file, 0, 0, 0, 0,
$thumb_width, $thumb_height, $width, $height);
//Generate the image
header('Content-type: image/png');
imagepng($thumb_frame);
exit();
break;
default:
//File does not exist, so lets create a 404 Image
image_404($thumb);
break;
}
} else {
//File does not exist, so lets create a 404 Image
image_404($thumb);
}
}
?>
This script will pretty much do the same thing as the other thumbnail function however you can specify the size of the image and the address. It also has a 404 image function built into it, so if the server cannot find the image then it will print out a black box with a 404 message in it.
The function can be found below, all you have to do is send the size of the thumbnail
function image_404($thumb) {
//Lets create the frame of the image
$thumb_frame = imagecreatetruecolor($thumb, $thumb);
//Choose the colour of the text, I'm going to choose white
$thumb_string = imagecolorallocate($thumb_frame, 255, 255, 255);
//Choose the background colour. I'm going to choose black
$thumb_background = imagecolorallocate($thumb_frame, 0, 0, 0);
//Lets fill the frame
imagefill($thumb_frame, 0, 0, $thumb_background);
//Print Image Not Found String
imagestring($thumb_frame, 2, 10, 10, '404 Not Found', $thumb_string);
//Generate & Show Image
header('Content-type: image/png');
imagepng($thumb_frame);
exit();
}
Now this is only one part of the process, we still have to update the .htaccess file so that it can dynamicly create thumbnails.
If you insert this line into the htaccess file it will let you request a dynamic thumbnail file.
RewriteCond %{REQUEST_URI} .(jpeg|gif|png|jpg)$ [NC]
RewriteRule ^([0-9]+)-thumb/(.*)$ create_thumb.php?size=$1&name=$2 [L]
For example if you request <img src="/images/125-thumb/image_name.gif" /> it will create a a thumbnail with a max size of 125 from the file /images/main/images_name.gif
In the create_thumb.php file you should have the following line of code below your two functions
create_quick_thumb('images/'.$_GET['size'].'-thumb/'.$_GET['name'], $_GET['size']);
This is an example of how it will work, I have requested the following file;
<img src="http://thecoder.com.au/100-thumb/73a74382cc903a2162d241fbbf2ab8cf.jpg" />
However in this example I have requested a file that doesn't exist;
<img src="http://thecoder.com.au/100-thumb/no_file.jpg" />