Thumbnails are basically a complicated yet effective way to save bandwidth. Although saving thumbnails as seperate images may make things simpler, generating thumbs on the fly help saving space and takes very little processing time in return.
Check the file for code and instructions.
<?php
/*
Paramters
---------
w: width
h: height
zc: zoom crop (0 or 1)
q: quality (default is 75 and max is 100)
HTML example: <img src="/scripts/Thumbnail.php?src=/images/whatever.jpg&w=150&h=200&zc=1" alt="" />
*/
define("CACHE_SIZE", 200); // number of files to store before clearing cache
define("CACHE_CLEAR", 5); // maximum number of files to delete on each cache clear
define("VERSION", "1.08"); // version number (to force a cache refresh
// set path to cache directory (default is ./cache)
// this can be changed to a different location
$cache_dir = './cache';
// get mime type of src
$mime_type = mime_type($src);
// check to see if this image is in the cache already
check_cache( $cache_dir, $mime_type );
// if not in cache then clear some space and generate a new file
cleanCache();
ini_set('memory_limit', "30M");
// make sure that the src is gif/jpg/png
if(!valid_src_mime_type($mime_type)) {
displayError("Invalid src mime type: " .$mime_type);
}
// check to see if GD function exist
if(!function_exists('imagecreatetruecolor')) {
displayError("GD Library Error: imagecreatetruecolor does not exist");
}
if(strlen($src) && file_exists($src)) {
// open the existing image
$image = open_image($mime_type, $src);
if($image === false) {
displayError('Unable to open image : ' . $src);
}
// Get original width and height
$width = imagesx($image);
$height = imagesy($image);
// don't allow new width or height to be greater than the original
if( $new_width > $width ) {
$new_width = $width;
}
if( $new_height > $height ) {
$new_height = $height;
}
// generate new w/h if not provided
if( $new_width && !$new_height ) {
$new_height = $height * ( $new_width / $width );
} elseif($new_height && !$new_width) {
$new_width = $width * ( $new_height / $height );
} elseif(!$new_width && !$new_height) {
$new_width = $width;
$new_height = $height;
}
// create a new true color image
$canvas = imagecreatetruecolor( $new_width, $new_height );
imagealphablending($canvas, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($canvas, 0, 0, $color);
// Restore transparency blending
imagesavealpha($canvas, true);
/**
* clean out old files from the cache
* you can change the number of files to store and to delete per loop in the defines at the top of the code
*/
function cleanCache() {
$files = glob("cache/*", GLOB_BRACE);
$yesterday = time() - (24 * 60 * 60);
if (count($files) > 0) {
usort($files, "filemtime_compare");
$i = 0;
if (count($files) > CACHE_SIZE) {
foreach ($files as $file) {
$i ++;
if ($i >= CACHE_CLEAR) {
return;
}
if (filemtime($file) > $yesterday) {
return;
}
unlink($file);
}
}
}
}
/**
* compare the file time of two files
*/
function filemtime_compare($a, $b) {
return filemtime($a) - filemtime($b);
}
/**
* determine the file mime type
*/
function mime_type($file) {
if (function_exists('mime_content_type')) {
$mime_type = mime_content_type($file);
}
// use PECL fileinfo to determine mime type
if (!valid_src_mime_type($mime_type)) {
if (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mime_type = finfo_file($finfo, $file);
finfo_close($finfo);
}
}
// try to determine mime type by using unix file command
// this should not be executed on windows
if (!valid_src_mime_type($mime_type) && $os != "WIN") {
if (preg_match("/FREEBSD|LINUX/", $os)) {
$mime_type = trim(@shell_exec('file -bi "' . $file . '"'));
}
}
// use file's extension to determine mime type
if (!valid_src_mime_type($mime_type)) {
if (strlen($ext) && strlen($types[$ext])) {
$mime_type = $types[$ext];
}
}
return $mime_type;
}
/**
*
*/
function valid_src_mime_type($mime_type) {
if (preg_match("/jpg|jpeg|gif|png/i", $mime_type)) {
return true;
}
return false;
}
/**
*
*/
function check_cache($cache_dir, $mime_type) {
// make sure cache dir exists
if (!file_exists($cache_dir)) {
// give 777 permissions so that developer can overwrite
// files created by web server user
mkdir($cache_dir);
chmod($cache_dir, 0777);
}
// don't allow users the ability to use '../'
// in order to gain access to files below document root
// src should be specified relative to document root like:
// src=images/img.jpg or src=/images/img.jpg
// not like:
// src=../images/img.jpg
$src = preg_replace("/\.\.+\//", "", $src);
//print_r($_SERVER);
// get path to image on file system
$src = get_document_root($src) . '/' . $src;
// the relative paths below are useful if Thumbnail is moved outside of document root
// specifically if installed in wordpress themes like mimbo pro:
// /wp-content/themes/mimbopro/scripts/Thumbnail.php
$paths = array(
".",
"..",
"../..",
"../../..",
"../../../..",
"../../../../.."
);