Copy the following code directly and create a new file named thumbnailimage. php. The file name should not be capitalized. Copy the following code:
Copy codeThe Code is as follows:
<? 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 ', 'Could not create the copy of <B> % s </B> ');
Define ('e _ 004 ', 'Could not 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 $ pai_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-> pai_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.
Thanks to Luis Larrateguy for the idea of this 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-> pai_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-> pai_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
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
?>
Usage:
1. reference the PHP file first (do not tell me not)
2. Call Code
Copy codeThe Code is as follows:
$ Tis = new ThumbnailImage ();
$ Tis-> src_file = "Path of the source file written here"
$ Tis-> dest_type = THUMB_JPEG; // The image generation type is jpg.
$ Tis-> dest_file = 'write the path of the destination file ';
$ Tis-> max_width = 120; // adaptive size, but the maximum width is 120
$ Tis-> max_height = 4000; // adaptive size, but the maximum height is 4000
$ Tis-> Output ();
The key to the code is max_width and max_height. If you fill in the value, you will be able to generate a file of about the same size. Generally, thumbnails are generated very considerate unless your image is very personal, for example, very long.