The Coder

Create Thumbnail

Original posted by: Mick on 27-02-2007

There are lot of free image hosting sites popping up every day, some are better then others. One of my favorites is Imageshack only because it shows the dimensions and size of the image that you are uploading.

I've written a little function that will show you how to do this yourself. You can choose to save the file into a directory or you can create it on the fly.


<?php
//Image On Server
function create_thumb($image_address, $on_fly = false) {
//Lets get the image address
$image_explode = explode('/', $image_address);

//Directory we are going to store the thumb in
$folder_address_thumb = 'images/thumb/'.end($image_explode);

//Check to see if folder is writable
if((!$on_fly && is_writable($folder_address_thumb)) || $on_fly) {

//Check to see if the image exists
if(file_exists($image_address)) {

//Get the image dimensions
@list($width, $height, $extension) = getimagesize($image_address);

//Get the image size
$size = filesize($image_address);

//Determine the thumb size (pixels)
$thumb = 250;

//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, remember we want to add information
//at the bottom of the image so we'll add an extra 50 pixels to the height
$thumb_frame = imagecreatetruecolor($thumb_width, ($thumb_height + 50));

//Choose the background colour. I'm going to choose black
$thumb_background = imagecolorallocate($thumb_frame, 0, 0, 0);

//Choose the colour of the text, I'm going to choose white
$thumb_string = imagecolorallocate($thumb_frame, 255, 255, 255);

//Lets fill the frame
imagefill($thumb_frame, 0, 0, $thumb_background);

//Lets write the text
imagestring($thumb_frame, 5, 2, $thumb_height,
'Image Dimensions: '.$width.'x'.$height, $thumb_string);
imagestring($thumb_frame, 5, 2, $thumb_height + 25,
'Image Size: '.number_format($size).' KB', $thumb_string);



//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);
if($on_fly) {
//Generate the image
header('Content-type: image/gif');
imagegif($thumb_frame);
exit();
} else {
//Save the image
imagegif($thumb_frame, $folder_address_thumb);
}
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);
if($on_fly) {
//Generate the image
header('Content-type: image/jpeg');
imagejpeg($thumb_frame);
exit();
} else {
//Save the image
imagejpeg($thumb_frame, $folder_address_thumb);
}

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);

if($on_fly) {
//Generate the image
header('Content-type: image/png');
imagepng($thumb_frame);
exit();
} else {
//Save the image
imagepng($thumb_frame, $folder_address_thumb);
}
break;
default:
//No valid case was found
return 'Not a valid image';
break;
}


} else {
return 'File does not exist';
}
} else {
return 'Folder not writable';
}
}
?>


To use this function all you have to do is send it a valid address of an image, such as;


//Save into directory
create_thumb('/home/username/public_html/images/73a74382cc903a2162d241fbbf2ab8cf.jpg');

//Save into directory
create_thumb('/home/username/public_html/images/73a74382cc903a2162d241fbbf2ab8cf.jpg');

//Create on the fly
create_thumb('/home/username/public_html/images/73a74382cc903a2162d241fbbf2ab8cf.jpg', false);


You can also write the name of the file on the image by adding in the following line below the other 'imagestring' functions


imagestring($thumb_frame, 5, 2, $thumb_height + 50, 'Image Name: '.end($image_explode), $thumb_string);


The code will produce an thumbnail like this (click it to see the original)




Login to make a comment 0 Comments Found