首页 > 编程 > PHP > 正文

PHP生成自适应大小的缩略图类及使用方法分享

2020-03-22 19:01:47
字体:
来源:转载
供稿:网友
把下面的代码直接复制,新建一个文件叫做 thumbnailimage.php ,文件名最好不要用大写,把以下代码复制进去:复制代码 代码如下:
?phpdefine ( '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
// ****************************************************************************html' target='_blank'>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.
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 );

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表