php圖片浮水印添加,壓縮,剪下類的封裝

來源:互聯網
上載者:User
  php對圖片檔案的操作主要是利用GD庫擴充。當我們頻繁利用php對圖片進行操作時,會自然封裝很多函數,否則會寫太多重複的代碼。當有很多對圖片的相關函數的時候,我們可以考慮將這些函數也整理一下,因而就有了封裝成類的想法。

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

  1. 開啟圖片
  2. 操作圖片
  3. 輸出圖片
  4. 銷毀圖片

  1,3,4三個步驟每次都要寫,每次又都差不多。真正需要變通的只有操作圖片的這一步驟了。操作圖片又往往通過1或多個主要的GD函數來完成。

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

php class Image{        private $info;    private $image;    public $type;    public function __construct($src)    {        $this->info=getimagesize($src);        $this->type=image_type_to_extension($this->info['2'],false);        $fun="imagecreatefrom{$this->type}";        $this->image=$fun($src);    }    /**     * 文字浮水印     * @param  [type]  $font     字型     * @param  [type]  $content  內容     * @param  [type]  $size     文字大小     * @param  [type]  $col      文字顏色(四元數組)     * @param  array   $location 位置      * @param  integer $angle    傾斜角度     * @return [type]                */public function fontMark($font,$content,$size,$col,$location,$angle=0){        $col=imagecolorallocatealpha($this->image, $col['0'], $col['1'], $col['2'],$col['3']);        imagettftext($this->image, $size, $angle, $location['0'], $location['1'], $col,$font,$content);    }        /**     * 圖片浮水印     * @param  [type] $imageMark 浮水印圖片地址     * @param  [type] $dst       浮水印圖片在原圖片中的位置     * @param  [type] $pct       透明度     * @return [type]                 */public function imageMark($imageMark,$dst,$pct){        $info2=getimagesize($imageMark);        $type=image_type_to_extension($info2['2'],false);        $func2="imagecreatefrom".$type;        $water=$func2($imageMark);        imagecopymerge($this->image, $water, $dst[0], $dst[1], 0, 0, $info2['0'], $info2['1'], $pct);        imagedestroy($water);    }    /**     * 壓縮圖片     * @param  [type] $thumbSize 壓縮圖片大小     * @return [type]            [description]     */public function thumb($thumbSize){        $imageThumb=imagecreatetruecolor($thumbSize[0], $thumbSize[1]);                imagecopyresampled($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $this->info['0'], $this->info['1']);        imagedestroy($this->image);        $this->image=$imageThumb;    }    /**    * 裁剪圖片     * @param  [type] $cutSize  裁剪大小     * @param  [type] $location 裁剪位置     * @return [type]           [description]     */public function cut($cutSize,$location){         $imageCut=imagecreatetruecolor($cutSize[0],$cutSize[1]);         imagecopyresampled($imageCut, $this->image, 0, 0, $location[0], $location[1],$cutSize[0],$cutSize[1],$cutSize[0],$cutSize[1]);         imagedestroy($this->image);         $this->image=$imageCut;     }    /**     * 展現圖片     * @return [type] [description]     */public function show(){        header("content-type:".$this->info['mime']);        $funn="image".$this->type;        $funn($this->image);    }    /**     * 儲存圖片 * @param  [type] $newname 新圖片名 * @return [type]          [description] */public function save($newname){         header("content-type:".$this->info['mime']);         $funn="image".$this->type;         $funn($this->image,$newname.'.'.$this->type);     }     public function __destruct(){         imagedestroy($this->image);     } } ?>

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

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

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.