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

首頁 > 編程 > PHP > 正文

功能強(qiáng)大的PHP圖片處理類(水印、透明度、旋轉(zhuǎn))

2020-03-22 17:46:21
字體:
供稿:網(wǎng)友
非常強(qiáng)大的php圖片處理類,可以自定義圖片水印、透明度、圖片縮放、圖片銳化、圖片旋轉(zhuǎn)、圖片翻轉(zhuǎn)、圖片剪切、圖片反色。* 圖片處理函數(shù)功能:縮放、剪切、相框、水印、銳化、旋轉(zhuǎn)、翻轉(zhuǎn)、透明度、反色
* 處理并保存歷史記錄的思路:當(dāng)有圖片有改動(dòng)時(shí)自動(dòng)生成一張新圖片,命名方式可以考慮在原圖片的基礎(chǔ)上加上步驟,例如:圖片名稱+__第幾步具體代碼如下:
php html' target='_blank'>class picture{ var $PICTURE_URL; //要處理的圖片 var $DEST_URL = "temp__01.jpg"; //生成目標(biāo)圖片位置 var $PICTURE_CREATE; //要?jiǎng)?chuàng)建的圖片 var $TURE_COLOR; //新建一個(gè)真彩圖象
var $FONT_COLOR = "#000000"; //文字顏色 var $FONT_PATH = "font/simkai.ttf"; //字體庫,默認(rèn)為宋體 var $FORCE_URL; //水印圖片 var $FORCE_X = 0; //水印橫坐標(biāo) var $FORCE_Y = 0; //水印縱坐標(biāo) var $FORCE_START_X = 0; //切起水印的圖片橫坐標(biāo) var $FORCE_START_Y = 0; //切起水印的圖片縱坐標(biāo)
$this - PICTURE_CREATE = imagecreatefromgif($PICTURE_URL); $this - PICTURE_TYPE = "imagejpeg"; $this - PICTURE_EXT = "jpg"; break; case 2: $this - PICTURE_CREATE = imagecreatefromjpeg($PICTURE_URL); $this - PICTURE_TYPE = "imagegif"; $this - PICTURE_EXT = "gif"; break; case 3: $this - PICTURE_CREATE = imagecreatefrompng($PICTURE_URL); $this - PICTURE_TYPE = "imagepng"; $this - PICTURE_EXT = "png"; break; /** * 文字顏色轉(zhuǎn)換16進(jìn)制轉(zhuǎn)換成10進(jìn)制 preg_match_all("/([0-f]){2,2}/i", $this - FONT_COLOR, $MATCHES); if(count($MATCHES) == 3){ $this - RED = hexdec($MATCHES[0][0]); $this - GREEN = hexdec($MATCHES[0][1]); $this - BLUE = hexdec($MATCHES[0][2]); # end of __construct * 將16進(jìn)制的顏色轉(zhuǎn)換成10進(jìn)制的(R,G,B) function hex2dec(){ preg_match_all("/([0-f]){2,2}/i", $this - FONT_COLOR, $MATCHES); if(count($MATCHES) == 3){ $this - RED = hexdec($MATCHES[0][0]); $this - GREEN = hexdec($MATCHES[0][1]); $this - BLUE = hexdec($MATCHES[0][2]); // 縮放類型 function zoom_type($ZOOM_TYPE){ $this - ZOOM = $ZOOM_TYPE; // 對(duì)圖片進(jìn)行縮放,如果不指定高度和寬度就進(jìn)行縮放 function zoom(){ // 縮放的大小 if($this - ZOOM == 0){ $this - ZOOM_WIDTH = $this - PICTURE_WIDTH * $this - ZOOM_MULTIPLE; $this - ZOOM_HEIGHT = $this - PICTURE_HEIGHT * $this - ZOOM_MULTIPLE; // 新建一個(gè)真彩圖象 $this - TRUE_COLOR = imagecreatetruecolor($this - ZOOM_WIDTH, $this - ZOOM_HEIGHT); $WHITE = imagecolorallocate($this - TRUE_COLOR, 255, 255, 255); imagefilledrectangle($this - TRUE_COLOR, 0, 0, $this - ZOOM_WIDTH, $this - ZOOM_HEIGHT, $WHITE); imagecopyresized($this - TRUE_COLOR, $this - PICTURE_CREATE, 0, 0, 0, 0, $this - ZOOM_WIDTH, $this - ZOOM_HEIGHT, $this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); # end of zoom // 裁切圖片,按坐標(biāo)或自動(dòng) function cut(){ $this - TRUE_COLOR = imagecreatetruecolor($this - CUT_WIDTH, $this - CUT_WIDTH); imagecopy($this - TRUE_COLOR, $this - PICTURE_CREATE, 0, 0, $this - CUT_X, $this - CUT_Y, $this - CUT_WIDTH, $this - CUT_HEIGHT); # end of cut * 在圖片上放文字或圖片 * 水印文字 function _mark_text(){ $this - TRUE_COLOR = imagecreatetruecolor($this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); $this - WORD = mb_convert_encoding($this - FONT_WORD, 'utf-8', 'gb2312'); /** * 取得使用 TrueType 字體的文本的范圍 $TEMP = imagettfbbox($this - FONT_SIZE, 0, $this - FONT_PATH, $this - WORD); $WORD_LENGTH = strlen($this - WORD); $WORD_WIDTH = $TEMP[2] - $TEMP[6]; $WORD_HEIGHT = $TEMP[3] - $TEMP[7]; /** * 文字水印的默認(rèn)位置為右下角 if($this - WORD_X == ""){ $this - WORD_X = $this - PICTURE_WIDTH - $WORD_WIDTH; if($this - WORD_Y == ""){ $this - WORD_Y = $this - PICTURE_HEIGHT - $WORD_HEIGHT; imagesettile($this - TRUE_COLOR, $this - PICTURE_CREATE); imagefilledrectangle($this - TRUE_COLOR, 0, 0, $this - PICTURE_WIDTH, $this - PICTURE_HEIGHT, IMG_COLOR_TILED); $TEXT2 = imagecolorallocate($this - TRUE_COLOR, $this - RED, $this - GREEN, $this - Blue); imagettftext($this - TRUE_COLOR, $this - FONT_SIZE, $this - ANGLE, $this - WORD_X, $this - WORD_Y, $TEXT2, $this - FONT_PATH, $this - WORD); * 水印圖片 function _mark_picture(){ /** * 獲取水印圖片的信息 @$SIZE = getimagesize($this - FORCE_URL); if(!$SIZE){ exit($this - ERROR['unalviable']); $FORCE_PICTURE_WIDTH = $SIZE[0]; $FORCE_PICTURE_HEIGHT = $SIZE[1]; // 創(chuàng)建水印圖片 switch($SIZE[2]){ case 1: $FORCE_PICTURE_CREATE = imagecreatefromgif($this - FORCE_URL); $FORCE_PICTURE_TYPE = "gif"; break; case 2: $FORCE_PICTURE_CREATE = imagecreatefromjpeg($this - FORCE_URL); $FORCE_PICTURE_TYPE = "jpg"; break; case 3: $FORCE_PICTURE_CREATE = imagecreatefrompng($this - FORCE_URL); $FORCE_PICTURE_TYPE = "png"; break; /** * 判斷水印圖片的大小,并生成目標(biāo)圖片的大小,如果水印比圖片大,則生成圖片大小為水印圖片的大小。否則生成的圖片大小為原圖片大小。 $this - NEW_PICTURE = $this - PICTURE_CREATE; if($FORCE_PICTURE_WIDTH $this - PICTURE_WIDTH){ $CREATE_WIDTH = $FORCE_PICTURE_WIDTH - $this - FORCE_START_X; }else{ $CREATE_WIDTH = $this - PICTURE_WIDTH; if($FORCE_PICTURE_HEIGHT $this - PICTURE_HEIGHT){ $CREATE_HEIGHT = $FORCE_PICTURE_HEIGHT - $this - FORCE_START_Y; }else{ $CREATE_HEIGHT = $this - PICTURE_HEIGHT; * 創(chuàng)建一個(gè)畫布 $NEW_PICTURE_CREATE = imagecreatetruecolor($CREATE_WIDTH, $CREATE_HEIGHT); $WHITE = imagecolorallocate($NEW_PICTURE_CREATE, 255, 255, 255); * 將背景圖拷貝到畫布中 imagecopy($NEW_PICTURE_CREATE, $this - PICTURE_CREATE, 0, 0, 0, 0, $this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); * 將目標(biāo)圖片拷貝到背景圖片上 imagecopy($NEW_PICTURE_CREATE, $FORCE_PICTURE_CREATE, $this - FORCE_X, $this - FORCE_Y, $this - FORCE_START_X, $this - FORCE_START_Y, $FORCE_PICTURE_WIDTH, $FORCE_PICTURE_HEIGHT); $this - TRUE_COLOR = $NEW_PICTURE_CREATE; # end of mark function alpha_(){ $this - TRUE_COLOR = imagecreatetruecolor($this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); $rgb = "#CDCDCD"; $tran_color = "#000000"; for($j = 0;$j = $this - PICTURE_HEIGHT-1;$j++){ for ($i = 0;$i = $this - PICTURE_WIDTH-1;$i++) $rgb = imagecolorat($this - PICTURE_CREATE, $i, $j); $r = ($rgb 16) $g = ($rgb 8) $b = $rgb $now_color = imagecolorallocate($this - PICTURE_CREATE, $r, $g, $b); if ($now_color == $tran_color) continue; else $color = imagecolorallocatealpha($this - PICTURE_CREATE, $r, $g, $b, $ALPHA); imagesetpixel($this - PICTURE_CREATE, $ALPHA_X + $i, $ALPHA_Y + $j, $color); $this - TRUE_COLOR = $this - PICTURE_CREATE; * 圖片旋轉(zhuǎn): * 沿y軸旋轉(zhuǎn) function turn_y(){ $this - TRUE_COLOR = imagecreatetruecolor($this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); for ($x = 0; $x $this - PICTURE_WIDTH; $x++) imagecopy($this - TRUE_COLOR, $this - PICTURE_CREATE, $this - PICTURE_WIDTH - $x - 1, 0, $x, 0, 1, $this - PICTURE_HEIGHT); * 沿X軸旋轉(zhuǎn) function turn_x(){ $this - TRUE_COLOR = imagecreatetruecolor($this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); for ($y = 0; $y $this - PICTURE_HEIGHT; $y++){ imagecopy($this - TRUE_COLOR, $this - PICTURE_CREATE, 0, $this - PICTURE_HEIGHT - $y - 1, 0, $y, $this - PICTURE_WIDTH, 1); * 任意角度旋轉(zhuǎn) function turn(){ $this - TRUE_COLOR = imagecreatetruecolor($this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); imageCopyResized($this - TRUE_COLOR, $this - PICTURE_CREATE, 0, 0, 0, 0, $this - PICTURE_WIDTH, $this - PICTURE_HEIGHT, $this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); $WHITE = imagecolorallocate($this - TRUE_COLOR, 255, 255, 255); $this - TRUE_COLOR = imagerotate ($this - TRUE_COLOR, $this - CIRCUMROTATE, $WHITE); * 圖片銳化 function sharp(){ $this - TRUE_COLOR = imagecreatetruecolor($this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); $cnt = 0; for ($x = 0; $x $this - PICTURE_WIDTH; $x++){ for ($y = 0; $y $this - PICTURE_HEIGHT; $y++){ $src_clr1 = imagecolorsforindex($this - TRUE_COLOR, imagecolorat($this - PICTURE_CREATE, $x-1, $y-1)); $src_clr2 = imagecolorsforindex($this - TRUE_COLOR, imagecolorat($this - PICTURE_CREATE, $x, $y)); $r = intval($src_clr2["red"] + $this - SHARP * ($src_clr2["red"] - $src_clr1["red"])); $g = intval($src_clr2["green"] + $this - SHARP * ($src_clr2["green"] - $src_clr1["green"])); $b = intval($src_clr2["blue"] + $this - SHARP * ($src_clr2["blue"] - $src_clr1["blue"])); $r = min(255, max($r, 0)); $g = min(255, max($g, 0)); $b = min(255, max($b, 0)); if (($DST_CLR = imagecolorexact($this - PICTURE_CREATE, $r, $g, $b)) == -1) $DST_CLR = imagecolorallocate($this - PICTURE_CREATE, $r, $g, $b); $cnt++; if ($DST_CLR == -1) die("color allocate faile at $x, $y ($cnt)."); imagesetpixel($this - TRUE_COLOR, $x, $y, $DST_CLR); * 將圖片反色處理 function return_color(){ * 創(chuàng)建一個(gè)畫布 $NEW_PICTURE_CREATE = imagecreate($this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); $WHITE = imagecolorallocate($NEW_PICTURE_CREATE, 255, 255, 255); * 將背景圖拷貝到畫布中 imagecopy($NEW_PICTURE_CREATE, $this - PICTURE_CREATE, 0, 0, 0, 0, $this - PICTURE_WIDTH, $this - PICTURE_HEIGHT); $this - TRUE_COLOR = $NEW_PICTURE_CREATE; * 生成目標(biāo)圖片并顯示 function show(){ // 判斷瀏覽器,若是IE就不發(fā)送頭 if(isset($_SERVER['HTTP_USER_AGENT'])) $ua = strtoupper($_SERVER['HTTP_USER_AGENT']); if(!preg_match('/^.*MSIE.*/)$/i', $ua)) header("Content-type:$this- PICTURE_MIME"); $OUT = $this - PICTURE_TYPE; $OUT($this - TRUE_COLOR); * 生成目標(biāo)圖片并保存 function save_picture(){ // 以 JPEG 格式將圖像輸出到瀏覽器或文件 $OUT = $this - PICTURE_TYPE; if(function_exists($OUT)){ // 判斷瀏覽器,若是IE就不發(fā)送頭 if(isset($_SERVER['HTTP_USER_AGENT'])) $ua = strtoupper($_SERVER['HTTP_USER_AGENT']); if(!preg_match('/^.*MSIE.*/)$/i', $ua)) header("Content-type:$this- PICTURE_MIME"); if(!$this - TRUE_COLOR){ exit($this - ERROR['unavilable']); }else{ $OUT($this - TRUE_COLOR, $this - DEST_URL); $OUT($this - TRUE_COLOR); * 析構(gòu)函數(shù):釋放圖片 function __destruct(){ * 釋放圖片 imagedestroy($this - TRUE_COLOR); imagedestroy($this - PICTURE_CREATE); # end of class 這就是非常強(qiáng)大的php圖片處理類,好好收藏,親,相信以后一定會(huì)派上用場(chǎng)的。PHP教程

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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 房产| 兰坪| 绥江县| 双牌县| 宁陕县| 汕头市| 本溪| 虞城县| 耿马| 淮南市| 屏山县| 岳西县| 贵溪市| 鹤峰县| 手机| 闵行区| 张家川| 兴海县| 荃湾区| 涿州市| 连南| 华宁县| 金华市| 肥城市| 满城县| 岱山县| 慈溪市| 盈江县| 千阳县| 大同县| 庄河市| 修武县| 平南县| 堆龙德庆县| 高陵县| 永吉县| 岐山县| 收藏| 灵石县| 广宁县| 昭平县|