Copy the following code directly, create a new file called thumbnailimage.php, the file name is best not in uppercase, the following code to copy in:
<?php
Define (' Max_img_size ', 100000);
Supported Image types
Define (' Thumb_jpeg ', ' image/jpeg ');
Define (' Thumb_png ', ' image/png ');
Define (' Thumb_gif ', ' image/gif ');
interlacing modes
Define (' Interlace_off ', 0);
Define (' interlace_on ', 1);
Output modes
Define (' STDOUT ', ');
Empty Constants
Define (' No_logo ', ');
Define (' No_label ', ');
Logo and label positioning
Define (' Pos_left ', 0);
Define (' Pos_right ', 1);
Define (' Pos_center ', 2);
Define (' Pos_top ', 3);
Define (' Pos_bottom ', 4);
Error messages
Define (' e_001 ', ' File <b>%s</b> do not exist ');
Define (' e_002 ', ' Failed reading image data from <b>%s</b> ');
Define (' e_003 ', ' cannot create the copy of <b>%s</b> ');
Define (' e_004 ', ' cannot copy the logo image ');
Define (' e_005 ', ' cannot create final image ');
// ****************************************************************************
CLASS DEFINITION
// ****************************************************************************
Class Thumbnailimage
{
// ****************************************************************************
Public PROPERTIES
// ****************************************************************************
var $src _file; Source image File
var $dest _file; Destination image file
var $dest _type; Destination Image Type
var $interlace; Destination Image interlacing mode
var $jpeg _quality; Quality of resulting JPEG
var $max _width; Maximal thumbnail width
var $max _height; Maximal thumbnail height
var $fit _to_max; Enlarge small images?
var $logo; Array of logo parameters
var $label; Array of label parameters
// ****************************************************************************
CLASS Constructor
// ****************************************************************************
/*
Description:
Defines default values for properties.
Prototype:
void Thumbimg (String src_file = ')
Parameters:
Src_file-source image filename
*/
function thumbnailimage ($src _file = ')
{
$this->src_file = $src _file;
$this->dest_file = STDOUT;
$this->dest_type = thumb_jpeg;
$this->interlace = Interlace_off;
$this->jpeg_quality =-1;
$this->max_width = 100;
$this->max_height = 90;
$this->fit_to_max = FALSE;
$this->logo[' file '] = No_logo;
$this->logo[' vert_pos '] = pos_top;
$this->logo[' horz_pos '] = pos_left;
$this->label[' text '] = No_label;
$this->label[' vert_pos '] = Pos_bottom;
$this->label[' horz_pos '] = pos_right;
$this->label[' font '] = ';
$this->label[' size '] = 20;
$this->label[' color '] = ' #000000 ';
$this->label[' angle '] = 0;
}
// ****************************************************************************
PRIVATE METHODS
// ****************************************************************************
/*
Description:
Extracts decimal color components from hex color string.
Prototype:
Array Parsecolor (String hex_color)
Parameters:
Hex_color-color in ' #rrggbb ' format
return:
Decimal values for red, green and blue color components.
*/
function Parsecolor ($hex _color)
{
if (Strpos ($hex _color, ' # ') = = 0)
$hex _color = substr ($hex _color, 1);
$r = Hexdec (substr ($hex _color, 0, 2));
$g = Hexdec (substr ($hex _color, 2, 2));
$b = Hexdec (substr ($hex _color, 4, 2));
Return Array ($r, $g, $b);
}
/*
Description:
Retrives image data as a string.
The to Luis Larrateguy for the "idea's" function.
Prototype:
String Getimagestr (String image_file)
Parameters:
Image_file-filename of Image
return:
Image file contents string.
*/
function Getimagestr ($image _file)
{
if (function_exists (' file_get_contents '))
{
$str = @file_get_contents ($image _file);
if (! $str)
{
$err = sprintf (e_002, $image _file);
trigger_error ($err, e_user_error);
}
return $str;
}
$f = fopen ($image _file, ' RB ');
if (! $f)
{
$err = sprintf (e_002, $image _file);
Trigger_error ($err, e_user_error);
}
$fsz = @filesize ($image _file);
if (! $fsz)
$fsz = max_img_size;
$str = Fread ($f, $fsz);
Fclose ($f);
return $str;
}
/*
Description:
Loads image from file.
Prototype:
Resource LoadImage (string image_file, int &image_width, int &image_height)
Parameters:
Image_file-filename of Image
Image_width-width of Loaded image
Image_height-height of Loaded image
return:
Image identifier representing the image obtained from the given file.
*/
function LoadImage ($image _file, & $image _width, & $image _height)
{
$image _width = 0;
$image _height = 0;
$image _data = $this->getimagestr ($image _file);
$image = imagecreatefromstring ($image _data);
if (! $image)
{
$err = sprintf (e_003, $image _file);
Trigger_error ($err, e_user_error);
}
$image _width = Imagesx ($image);
$image _height = Imagesy ($image);
return $image;
}
/*
Description:
Calculates thumbnail image sizes from source image width and height.
Prototype:
Array getthumbsize (int src_width, int src_height)
Parameters:
Src_width-width of Source image
Src_height-height of Source image
return:
An array with 2 elements. Index 0 contains the width of thumbnail image
And index 1 contains the height.
*/
function Getthumbsize ($src _width, $src _height)
{
$max _width = $this->max_width;
$max _height = $this->max_height;
$x _ratio = $max _width/$src _width;
$y _ratio = $max _height/$src _height;
$is _small = ($src _width <= $max _width && $src _height <= $max _height);
if (! $this->fit_to_max && $is _small)
{
$dest _width = $src _width;
$dest _height = $src _height;
}
ElseIf ($x _ratio * $src _height < $max _height)
{
$dest _width = $max _width;
$dest _height = ceil ($x _ratio * $src _height);
}
Else
{
$dest _width = ceil ($y _ratio * $src _width);
$dest _height = $max _height;
}
Return Array ($dest _width, $dest _height);
}
/*
Description:
Adds logo image to thumbnail.
Prototype:
void Addlogo (int thumb_width, int thumb_height, resource &thumb_img)
Parameters:
Thumb_width-width of thumbnail image
Thumb_height-height of thumbnail image
Thumb_img-thumbnail Image Identifier
*/
function Addlogo ($thumb _width, $thumb _height, & $thumb _img)
{
Extract ($this->logo);
$logo _image = $this->loadimage ($file, $logo _width, $logo _height);
if ($vert _pos = = pos_center)
$y _pos = ceil ($thumb _height/2-$logo _HEIGHT/2);
ElseIf ($vert _pos = = pos_bottom)
$y _pos = $thumb _height-$logo _height;
Else
$y _pos = 0;
if ($horz _pos = = pos_center)
$x _pos = ceil ($thumb _width/2-$logo _WIDTH/2);
ElseIf ($horz _pos = = pos_right)
$x _pos = $thumb _width-$logo _width;
Else
$x _pos = 0;
if (! imagecopy ($thumb _img, $logo _image, $x _pos, $y _pos, 0, 0,
$logo _width, $logo _height))
Trigger_error (e_004, e_user_error);
}
/*
Description:
Adds label text to thumbnail.
Prototype:
void AddLabel (int thumb_width, int thumb_height, resource &thumb_img)
Parameters:
Thumb_width-width of thumbnail image
Thumb_height-height of thumbnail image
Thumb_img-thumbnail Image Identifier
*/
function AddLabel ($thumb _width, $thumb _height, & $thumb _img)
{
Extract ($this->label);
List ($r, $g, $b) = $this->parsecolor ($color);
$color _id = imagecolorallocate ($thumb _img, $r, $g, $b);
$text _box = Imagettfbbox ($size, $angle, $font, $text);
$text _width = $text _box [2]-$text _box [0];
$text _height = ABS ($text _box [1]-$text _box [7]);
if ($vert _pos = = pos_top)
$y _pos = 5 + $text _height;
ElseIf ($vert _pos = = pos_center)
$y _pos = ceil ($thumb _height/2-$text _HEIGHT/2);
ElseIf ($vert _pos = = pos_bottom)
$y _pos = $thumb _height-$text _height;
if ($horz _pos = = pos_left)
$x _pos = 5;
ElseIf ($horz _pos = = pos_center)
$x _pos = ceil ($thumb _width/2-$text _WIDTH/2);
ElseIf ($horz _pos = = pos_right)
$x _pos = $thumb _width-$text _width-5;
Imagettftext ($thumb _img, $size, $angle, $x _pos, $y _pos,
$color _id, $font, $text);
}
/*
Description:
Output thumbnail image into the browser.
Prototype:
void Outputthumbimage (Resource dest_image)
Parameters:
Dest_img-thumbnail Image Identifier
*/
function Outputthumbimage ($dest _image)
{
Imageinterlace ($dest _image, $this->interlace);
Header (' Content-type: '. $this->dest_type);
if ($this->dest_type = = thumb_jpeg)
Imagejpeg ($dest _image, ", $this->jpeg_quality);
ElseIf ($this->dest_type = = thumb_gif)
Imagegif ($dest _image);
ElseIf ($this->dest_type = = thumb_png)
Imagepng ($dest _image);
}
/*
Description:
Save thumbnail image into the disc file.
Prototype:
void Savethumbimage (String image_file, resource dest_image)
Parameters:
Image_file-destination file name
Dest_img-thumbnail Image Identifier
*/
function Savethumbimage ($image _file, $dest _image)
{
Imageinterlace ($dest _image, $this->interlace);
if ($this->dest_type = = thumb_jpeg)
Imagejpeg ($dest _image, $this->dest_file, $this->jpeg_quality);
ElseIf ($this->dest_type = = thumb_gif)
Imagegif ($dest _image, $this->dest_file);
ElseIf ($this->dest_type = = thumb_png)
Imagepng ($dest _image, $this->dest_file);
}
// ****************************************************************************
Public METHODS
// ****************************************************************************
/*
Description:
Output thumbnail image into the browser or disc file according to the
Values of parameters.
Prototype:
void Output ()
*/
function Output ()
{
$src _image = $this->loadimage ($this->src_file, $src _width, $src _height);
$dest _size = $this->getthumbsize ($src _width, $src _height);
$dest _width= $dest _size[0];
$dest _height= $dest _size[1];
$dest _image=imagecreatetruecolor ($dest _width, $dest _height);
if (! $dest _image)
Trigger_error (e_005, e_user_error);
Imagecopyresampled ($dest _image, $src _image, 0, 0, 0, 0,
$dest _width, $dest _height, $src _width, $src _height);
if ($this->logo[' file ']!= No_logo)
$this->addlogo ($dest _width, $dest _height, $dest _image);
if ($this->label[' text ']!= No_label)
$this->addlabel ($dest _width, $dest _height, $dest _image);
if ($this->dest_file = = STDOUT)
$this->outputthumbimage ($dest _image);
Else
$this->savethumbimage ($this->dest_file, $dest _image);
Imagedestroy ($src _image);
Imagedestroy ($dest _image);
}
}//End of class definition
?>
2. Calling code
The key to the code is max_width and Max_height, the number of filling, will help you generate a file of approximately the same size, in general, unless your image is very personalized, such as very long, otherwise thumbnail generation is very thoughtful.