国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP生成自適應(yīng)大小的縮略圖類及使用方法分享

2020-03-22 19:01:47
字體:
供稿:網(wǎng)友
把下面的代碼直接復(fù)制,新建一個(gè)文件叫做 thumbnailimage.php ,文件名最好不要用大寫,把以下代碼復(fù)制進(jìn)去:復(fù)制代碼 代碼如下:
?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 );

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 潼关县| 肇东市| 尚义县| 靖江市| 宿州市| 大同市| 白沙| 六安市| 广河县| 土默特左旗| 兴海县| 淮北市| 正阳县| 合肥市| 定襄县| 玛纳斯县| 靖边县| 巴楚县| 寿光市| 开原市| 新和县| 普定县| 壶关县| 明光市| 灵川县| 广平县| 广丰县| 图木舒克市| 章丘市| 台安县| 四子王旗| 漠河县| 旬阳县| 桐庐县| 盐山县| 文昌市| 德格县| 彰化市| 富平县| 茶陵县| 沈丘县|