最新PHP 圖檔處理類(水印、透明度、縮放、相框、銳化、旋轉、翻轉、剪切、反色)
以下是三零網為大家整理的最新PHP 圖檔處理類(水印、透明度、縮放、相框、銳化、旋轉、翻轉、剪切、反色)的文章,希望大家能夠喜歡!
可實作圖檔的縮放、剪切、相框、水印、銳化、旋轉、翻轉、透明度、反色功能操作。
<?php /** * 圖檔處理函數功能:縮放、剪切、相框、水印、銳化、旋轉、翻轉、透明度、反色 * 處理并儲存曆史記錄的思路:當有圖檔有改動時自動生成一張新圖檔,命名方式可以考慮在原圖檔的基礎上加上步驟,例如:圖檔名稱+__第幾步 */ class picture{ var $PICTURE_URL; //要處理的圖檔 var $DEST_URL = "temp__01.jpg"; //生成目标圖檔位置 var $PICTURE_CREATE; //要建立的圖檔 var $TURE_COLOR; //建立一個真彩圖象 var $PICTURE_WIDTH; //原圖檔寬度 var $PICTURE_HEIGHT; //原圖檔高度 /** * 水印的類型,預設的為水印文字 */ var $MARK_TYPE = 1; var $WORD; //經過UTF-8後的文字 var $WORD_X; //文字橫坐标 var $WORD_Y; //文字縱坐标 var $FONT_TYPE; //字型類型 var $FONT_SIZE = "12"; //字型大小 var $FONT_WORD; //文字 var $ANGLE = 0; //文字的角度,預設為0 var $FONT_COLOR = "#000000"; //文字顔色 var $FONT_PATH = "font/simkai.ttf"; //字型庫,預設為宋體 var $FORCE_URL; //水印圖檔 var $FORCE_X = 0; //水印橫坐标 var $FORCE_Y = 0; //水印縱坐标 var $FORCE_START_X = 0; //切起水印的圖檔橫坐标 var $FORCE_START_Y = 0; //切起水印的圖檔縱坐标 var $PICTURE_TYPE; //圖檔類型 var $PICTURE_MIME; //輸出的頭部 * 縮放比例為1的話就按縮放高度和寬度縮放 var $ZOOM = 1; //縮放類型 var $ZOOM_MULTIPLE; //縮放比例 var $ZOOM_WIDTH; //縮放寬度 var $ZOOM_HEIGHT; //縮放高度 * 裁切,按比例和固定長度、寬度 var $CUT_TYPE = 1; //裁切類型 var $CUT_X = 0; //裁切的橫坐标 var $CUT_Y = 0; //裁切的縱坐标 var $CUT_WIDTH = 100; //裁切的寬度 var $CUT_HEIGHT = 100; //裁切的高度 * 銳化 var $SHARP = "7.0"; //銳化程度 * 透明度處理 var $ALPHA = '100'; //透明度在0-127之間 var $ALPHA_X = "90"; var $ALPHA_Y = "50"; * 任意角度旋轉 var $CIRCUMROTATE = "90.0"; //注意,必須為浮點數 * 出錯資訊 var $ERROR = array( 'unalviable' => '沒有找到相關圖檔!' ); * 構造函數:函數初始化 function __construct($PICTURE_URL){ $this -> get_info($PICTURE_URL); } function get_info($PICTURE_URL){ /** * 處理原圖檔的資訊,先檢測圖檔是否存在,不存在則給出相應的資訊 */ @$SIZE = getp_w_picpathsize($PICTURE_URL); if(!$SIZE){ exit($this -> ERROR['unalviable']); } // 得到原圖檔的資訊類型、寬度、高度 $this -> PICTURE_MIME = $SIZE['mime']; $this -> PICTURE_WIDTH = $SIZE[0]; $this -> PICTURE_HEIGHT = $SIZE[1]; // 建立圖檔 switch($SIZE[2]){ case 1: $this -> PICTURE_CREATE = p_w_picpathcreatefromgif($PICTURE_URL); $this -> PICTURE_TYPE = "p_w_picpathjpeg"; $this -> PICTURE_EXT = "jpg"; break; case 2: $this -> PICTURE_CREATE = p_w_picpathcreatefromjpeg($PICTURE_URL); $this -> PICTURE_TYPE = "p_w_picpathgif"; $this -> PICTURE_EXT = "gif"; case 3: $this -> PICTURE_CREATE = p_w_picpathcreatefrompng($PICTURE_URL); $this -> PICTURE_TYPE = "p_w_picpathpng"; $this -> PICTURE_EXT = "png"; * 文字顔色轉換16進制轉換成10進制 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進制的顔色轉換成10進制的(R,G,B) function hex2dec(){ preg_match_all("/([0-f]){2,2}/i", $this -> FONT_COLOR, $MATCHES); if(count($MATCHES) == 3){ // 縮放類型 function zoom_type($ZOOM_TYPE){ $this -> ZOOM = $ZOOM_TYPE; // 對圖檔進行縮放,如果不指定高度和寬度就進行縮放 function zoom(){ // 縮放的大小 if($this -> ZOOM == 0){ $this -> ZOOM_WIDTH = $this -> PICTURE_WIDTH * $this -> ZOOM_MULTIPLE; $this -> ZOOM_HEIGHT = $this -> PICTURE_HEIGHT * $this -> ZOOM_MULTIPLE; // 建立一個真彩圖象 $this -> TRUE_COLOR = p_w_picpathcreatetruecolor($this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT); $WHITE = p_w_picpathcolorallocate($this -> TRUE_COLOR, 255, 255, 255); p_w_picpathfilledrectangle($this -> TRUE_COLOR, 0, 0, $this -> ZOOM_WIDTH, $this -> ZOOM_HEIGHT, $WHITE); p_w_picpathcopyresized($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 // 裁切圖檔,按坐标或自動 function cut(){ $this -> TRUE_COLOR = p_w_picpathcreatetruecolor($this -> CUT_WIDTH, $this -> CUT_WIDTH); p_w_picpathcopy($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 = p_w_picpathcreatetruecolor($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); $this -> WORD = mb_convert_encoding($this -> FONT_WORD, 'utf-8', 'gb2312'); * 取得使用 TrueType 字型的文本的範圍 $TEMP = p_w_picpathttfbbox($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]; * 文字水印的預設位置為右下角 if($this -> WORD_X == ""){ $this -> WORD_X = $this -> PICTURE_WIDTH - $WORD_WIDTH; if($this -> WORD_Y == ""){ $this -> WORD_Y = $this -> PICTURE_HEIGHT - $WORD_HEIGHT; p_w_picpathsettile($this -> TRUE_COLOR, $this -> PICTURE_CREATE); p_w_picpathfilledrectangle($this -> TRUE_COLOR, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT, IMG_COLOR_TILED); $TEXT2 = p_w_picpathcolorallocate($this -> TRUE_COLOR, $this -> RED, $this -> GREEN, $this -> Blue); p_w_picpathttftext($this -> TRUE_COLOR, $this -> FONT_SIZE, $this -> ANGLE, $this -> WORD_X, $this -> WORD_Y, $TEXT2, $this -> FONT_PATH, $this -> WORD); * 水印圖檔 function _mark_picture(){ * 擷取水印圖檔的資訊 @$SIZE = getp_w_picpathsize($this -> FORCE_URL); if(!$SIZE){ exit($this -> ERROR['unalviable']); $FORCE_PICTURE_WIDTH = $SIZE[0]; $FORCE_PICTURE_HEIGHT = $SIZE[1]; // 建立水印圖檔 switch($SIZE[2]){ case 1: $FORCE_PICTURE_CREATE = p_w_picpathcreatefromgif($this -> FORCE_URL); $FORCE_PICTURE_TYPE = "gif"; break; case 2: $FORCE_PICTURE_CREATE = p_w_picpathcreatefromjpeg($this -> FORCE_URL); $FORCE_PICTURE_TYPE = "jpg"; case 3: $FORCE_PICTURE_CREATE = p_w_picpathcreatefrompng($this -> FORCE_URL); $FORCE_PICTURE_TYPE = "png"; * 判斷水印圖檔的大小,并生成目标圖檔的大小,如果水印比圖檔大,則生成圖檔大小為水印圖檔的大小。否則生成的圖檔大小為原圖檔大小。 $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; $CREATE_HEIGHT = $this -> PICTURE_HEIGHT; * 建立一個畫布 $NEW_PICTURE_CREATE = p_w_picpathcreatetruecolor($CREATE_WIDTH, $CREATE_HEIGHT); $WHITE = p_w_picpathcolorallocate($NEW_PICTURE_CREATE, 255, 255, 255); * 将背景圖拷貝到畫布中 p_w_picpathcopy($NEW_PICTURE_CREATE, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); * 将目标圖檔拷貝到背景圖檔上 p_w_picpathcopy($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 = p_w_picpathcreatetruecolor($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 = p_w_picpathcolorat($this -> PICTURE_CREATE, $i, $j); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $now_color = p_w_picpathcolorallocate($this -> PICTURE_CREATE, $r, $g, $b); if ($now_color == $tran_color) { continue; else { $color = p_w_picpathcolorallocatealpha($this -> PICTURE_CREATE, $r, $g, $b, $ALPHA); p_w_picpathsetpixel($this -> PICTURE_CREATE, $ALPHA_X + $i, $ALPHA_Y + $j, $color); $this -> TRUE_COLOR = $this -> PICTURE_CREATE; * 圖檔旋轉: * 沿y軸旋轉 function turn_y(){ for ($x = 0; $x < $this -> PICTURE_WIDTH; $x++) { p_w_picpathcopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, $this -> PICTURE_WIDTH - $x - 1, 0, $x, 0, 1, $this -> PICTURE_HEIGHT); * 沿X軸旋轉 function turn_x(){ for ($y = 0; $y < $this -> PICTURE_HEIGHT; $y++) p_w_picpathcopy($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, $this -> PICTURE_HEIGHT - $y - 1, 0, $y, $this -> PICTURE_WIDTH, 1); * 任意角度旋轉 function turn(){ p_w_picpathCopyResized($this -> TRUE_COLOR, $this -> PICTURE_CREATE, 0, 0, 0, 0, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT, $this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); $WHITE = p_w_picpathcolorallocate($this -> TRUE_COLOR, 255, 255, 255); $this -> TRUE_COLOR = p_w_picpathrotate ($this -> TRUE_COLOR, $this -> CIRCUMROTATE, $WHITE); * 圖檔銳化 function sharp(){ $cnt = 0; for ($x = 0; $x < $this -> PICTURE_WIDTH; $x++){ for ($y = 0; $y < $this -> PICTURE_HEIGHT; $y++) $src_clr1 = p_w_picpathcolorsforindex($this -> TRUE_COLOR, p_w_picpathcolorat($this -> PICTURE_CREATE, $x-1, $y-1)); $src_clr2 = p_w_picpathcolorsforindex($this -> TRUE_COLOR, p_w_picpathcolorat($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 = p_w_picpathcolorexact($this -> PICTURE_CREATE, $r, $g, $b)) == -1) $DST_CLR = p_w_picpathcolorallocate($this -> PICTURE_CREATE, $r, $g, $b); $cnt++; if ($DST_CLR == -1) die("color allocate faile at $x, $y ($cnt)."); p_w_picpathsetpixel($this -> TRUE_COLOR, $x, $y, $DST_CLR); * 将圖檔反色處理?? function return_color(){ $NEW_PICTURE_CREATE = p_w_picpathcreate($this -> PICTURE_WIDTH, $this -> PICTURE_HEIGHT); * 生成目标圖檔并顯示 function show(){ // 判斷浏覽器,若是IE就不發送頭 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); |