圖片操作類(文字水印,圖片水印,非等比縮放),還有哪些操作?準備將其他的操作補齊作為工具類
<?php /********************* *????pictureDeal.class.php *????圖片處理類 * *********************/ class?PictureDeal{ ????private?$imagePathName; ????public?function?__construct($filepath){ ????????$this->init($filepath); ????} ????public?function?init($filepath){ ????????if(is_string($filepath)?&&?file_exists($filepath)?&&?(getimagesize($filepath)!==false)){ ????????????$this->imagePathName=$filepath; ????????} ????????else{ ????????????//return?false; ????????????exit("不是圖片或文件不存在!"); ????????} ????} ???? ????/* ????*????@function?給圖片添加文字水印 ????*????@param?string?$savePathName?保存路徑及文件名(不含后綴名),?string?$content?文字內容,?int?$fontSize?字體大小,?string?$font?字體路徑,?array?$fontColor?RGB對應的int值0-255,?int?$transparency?透明度,?int?$x?相距原圖0,0點的橫向距離,?int?$y?相距原圖0,0點的縱向距離 ????*/ ????public?function?addTextWaterMark($savePathName,$content,$fontSize,$font,$fontColor,$transparency,$x,$y){ ????????$imageInfo=$this->getImageInfo($this->imagePathName); ????????$imageExt=$this->getImageExt($imageInfo); ????????$this->operateTextCarbon($savePathName,$imageExt,$content,$fontSize,$font,$fontColor,$transparency,$x,$y);//圖片打開,操作,輸出,銷毀 ????} ????/* ????*????@function?給圖片添加圖片水印 ????*????@param?string?$savePathName?保持路徑及文件名(不含后綴名),?string?$waterImagePath?水印圖片的全路徑,?int?$transparency?透明度,?int?$x?相距原圖0,0點的橫向距離,?int?$y?相距原圖0,0點的縱向距離 ????*/ ????public?function?addPicWaterMark($savePathName,$waterImagePath,$transparency,$x,$y){ ????????$imageInfo=$this->getImageInfo($this->imagePathName); ????????$imageExt=$this->getImageExt($imageInfo); ????????$waterImageInfo=getimagesize($waterImagePath); ????????$waterImageExt=$this->getImageExt($waterImageInfo); ????????$this->operateImageCarbon($savePathName,$imageExt,$imageInfo,$waterImagePath,$waterImageExt,$waterImageInfo,$transparency,$x,$y); ????} ????/* ????*????@function?壓縮圖片(非等比縮放) ????*????@param?string?$savePathName?保存路徑及文件名(不含后綴名),?int?$width?縮放后的寬度,?int?$height?縮放后的高度 ????*/ ????public?function?thumbImage($savePathName,$width,$height){ ????????$imageInfo=$this->getImageInfo($this->imagePathName); ????????$imageExt=$this->getImageExt($imageInfo); ????????$this->operateThumbImage($savePathName,$imageExt,$imageInfo,$width,$height); ????} ????/* ????*????@function?獲取圖片信息 ????*????@param ????*????@return?array ????*/ ????public?function?getImageInfo($file){ ????????$info=getimagesize($file); ????????return?$info; ????} ???? ????/* ????*????@function?獲得圖像后綴名 ????*????@param?array?$imageInfo ????*????@return?string ????*/ ????public?function?getImageExt($imageInfo){ ????????return?image_type_to_extension($imageInfo[2],false); ????} ???? ????/* ????*????@function?對圖片添加文字水印操作并生成新圖片 ????*????@param?string?$imageExt?原圖片的后綴名 ????*????@notice?包含打開,操作,輸出,銷毀圖片的操作 ????*/ ????public?function?operateTextCarbon($savePathName,$imageExt,$content,$fontSize,$font,$fontColor,$transparency,$x,$y){ ????????$tmp_handler=$this->getImageResource($imageExt,$this->imagePathName);//打開資源 ????????$color=imagecolorallocatealpha($tmp_handler,$fontColor["R"],$fontColor["G"],$fontColor["B"],$transparency); ????????imagettftext($tmp_handler,?$fontSize,?0,?$x,?$y,?$color,?$font,?$content);//操作圖片 ????????$this->createImageByResource($imageExt,$tmp_handler,$savePathName);//輸出圖片 ????????imagedestroy($tmp_handler);//銷毀圖片 ????} ???? ????/* ????*????@function?對圖片添加圖片水印操作并生成新圖片 ????*????@param?string?$imageExt?原圖片的后綴名,?array?$imageInfo?原圖片信息,?string?$waterImagePath?水印圖片全路徑? ????*????@notice?包含打開,操作,輸出,銷毀圖片的操作 ????*/ ????public?function?operateImageCarbon($savePathName,$imageExt,$imageInfo,$waterImagePath,$waterImageExt,$waterImageInfo,$transparency,$x,$y){ ????????$this->copyResourceImage($imageExt,$imageInfo,$savePathName);//復制圖片 ????????$carbon_filename=$savePathName.".".$imageExt;//獲得復制后的圖片全路徑名 ????????$tmp_carbon_handler=$this->getImageResource($imageExt,$carbon_filename);//打開副本圖片資源 ????????$tmp_water_handler=$this->getImageResource($waterImageExt,$waterImagePath);//打開水印圖片資源 ????????//操作圖片 ????????imagecopymerge($tmp_carbon_handler,?$tmp_water_handler,?$x,?$y,?0,?0,?$waterImageInfo[0],?$waterImageInfo[1],?$transparency); ????????//header("content-type:image/jpeg"); ????????//imagejpeg($tmp_carbon_handler); ????????$this->createImageByResource($imageExt,$tmp_carbon_handler,$savePathName); ????????imagedestroy($tmp_water_handler);//銷毀圖片 ????????imagedestroy($tmp_carbon_handler);//銷毀圖片 ????} ???? ????/* ????*????@function?復制原圖,以副本進行圖像操作 ????*????@param?string?$imageExt?原圖后綴名,?array?$imageInfo?原圖信息,?string?$savePathName?保存路徑及文件名(不含后綴名) ????*/ ????public?function?copyResourceImage($imageExt,$imageInfo,$savePathName){ ????????$tmp_handler=$this->getImageResource($imageExt,$this->imagePathName); ????????$tmp_carbon_handler=imagecreatetruecolor($imageInfo[0],$imageInfo[1]); ????????imagecopy($tmp_carbon_handler,$tmp_handler,0,0,0,0,$imageInfo[0],$imageInfo[1]); ????????$this->createImageByResource($imageExt,$tmp_carbon_handler,$savePathName); ????????imagedestroy($tmp_handler); ????????imagedestroy($tmp_carbon_handler); ????} ????public?function?operateThumbImage($savePathName,$imageExt,$imageInfo,$width,$height){???????? ????????$tmp_dst_handler=imagecreatetruecolor($width,$height); ????????$tmp_handler=$this->getImageResource($imageExt,$this->imagePathName); ????????imagecopyresampled?($tmp_dst_handler,?$tmp_handler,?0,?0,?0,?0,?$width,?$height,?$imageInfo[0],?$imageInfo[1]); ????????$this->createImageByResource($imageExt,$tmp_dst_handler,$savePathName); ????????imagedestroy($tmp_handler); ????????imagedestroy($tmp_dst_handler); ????} ???? ????/* ????*????@function?根據后綴名以不同的形式獲取資源句柄 ????*????@param?string?$imageExt?原圖后綴名,?string?$file?原圖全路徑 ????*????@return?resource ????*/ ????public?function?getImageResource($imageExt,$file){ ????????$handler=null; ????????if($imageExt=="jpeg"){ ????????????$handler=imagecreatefromjpeg($file); ????????} ????????if($imageExt=="png"){ ????????????$handler=imagecreatefrompng($file); ????????} ????????if($imageExt=="gif"){ ????????????$handler=imagecreatefromgif($file); ????????} ????????return?$handler; ????} ???? ????/* ????*????@function?根據后綴名生成格式不同的圖片 ????*????@param?string?$imageExt?原圖后綴名,?resource?$handler?圖片資源句柄,?string?$savePathName?保存路徑及文件名(不含后綴名) ????*/ ????public?function?createImageByResource($imageExt,$handler,$savePathName){ ????????if($imageExt=="jpeg"){ ????????????imagejpeg($handler,$savePathName.".".$imageExt); ????????} ????????if($imageExt=="png"){ ????????????imagepng($handler,$savePathName.".".$imageExt); ????????} ????????if($imageExt=="gif"){ ????????????imagegif($handler,$savePathName.".".$imageExt); ????????} ????} ???? } /* //文字水印的測試 $pathname="./resource/Nico_cool_r342.jpg"; $picDeal=new?PictureDeal($pathname); $savePathName="./resource/new_image_text"; $content="にっこにっこにーー!"; $fontSize=20; $font="./resource/msyh.ttc"; $fontColor=array("R"=>0,"G"=>0,"B"=>0); $transparency=50; $x=55; $y=20; $picDeal->addTextWaterMark($savePathName,$content,$fontSize,$font,$fontColor,$transparency,$x,$y); */ /* //圖片水印的測試 $pathname="./resource/Nico_cool_r342.jpg"; $picDeal=new?PictureDeal($pathname); $savePathName="./resource/new_image_img"; $waterImagePath="./resource/getimage_2.jpg"; $transparency=50; $x=10; $y=20; $picDeal->addPicWaterMark($savePathName,$waterImagePath,$transparency,$x,$y); */ //非等比縮放的測試 $pathname="./resource/Nico_cool_r342.jpg"; $picDeal=new?PictureDeal($pathname); $savePathName="./resource/new_image_thumb"; $width=60; $height=80; $picDeal->thumbImage($savePathName,$width,$height); ?>
本人菜鳥一個,各位大大們看看,有寫的不好的地方請指出,個人不喜歡動態調用方法,所以直接就用多重if了。這幾天在趕項目等空下來,準備將圖片的其余操作給補全了,作為將來可以拿來作為工具類。
2015-02-26
再有個旋轉和裁剪基本上可以滿足大部分需求了
2015-02-28
あぃがとうございます 謝謝。