This requires that GD is installed. I will probably expand upon this later to include watermark/text inclusion on the thumbnails, but this is all you need for now. This is a sector of code from a module I wrote for the CMS I'm working on, so it looks somewhat insignificant for a class alone:
<?php
class photos {
/**
* Checks the mime type of the given filename.
* // TODO: Make sure this works on windows servers
*
* @param string $f
* @return string
*/
function check_file_type($f) {
// Escape the shell arguments
$f = escapeshellarg($f);
// Remove whitespace
$r = trim(`file -bi $f`);
/**
* If there's a semi-colen, there's usually unwanted
* data after it. Remove it here:
*/
if( $p = strstr($r, ';') )
$r = substr($r, 0, ( strlen($r) - ( strlen($p) ) ) );
return $r;
}
/**
* Generates a thumbnail from a file; if MODE_SAVE, the read directory
* must be write accessable (mode 0777).
*
* @param int $x
* @param int $y
* @param string $filename
* @param FORMAT $format
* @param MODE $mode
* @return bool
*/
function generate_thumbnail($resize_x, $resize_y, $filename, $thumbnail_format=FORMAT_PNG, $mode=MODE_SAVE) {
$f = $filename;
$c = $filename;
$e = $this->get_extension($f);
// Open the file as the correct format
switch( strtolower($e) ) {
case 'jpg':
case 'jpeg':
$im = imagecreatefromjpeg($f);
break;
case 'gif':
$im = imagecreatefromgif($f);
break;
case 'wbmp':
$im = imagecreatefromwbmp($f);
break;
case 'png':
$im = imagecreatefrompng($f);
break;
}
// Get the image resolution
$original_x = imagesx($im);
$original_y = imagesy($im);
// Resize according to the original size -- shorten 200 to maintain correct x:y ratio
// x:y ratio
$xyr = ( $original_x / $original_y );
// If x:y > 1, y is the smaller of x and y; reduce its thumbnail y
if( $xyr > 1 ) {
// Round... there aren't half pixels!
$thumb_y = round( ( $resize_y / $xyr ), 0 );
$thumb_x = $resize_x;
} else if( $xyr < 1) { // If x:y < 1, x is the smaller of x and y; reduce its thumbnail x
$thumb_y = $resize_y;
$thumb_x = round( ( $resize_x * $xyr ), 0 );
} else if( $xyr == 1) { // In the rare event x:y = 1, don't change the thumbnail resolution
$thumb_y = $resize_y;
$thumb_x = $resize_x;
}
// Create an image object to write the thumbnail to
$thumb_im = imagecreatetruecolor($thumb_x, $thumb_y);
// Copy the original image to the thumbnail and resize it
imagecopyresized($thumb_im, $im, 0, 0, 0, 0, $thumb_x, $thumb_y, $original_x, $original_y);
// Destroy the original image object; we don't need it anymore
imagedestroy($im);
// Alright, we're done; process the image
if( $mode == MODE_SAVE ) {
// Generate a name for the thumbnail: <filename>-thumb.<extension>
$j = substr( $c, 0, ( strlen($c) - ( strlen($e) + 1 ) ) );
$thumb_url = "$j-thumb.$thumbnail_format";
// Write the thumbnail image to a file
eval("image$thumbnail_format(\$thumb_im, '$thumb_url');");
// If the file wasn't written, return false
if(! file_exists($thumb_url) )
return false;
} else if( $mode == MODE_OUTPUT ) {
// Display the image in the browser
header("Content-Type: image/$thumbnail_format");
eval("image$thumbnail_format(\$thumb_im);");
}
imagedestroy($thumb_im);
return true;
}
/**
* Gets the extension of the given filename.
*
* @param string $f
* @return string
*/
function get_extension($f) {
$e = explode('.', $f);
if( sizeof($e) ) {
$e = $e[ sizeof($e) - 1 ];
if( strstr($e, '/') ) {
return NULL;
} else {
return $e;
}
} else {
return NULL;
}
}
}
?>