把下面的代码直接复制,新建一个文件叫做 thumbnailimage.php ,文件名最好不要用大写,把以下代码复制进去:
<?php</P><P>define ( 'MAX_IMG_SIZE', 100000 );</P><P>// Supported image types<BR>define ( 'THUMB_JPEG', 'image/jpeg' );<BR>define ( 'THUMB_PNG', 'image/png' );<BR>define ( 'THUMB_GIF', 'image/gif' );</P><P>// Interlacing modes<BR>define ( 'INTERLACE_OFF', 0 );<BR>define ( 'INTERLACE_ON', 1 );</P><P>// Output modes<BR>define ( 'STDOUT', '' );</P><P>// Empty constants<BR>define ( 'NO_LOGO', '' );<BR>define ( 'NO_LABEL', '' );</P><P>// Logo and label positioning<BR>define ( 'POS_LEFT', 0 );<BR>define ( 'POS_RIGHT', 1 );<BR>define ( 'POS_CENTER', 2 );<BR>define ( 'POS_TOP', 3 );<BR>define ( 'POS_BOTTOM', 4 );</P><P>// Error messages<BR>define ( 'E_001', 'File <b>%s</b> do not exist' );<BR>define ( 'E_002', 'Failed reading image data from <b>%s</b>' );<BR>define ( 'E_003', 'Cannot create the copy of <b>%s</b>' );<BR>define ( 'E_004', 'Cannot copy the logo image' );<BR>define ( 'E_005', 'Cannot create final image' );</P><P>// ****************************************************************************<BR>// CLASS DEFINITION<BR>// ****************************************************************************</P><P>class ThumbnailImage<BR>{</P><P>// ****************************************************************************<BR>// PUBLIC PROPERTIES<BR>// ****************************************************************************</P><P> var $src_file; // source image file<BR> var $dest_file; // destination image file<BR> var $dest_type; // destination image type</P><P> var $interlace; // destination image interlacing mode<BR> var $jpeg_quality; // quality of resulting JPEG</P><P> var $max_width; // maximal thumbnail width<BR> var $max_height; // maximal thumbnail height<BR> var $fit_to_max; // enlarge small images?</P><P> var $logo; // array of logo parameters<BR> var $label; // array of label parameters</P><P>// ****************************************************************************<BR>// CLASS CONSTRUCTOR<BR>// ****************************************************************************</P><P> /*<BR> Description:<BR> Defines default values for properties.<BR> Prototype:<BR> void ThumbImg ( string src_file = '' )<BR> Parameters:<BR> src_file - source image filename<BR> */<BR> function ThumbnailImage ( $src_file = '' )<BR> {</P><P> $this->src_file = $src_file;<BR> $this->dest_file = STDOUT;<BR> $this->dest_type = THUMB_JPEG;</P><P> $this->interlace = INTERLACE_OFF;<BR> $this->jpeg_quality = -1;</P><P> $this->max_width = 100;<BR> $this->max_height = 90;<BR> $this->fit_to_max = FALSE;</P><P> $this->logo['file'] = NO_LOGO;<BR> $this->logo['vert_pos'] = POS_TOP;<BR> $this->logo['horz_pos'] = POS_LEFT;</P><P> $this->label['text'] = NO_LABEL;<BR> $this->label['vert_pos'] = POS_BOTTOM;<BR> $this->label['horz_pos'] = POS_RIGHT;<BR> $this->label['font'] = '';<BR> $this->label['size'] = 20;<BR> $this->label['color'] = '#000000';<BR> $this->label['angle'] = 0;</P><P> }</P><P>// ****************************************************************************<BR>// PRIVATE METHODS<BR>// ****************************************************************************</P><P> /*<BR> Description:<BR> Extracts decimal color components from hex color string.<BR> Prototype:<BR> array ParseColor ( string hex_color )<BR> Parameters:<BR> hex_color - color in '#rrggbb' format<BR> Return:<BR> Decimal values for red, green and blue color components.<BR> */<BR> function ParseColor ( $hex_color )<BR> {</P><P> if ( strpos ( $hex_color, '#' ) === 0 )<BR> $hex_color = substr ( $hex_color, 1 );</P><P> $r = hexdec ( substr ( $hex_color, 0, 2 ) );<BR> $g = hexdec ( substr ( $hex_color, 2, 2 ) );<BR> $b = hexdec ( substr ( $hex_color, 4, 2 ) );</P><P> return array ( $r, $g, $b );</P><P> }</P><P> /*<BR> Description:<BR> Retrives image data as a string.<BR> Thanks to Luis Larrateguy for the idea of this function.<BR> Prototype:<BR> string GetImageStr ( string image_file )<BR> Parameters:<BR> image_file - filename of image<BR> Return:<BR> Image file contents string.<BR> */<BR> function GetImageStr ( $image_file )<BR> {</P><P> if ( function_exists ( 'file_get_contents' ) )<BR> {<BR> $str = @file_get_contents ( $image_file );<BR> if ( ! $str )<BR> {<BR> $err = sprintf( E_002, $image_file );<BR> trigger_error( $err, E_USER_ERROR );<BR> }<BR> return $str;<BR> }</P><P> $f = fopen ( $image_file, 'rb' );<BR> if ( ! $f )<BR> {<BR> $err = sprintf( E_002, $image_file );<BR> trigger_error( $err, E_USER_ERROR );<BR> }<BR> $fsz = @filesize ( $image_file );<BR> if ( ! $fsz )<BR> $fsz = MAX_IMG_SIZE;<BR> $str = fread ( $f, $fsz );<BR> fclose ( $f );</P><P> return $str;</P><P> }</P><P> /*<BR> Description:<BR> Loads image from file.<BR> Prototype:<BR> resource LoadImage ( string image_file, int &image_width, int &image_height )<BR> Parameters:<BR> image_file - filename of image<BR> image_width - width of loaded image<BR> image_height - height of loaded image<BR> Return:<BR> Image identifier representing the image obtained from the given file.<BR> */<BR> function LoadImage ( $image_file, &$image_width, &$image_height )<BR> {</P><P> $image_width = 0;<BR> $image_height = 0;</P><P> $image_data = $this->GetImageStr( $image_file );</P><P> $image = imagecreatefromstring ( $image_data );<BR> if ( ! $image )<BR> {<BR> $err = sprintf( E_003, $image_file );<BR> trigger_error( $err, E_USER_ERROR );<BR> }</P><P> $image_width = imagesx ( $image );<BR> $image_height = imagesy ( $image );</P><P> return $image;</P><P> }</P><P> /*<BR> Description:<BR> Calculates thumbnail image sizes from source image width and height.<BR> Prototype:<BR> array GetThumbSize ( int src_width, int src_height )<BR> Parameters:<BR> src_width - width of source image<BR> src_height - height of source image<BR> Return:<BR> An array with 2 elements. Index 0 contains the width of thumbnail image<BR> and index 1 contains the height.<BR> */<BR> function GetThumbSize ( $src_width, $src_height )<BR> {</P><P> $max_width = $this->max_width;<BR> $max_height = $this->max_height;</P><P> $x_ratio = $max_width / $src_width;<BR> $y_ratio = $max_height / $src_height;</P><P> $is_small = ( $src_width <= $max_width && $src_height <= $max_height );</P><P> if ( ! $this->fit_to_max && $is_small )<BR> {<BR> $dest_width = $src_width;<BR> $dest_height = $src_height;<BR> }<BR> elseif ( $x_ratio * $src_height < $max_height )<BR> {<BR> $dest_width = $max_width;<BR> $dest_height = ceil ( $x_ratio * $src_height );<BR> }<BR> else<BR> {<BR> $dest_width = ceil ( $y_ratio * $src_width );<BR> $dest_height = $max_height;<BR> }</P><P> return array ( $dest_width, $dest_height );</P><P> }</P><P> /*<BR> Description:<BR> Adds logo image to thumbnail.<BR> Prototype:<BR> void AddLogo ( int thumb_width, int thumb_height, resource &thumb_img )<BR> Parameters:<BR> thumb_width - width of thumbnail image<BR> thumb_height - height of thumbnail image<BR> thumb_img - thumbnail image identifier<BR> */<BR> function AddLogo ( $thumb_width, $thumb_height, &$thumb_img )<BR> {</P><P> extract ( $this->logo );</P><P> $logo_image = $this->LoadImage ( $file, $logo_width, $logo_height );</P><P> if ( $vert_pos == POS_CENTER )<BR> $y_pos = ceil ( $thumb_height / 2 - $logo_height / 2 );<BR> elseif ($vert_pos == POS_BOTTOM)<BR> $y_pos = $thumb_height - $logo_height;<BR> else<BR> $y_pos = 0;</P><P> if ( $horz_pos == POS_CENTER )<BR> $x_pos = ceil ( $thumb_width / 2 - $logo_width / 2 );<BR> elseif ( $horz_pos == POS_RIGHT )<BR> $x_pos = $thumb_width - $logo_width;<BR> else<BR> $x_pos = 0;</P><P> if ( ! imagecopy ( $thumb_img, $logo_image, $x_pos, $y_pos, 0, 0,<BR> $logo_width, $logo_height ) )<BR> trigger_error( E_004, E_USER_ERROR );</P><P> }</P><P> /*<BR> Description:<BR> Adds label text to thumbnail.<BR> Prototype:<BR> void AddLabel ( int thumb_width, int thumb_height, resource &thumb_img )<BR> Parameters:<BR> thumb_width - width of thumbnail image<BR> thumb_height - height of thumbnail image<BR> thumb_img - thumbnail image identifier<BR> */<BR> function AddLabel ( $thumb_width, $thumb_height, &$thumb_img )<BR> {</P><P> extract ( $this->label );</P><P> list( $r, $g, $b ) = $this->ParseColor ( $color );<BR> $color_id = imagecolorallocate ( $thumb_img, $r, $g, $b );</P><P> $text_box = imagettfbbox ( $size, $angle, $font, $text );<BR> $text_width = $text_box [ 2 ] - $text_box [ 0 ];<BR> $text_height = abs ( $text_box [ 1 ] - $text_box [ 7 ] );</P><P> if ( $vert_pos == POS_TOP )<BR> $y_pos = 5 + $text_height;<BR> elseif ( $vert_pos == POS_CENTER )<BR> $y_pos = cei<strong>(本文来源gaodai#ma#com搞@@代~&码网</strong><pre>搞代gaodaima码
l( $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
?>
使用方法:
1、首先引用该php文件(不要告诉我不会)
2、调用代码
$tis = new ThumbnailImage();<BR> $tis->src_file = "这里写源文件的路径"<BR> $tis->dest_type = THUMB_JPEG;//生成图片的类型是 jpg<BR> $tis->dest_file = '这里写目标文件的路径';</P><P> $tis->max_width = 120;//自适应大小,但是最大宽度为120<BR> $tis->max_height = 4000; //自适应大小,但是最大高度为4000<BR> $tis->Output();<BR>
代码关键在于 max_width 和max_height,填多少,就会帮你生成一个 差不多大小的文件,一般来说除非你的图片很有个性,譬如很长,否则缩略图生成的还是很体贴的。