圖片處理函數功能:縮放、剪切、相框、浮水印、銳利化、旋轉、翻轉、透明度、反色,處理並保存歷史記錄的想法:當有圖片有改動時自動產生一張新圖片,命名方式可以考慮在原始圖片的基礎上加上步驟,例如:圖片名稱__第幾步。 在一些需要圖片進階處理功能的網路應用程式中,可以參考這個類別。
-
/*
- 圖片處理函數功能:縮放、剪切、相框、水印、銳利化、旋轉、翻轉、透明度、反色
- 處理並保存歷史記錄的想法:當有圖片有改動時自動產生一張新圖片,命名方式可以考慮在原圖片的基礎上加上步驟,例如:圖片名稱__第幾步
- */
- 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_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 $ZOOM_HEIGHT;//縮放高度
-
- /*切,按比例和固定長度、寬度
- */
- var $CUT_TYPE=1;//裁切類型
- var $CUT_X=0;//裁切的橫座標
- var $CUT_Y =0;//裁切的縱座標
- var $CUT_;//裁切的寬度
- 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=getimagesize($PICTURE_URL);
- if(!$SIZE)
- {
- exit($this ->ERROR['unalviable']);
- }
-
- //得到原圖片的資訊類型、寬度、高度
- $this->PICTURE_MIME=$SIZE['mime'];
- $this->PICTURE_;
- $this->PICTURE_HEIGHT=$SIZE[1];
-
- //建立圖片
- switch($SIZE[2])
- {
- case 1:
- $this->PICTURE_CREATE=imagecreatefromgif($PICTURE_URL);
- $this->PICTURE_TYPE="imagejpeg";
- $this->PICTURE_EXT="jpg";
- case 2:
- $this->PICTURE_CREATE=imagecreatefromjpeg($PICTURE_URL);
- $this->PICTURE_TYPE="imagegif";
- $this->PICTURE_EXT=">> break;
- case 3:
- $this->PICTURE_CREATE=imagecreatefrompng($PICTURE_URL);
- $this->PICTURE_TYPE="imagepng";
- $this->PICTURE_EXT="png"; 🎜> break;
- }
-
- /*
- 文字顏色轉換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)
- {
- $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;
- }
-
- //對圖片進行縮放,如果不指定高度和寬度就進行縮放
- function zoom()
- {
- //縮放的大小
- if($this->ZOOM==0)
- {
- $this->ZOOM_;gt;PICTURE_WIDTH * $this->ZOOM_MULTIPLE;
- $this->ZOOM_HEIGHT=$this->PICTURE_HEIGHT * $this->ZOOM_MULTIPLE;
- }
- }彩色圖象
- $this->TRUE_COLOR=imagecreatetruecolor($this->ZOOM_WIDTH,$this->ZOOM_HEIGHT);
- $WHITE=imagecolorallocate($this->TRUE_COLOR,255,255,25555); this->TRUE_COLOR,0,0,$this->ZOOM_WIDTH,$this->ZOOM_HEIGHT,$WHITE);
- imagecopyresized($this->TRUE_COLOR,$this->PICTURE_CREATE,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, $this->ZOOM_WIDTH,$this->ZOOM_HEIGHT,$this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
- }
-
- #end of zoom
- //裁切圖片,按座標座標或自動
- function cut()
- {
- $this->TRUE_COLOR=imagecreatetruecolor($this->CUT_WIDTH,$this->CUT_WIDTH);
- imagecopy($this-this>TRthis_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=imagecreatecolor($this-this. $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];
- /*
- 文字浮水印的預設位置為右下角
- */
- 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); $TE ,$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_;
- $FORCE_PICTURE_HEIGHT=$SIZE[1];
- $FORCE_PICTURE_HEIGHT=$SIZE[1];
- //建立水印圖片
- switch($SIZE[2])
- {
- case 1:
- $FORCE_PICTURE_CREATE=imagecreatefromgif($this->FORCE_URL);
- $FORCE_PICTURE_TYPEbreak="> case 2:
- $FORCE_PICTURE_CREATE=imagecreatefromjpeg($this->FORCE_URL);
- $FORCE_PICTURE_TYPE="jpg";
- break>
- Cate; this->FORCE_URL);
- $FORCE_PICTURE_TYPE="png";
- break;
- }
- /*
- 判斷水印圖片的大小,並產生目標圖片的大小,如果水印比圖片大,則產生圖片大小為浮水印圖片的大小。否則產生的圖片大小為原始圖片大小。
- */
- $this->NEW_PICTURE=$this->PICTURE_CREATE;
- if($FORCE_PICTURE_WIDTH>$this->PICTURE_WIDTH)
- { $ $S; $S; > }
- else
- {
- $CREATE_;gt;PICTURE_WIDTH;
- }
-
- if($FORCE_PICTURE_HEIGHT>$this->PICTURE_HEATE)FORCE_START_Y;
- }
- else
- {
- $CREATE_HEIGHT=$this->PICTURE_HEIGHT;
- }
- /
- 🎜> */
- $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);
-
- /* */
- imagecopy($NEW_PICTURE_CREATE, $FORCE_PICTURE_CREATE, $this->FORCE_X, $this->FORCE_Y, $this-> ;FORCE_START_X, $this-A_X, $ $FORCE_PICTURE_HEIGHT);
- $this->TRUE_COLOR=$NEW_PICTURE_CREATE;
- }
- #結束標記
-
- 函數 $thispha_( )PICTURE_WIDTH,$this->PICTURE_HEIGHT);
- $rgb="#CDCCDCD";
- $tran_color=" #000000";
- for($j=0;$jPICTURE_HEIGHT-1;$j )
- {
- for ($i=0;$iPICTURE_WIDTH-1;$i )
- {
- $rgb = imagecolorat($this->PICTURE_CREATE,$i,$j);
- $r = ($rgb >>> 16) & 0xFF;
- $g = ($rgb >> 8) & 0xFF;
- $b = $rgb & 0xFF;
- $now_color=imagecolorallocate($this->PICTURE_CREATE,$ r,$g,$b);
- if ($now_color==$tran_color)
- {
- 繼續;
- }
- else
- {
- $color=imagecolorallocatealpha( $this->PICTURE__,$r,$g,$b,$ALPHA);
- imagethispixel($ imagethis ->PICTURE_CREATE,$ALPHA_X $i,$ALPHA_Y $j,$color);
- }
- $this->TRUE_COLOR=$this->PICTURE_CREATE;
- }
- }
- }
-
- /*
- 圖片旋轉:
- 沿著y軸旋轉
- */
- 函數turn_y()
- {
- $this->TRUE_COLOR=imagecreatetruecolor($this - >PICTURE_WIDTH,$this->PICTURE_HEIGHT);
- for ($x = 0; $x $x )
- {
- imagecopy($this->TRUE_COLOR, $this->PICTURE_CREATE, $this->PICTURE_WIDTH - $x - 1, 0, $x, 0, 1 , $this->PICTURE_HEIGHT);
- }
- }
- /*
- 沿X軸旋轉
- */
- functionturn_x( )
- {
- $ $-this> TRUE_COLOR=imagecreatetruecolor($this->PICTURE_WIDTH,$this->PICTURE_HEIGHT);
- for ($y = 0; $y PICTURE_HEIGHT; $y )
- {
- thisimagecopy($ ->TRUE_COLOR, $this->PICTURE_CREATE, 0, $this->PICTURE_HEIGHT - $y - 1, 0, $y, $this-> PICTURE_WIDTH, 1);
- }
- }
-
- /*
- 任意角度旋轉
- */
- functionturn()
- {
- $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_HEIGHTtruecolor($this->PICTURE_WIDTH,$this-> ;PICTURE_HEIGHT );
- $cnt=0;
- for ($x=0; $xPICTURE_WIDTH; $x )
- {
- for ($y=0; $ yPICTURE_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_cl2 ["red"]-$src_clr1["red"]));
- $g = intval($src_clr2["green"] $this->SHARP*($src_clr2 ["綠色"]-$src_clr1["綠色"]));
- $b = intval($src_clr2["藍色"] $this->SHARP*($src_clr2["藍色"]-$src_clr1["藍色"]));
- $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("顏色分配失敗於$x, $y ($cnt).");
- imagesetpixel($this->TRUE_COLOR, $x, $y, $DST_CLR);
- }
- }
- }
-
- /*
- 圖片反色處理?
- */
- function return_color()
- {
- /*
- 建立圖片
- */
- $NEW_PICTURE_CREATE= imagecreate($this->PICTURE_WIDTH,,$ PICTURE_HEIGHT);
- $WHITE=imagecolorallocate($NEW_PICTURE_CREATE,255,255,255);
- /*
- 將背景圖複製到修復中PICTURE_WIDTH,$this->PICTURE_HEIGHT);
- $this->TRUE_COLOR=$ NEW_PICTURE_CREATE;
- }
-
- / >🎜>目標圖片並顯示
- */
- function show()
- {
- // 判斷瀏覽器,如果IE不傳送頭
- if(isset($_SERVER['HTTP_USER_AGENT') ]) )
- {
- $ua = strtoupper($_SERVER['HTTP_USER_AGENT']);
- if(!preg_match('/ ^.*MSIE.*)$/i',$ua))
- {
- header("內容類型:$this->PICTURE_MIME");
- }
- }
- $OUT=$this->PICTURE_TYPE;
- $OUT($this ->TRUE_COLOR );
- }
-
- /*
- 產生目標圖片並儲存
- */
- function save_picture()
- {
- //以JPEG格式將圖片輸出到瀏覽器或檔案
- $OUT=$this->PICTURE_TYPE;
- if(function_exists($OUT))
- {
- // 判斷瀏覽器,如果IE不傳送頭
- if( isset($_SERVER['HTTP_USER_AGENT']))
- {
- $ua = strtoupper($_SERVER[ 'HTTP_USER_AGENT']);
- if(!preg_match('/^.*MSIE. *)$ /i',$ua))
- {
- header("內容類型:$this-> ;PICTURE_MIME");
- }
- }
- if(!$this- >TRUE_COLOR)
- {
- exit($this->ERROR['不可用']);
- }
- else
- {
- $OUT($this->TRUE_COLOR,$this -> DEST_URL);
- $OUT($this->TRUE_COLOR);
- }
- }
- }
- /*
- 解析建構子:釋放圖片
- */
- function __destruct()
- {
- /*釋放圖片*/
- imagedestroy ($this->TRUE_COLOR);
- imagedestroy($this->PICTURE_CREATE);
- imagedestroy($this->PICTURE_CREATE);
- }
- #🎜>課程課程課程結束
- }
- ?>
複製程式碼
|
多功能、圖片處理、php