This class is an hack of the Thumbnail class created by Philipp v. Criegern
you can download the latest version of the SmartThumb (v. 1.2 02/03/2004) class or take a look of the source
here some demo of the class:
<?php /**/ ?><?php
/**
* Thumbnail Class
*
* Creates resized (preview-) images and add watermark
*
* Usage Example:
* $img = new Thumbnail();
* echo $img->create_tag("../images/photos/sunflower.jpg");
*
* or
*
* $img = new Thumbnail();
* $img->watermark_file = '../images/logo.png';
* echo $img->add_watermark("../images/photos/sunflower.jpg");
*
* @author Philipp v. Criegern <criegep@criegern.com>
* @author Francesco Fullone <ffullone@progettoaroma.com>
*
* @version 1.2 02/03/2004 (hacked)
*
* WHATS NEW 1.2
* - prefix to thumbnail name
*
* OLD VERSION
* - 1.1 27/04/2003
*/
class Thumbnail
{
/**
* Directory where the created thumbnails are stored in (for PHP access)
* e.g. '/usr/local/apache/htdocs/images/thumbnails'
* Can be overwritten by global configuration array $_CONFIG['thumbnail_dir_internal']
*
* @access public
*/
var $thumbnail_dir_internal = '';
/**
* Path to thumbnail directory for browser access
* e.g. '/images/thumbnails'
* Can be overwritten by global configuration array $_CONFIG['thumbnail_dir_external']
*
* @access public
*/
var $thumbnail_dir_external = '';
/**
* Maximum pixel width of created thumbnail
*
* @access public
*/
var $max_width = 120;
/**
* Maximum pixel height of created thumbnail
*
* @access public
*/
var $max_height = 120;
/**
* File name of created thumbnail
* e.g. 'sunflower.png'
*
* @access public
*/
var $image_name;
/**
* Complete image tag for created thumbnail
* e.g. '<img src="/images/thumbnails/sunflower.png" width="120" height="80" border="0">'
*
* @access public
*/
var $image_tag;
/**
* Add a prefix to the thumbnail (or watermark) name
* e.g. thumb_sunflower.jpg
*
* @access public
*/
var $thumb_prefix = 'thumb_';
/**
* Error message if creation fails
*
* @access public
*/
var $error;
/**
* Pixel width of created thumbnail
*
* @access public
*/
var $width;
/**
* Pixel height of created thumbnail
*
* @access public
*/
var $height;
/**
* where the watermark is inserted
* possible value topleft, bottomleft, topright, bottomright
*
* @access public
*/
var $watermark_position = "TOPLEFT";
/**
* the watermark filename
* MUST be a PNG24 file!
*
* @access public
*/
var $watermark_file;
/**
* which kind of output is request
* valid value is JPG, PNG
*
* @access public
*/
var $extension = 'JPG';
/**
* Thumbnail Constructor
*
* @access public
*/
function Thumbnail ()
{
global $_CONFIG;
if (!empty($_CONFIG['thumbnail_dir_internal']))
{
$this->thumbnail_dir_internal = $_CONFIG['thumbnail_dir_internal'];
}
if (!empty($_CONFIG['thumbnail_dir_external']))
{
$this->thumbnail_dir_external = $_CONFIG['thumbnail_dir_external'];
}
}
/**
* Create Thumbnail and return Image Name
*
* @access public
* @param string $parameter Filename of source image
* @return string Filename of created thumbnail
*/
function create_name ( $parameter = '' )
{
$this->create( $parameter );
return $this->image_name;
}
/**
* Create Thumbnail and return Image Tag
*
* @access public
* @param string $parameter Filename of source image
* @return string Complete HTML Image-Tag of created thumbnail
*/
function create_tag ( $parameter = '' )
{
$this->create( $parameter );
return $this->image_tag;
}
/**
* Create Thumbnail
*
* @access public
* @param string $parameter Filename of source image
* @return void
*/
function create ( $imagefile )
{
$this->check_conf();
$this->image_name = basename($imagefile);
if (!is_file($this->thumbnail_dir_internal . $this->thumb_prefix . $this->image_name))
{
$old = $this->image_info($imagefile);
$old_w = $this->width;
$old_h = $this->height;
if ($this->max_width && ($this->width > $this->max_width))
{
$this->height = round($this->height * $this->max_width / $this->width);
$this->width = $this->max_width;
}
if ($this->max_height && ($this->height > $this->max_height))
{
$this->width = round($this->width * $this->max_height / $this->height);
$this->height = $this->max_height;
}
if (function_exists('imagecreatetruecolor'))
{
$new = imagecreatetruecolor($this->width, $this->height);
imagecopyresampled($new, $old, 0,0, 0,0, $this->width, $this->height, $old_w, $old_h);
}
else
{
$new = imagecreate($this->width, $this->height);
imagecopyresized($new, $old, 0,0, 0,0, $this->width, $this->height, $old_w, $old_h);
}
$this->save_image($new);
ImageDestroy($new);
ImageDestroy($old);
}
else
{
$arr = getimagesize($this->thumbnail_dir_internal . $this->image_name);
$this->width = $arr[0];
$this->height = $arr[1];
}
$this->image_tag = '<IMG SRC="' . $this->thumbnail_dir_external . $this->image_name
. '" WIDTH="' . $this->width
. '" HEIGHT="' . $this->height
. '" BORDER="0">';
}
/**
* Add watermark to the image,
* using a PNG24 image file
* this method REQUIRE the GD 2.0+
*
* @access public
* @param string $imagefile Filename of source image
* @return void
*/
function PNG_watermark ( $imagefile )
{
if ( (is_file($this->watermark_file)) && (function_exists('imagecreatetruecolor')) )
{
$this->image_name = 'watermark_'.basename($imagefile);
if (!is_file($this->thumbnail_dir_internal . $this->image_name))
{
$old = $this->image_info($imagefile);
$logo = ImageCreateFromPNG($this->watermark_file) or die($this->watermark_file.' is not a valid PNG24 file!');
$logo_w = imagesX($logo);
$logo_h = imagesY($logo);
if (strtoupper($this->watermark_position) == "TOPLEFT")
{
$src_x = 0;
$src_y = 0;
}
elseif (strtoupper($this->watermark_position) == "TOPRIGHT")
{
$src_x = $this->width - $logo_w;
$src_y = 0;
}
elseif (strtoupper($this->watermark_position) == "BOTTOMLEFT")
{
$src_x = 0;
$src_y = $this->height - $logo_h;
}
elseif (strtoupper($this->watermark_position) == "BOTTOMRIGHT")
{
$src_x = $this->width - $logo_w;
$src_y = $this->height - $logo_h;
}
else
{
die ('the watermark position: ' . $this->watermark_position . ' is non recognized! try with: TOPLEFT, BOTTOMLEFT, TOPRIGHT, BOTTOMRIGHT');
}
$new = imagecreatetruecolor($this->width, $this->height);
ImageAlphaBlending($new, true) or die ("Could not alpha blend");
ImageCopy($new, $old, 0, 0, 0, 0, $this->width, $this->height);
ImageCopy($new, $logo, $src_x, $src_y, 0, 0, $logo_w, $logo_h);
$this->save_image($new);
ImageDestroy($new);
ImageDestroy($old);
ImageDestroy($logo);
}
else
{
$arr = getimagesize($this->thumbnail_dir_internal . $this->image_name);
$this->width = $arr[0];
$this->height = $arr[1];
}
$this->image_tag = '<IMG SRC="' . $this->thumbnail_dir_external . $this->image_name
. '" WIDTH="' . $this->width
. '" HEIGHT="' . $this->height
. '" BORDER="0">';
}
else { die('il file: '. $this->watermark_file.' non esiste!'); }
}
/**
* check the directory and update the class var
*
* @access private
* @return void
*/
function check_conf()
{
if (strlen($this->thumbnail_dir_internal) && (substr($this->thumbnail_dir_internal, -1) != '/'))
{
$this->thumbnail_dir_internal .= '/';
}
if (strlen($this->thumbnail_dir_external) && (substr($this->thumbnail_dir_external, -1) != '/'))
{
$this->thumbnail_dir_external .= '/';
}
}
/**
* check the imagefile info to create the correct image stream
*
* @access private
* @param $imagefile the image filename
* @return $img the image stream
*/
function image_info($imagefile)
{
$tmp_extension = strtoupper(substr(basename($imagefile), -3));
if ( $tmp_extension == 'JPG' )
{
$img = ImageCreateFromJPEG($imagefile) or die ("Could not create from JPEG");
}
elseif ( $tmp_extension == 'PNG' )
{
$img = ImageCreateFromPNG($imagefile) or die ("Could not create from PNG");
}
else
{
die('the extension '.$tmp_extension.' is not valid, try with JPG or PNG images');
}
$this->width = imagesX($img);
$this->height = imagesY($img);
return $img;
}
/**
* save the image to disk
*
* @access private
* @param $img the image stream
* @return void
*/
function save_image($img)
{
if (strtoupper($this->extension) == 'JPG' )
{
$this->image_name = substr($this->image_name, 0, -4) . '.jpg';
ImageJPEG($img, $this->thumbnail_dir_internal . $this->thumb_prefix . $this->image_name);
}
elseif (strtoupper($this->extension) == 'PNG' )
{
$this->image_name = substr($this->image_name, 0, -4) . '.png';
ImagePNG($img, $this->thumbnail_dir_internal . $this->thumb_prefix . $this->image_name);
}
else
{
die('the extension '.$this->extension.' is not valid, try with JPG or PNG');
}
}
/**
* delete a thumbnail or watermark file image from
* the thumbnail_dir_internal
*
* @access private
* @param $imagefile the image filename
* @return void
*/
function delete_image($imagefile)
{
$this->image_name = basename($imagefile);
if (is_file($this->thumbnail_dir_internal . $this->image_name))
{
unlink($this->thumbnail_dir_internal . $this->image_name);
}
else
{
die('the file '.$this->thumbnail_dir_internal . $this->image_name.' doesn\'t exist');
}
}
} //end class
?>