php圖片水印添加,壓縮,剪切類別的封裝

WBOY
發布: 2016-07-30 13:31:23
原創
912 人瀏覽過

  php對圖片檔案的操作主要是利用GD庫擴充。當我們經常利用php對圖片進行操作時,會自然封裝很多函數,否則會寫太多重複的程式碼。當有很多對圖片的相關函數的時候,我們可以考慮將這些函數也整理一下,因而有了封裝成類別的想法。

  操作圖片主要歷經四個步驟:

  1. 打開圖片
  2. 操作圖片
  3. 輸出圖片
  4. 銷毀圖片
🀜 㟎1,34三個步驟都差不多。真正需要變通的只有操作圖片的這一步驟了。操作圖片又往往透過1或多個主要的GD函數來完成。

  本文封裝類別裡面的四種方法,文字浮水印(imagettftext()),圖片水印(imagecopymerge()),圖片壓縮,圖片剪切(imagecopyresampled()),其餘的常用GD函數便不賘述。直接上碼:

<?<span>php 

</span><span>class</span><span> Image
{    
    </span><span>private</span><span> $info;

    </span><span>private</span><span> $image;
    </span><span>public</span><span> $type;
    </span><span>public</span><span> function __construct($src)
    {

        $</span><span>this</span>->info=<span>getimagesize($src);
        $</span><span>this</span>->type=image_type_to_extension($<span>this</span>->info[<span>'</span><span>2</span><span>'</span>],<span>false</span><span>);
        $fun</span>=<span>"</span><span>imagecreatefrom{$this->type}</span><span>"</span><span>;
        $</span><span>this</span>->image=<span>$fun($src);
    }
    </span><span>/*</span><span>*
     * 文字水印
     * @param  [type]  $font     字体
     * @param  [type]  $content  内容
     * @param  [type]  $size     文字大小
     * @param  [type]  $col      文字颜色(四元数组)
     * @param  array   $location 位置 
     * @param  integer $angle    倾斜角度
     * @return [type]           
     </span><span>*/</span><span>public</span> function fontMark($font,$content,$size,$col,$location,$angle=<span>0</span><span>){
        $col</span>=imagecolorallocatealpha($<span>this</span>->image, $col[<span>'</span><span>0</span><span>'</span>], $col[<span>'</span><span>1</span><span>'</span>], $col[<span>'</span><span>2</span><span>'</span>],$col[<span>'</span><span>3</span><span>'</span><span>]);

        imagettftext($</span><span>this</span>->image, $size, $angle, $location[<span>'</span><span>0</span><span>'</span>], $location[<span>'</span><span>1</span><span>'</span><span>], $col,$font,$content);
    }
    
    </span><span>/*</span><span>*
     * 图片水印
     * @param  [type] $imageMark 水印图片地址
     * @param  [type] $dst       水印图片在原图片中的位置
     * @param  [type] $pct       透明度
     * @return [type]            
     </span><span>*/</span><span>public</span><span> function imageMark($imageMark,$dst,$pct){
        $info2</span>=<span>getimagesize($imageMark);
        $type</span>=image_type_to_extension($info2[<span>'</span><span>2</span><span>'</span>],<span>false</span><span>);
        $func2</span>=<span>"</span><span>imagecreatefrom</span><span>"</span><span>.$type;
        $water</span>=<span>$func2($imageMark);

        imagecopymerge($</span><span>this</span>->image, $water, $dst[<span>0</span>], $dst[<span>1</span>], <span>0</span>, <span>0</span>, $info2[<span>'</span><span>0</span><span>'</span>], $info2[<span>'</span><span>1</span><span>'</span><span>], $pct);
        imagedestroy($water);

    }
    </span><span>/*</span><span>*
     * 压缩图片
     * @param  [type] $thumbSize 压缩图片大小
     * @return [type]            [description]
     </span><span>*/</span><span>public</span><span> function thumb($thumbSize){
        $imageThumb</span>=imagecreatetruecolor($thumbSize[<span>0</span>], $thumbSize[<span>1</span><span>]);
        
        imagecopyresampled($imageThumb, $</span><span>this</span>->image, <span>0</span>, <span>0</span>, <span>0</span>, <span>0</span>, $thumbSize[<span>0</span>], $thumbSize[<span>1</span>], $<span>this</span>->info[<span>'</span><span>0</span><span>'</span>], $<span>this</span>->info[<span>'</span><span>1</span><span>'</span><span>]);
        imagedestroy($</span><span>this</span>-><span>image);
        $</span><span>this</span>->image=<span>$imageThumb;
    }
    </span><span>/*</span><span>*
    * 裁剪图片
     * @param  [type] $cutSize  裁剪大小
     * @param  [type] $location 裁剪位置
     * @return [type]           [description]
     </span><span>*/</span><span>public</span><span> function cut($cutSize,$location){
         $imageCut</span>=imagecreatetruecolor($cutSize[<span>0</span>],$cutSize[<span>1</span><span>]);

         imagecopyresampled($imageCut, $</span><span>this</span>->image, <span>0</span>, <span>0</span>, $location[<span>0</span>], $location[<span>1</span>],$cutSize[<span>0</span>],$cutSize[<span>1</span>],$cutSize[<span>0</span>],$cutSize[<span>1</span><span>]);
         imagedestroy($</span><span>this</span>-><span>image);
         $</span><span>this</span>->image=<span>$imageCut;
     }
    </span><span>/*</span><span>*
     * 展现图片
     * @return [type] [description]
     </span><span>*/</span><span>public</span><span> function show(){
        header(</span><span>"</span><span>content-type:</span><span>"</span>.$<span>this</span>->info[<span>'</span><span>mime</span><span>'</span><span>]);

        $funn</span>=<span>"</span><span>image</span><span>"</span>.$<span>this</span>-><span>type;

        $funn($</span><span>this</span>-><span>image);
    }
    </span><span>/*</span><span>*
     * 保存图片
 * @param  [type] $newname 新图片名
 * @return [type]          [description]
 </span><span>*/</span><span>public</span><span> function save($newname){
         header(</span><span>"</span><span>content-type:</span><span>"</span>.$<span>this</span>->info[<span>'</span><span>mime</span><span>'</span><span>]);

         $funn</span>=<span>"</span><span>image</span><span>"</span>.$<span>this</span>-><span>type;

         $funn($</span><span>this</span>->image,$newname.<span>'</span><span>.</span><span>'</span>.$<span>this</span>-><span>type);
     }
     </span><span>public</span><span> function __destruct(){
         imagedestroy($</span><span>this</span>-><span>image);
     }

 }

 </span>?>
登入後複製

  如果還需要其他操作,只需要再往這個類別裡面添加就好啦~~

以上就介紹了php圖片水印添加,壓縮,剪切類別的封裝,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板