ホームページ  >  記事  >  バックエンド開発  >  テキスト透かし/画像透かしの追加、圧縮、およびせん断のための PHP カプセル化クラス

テキスト透かし/画像透かしの追加、圧縮、およびせん断のための PHP カプセル化クラス

WBOY
WBOYオリジナル
2016-06-20 13:03:30862ブラウズ

この記事の PHP 画像操作カプセル化クラスの 4 つのメソッドは、テキスト ウォーターマーク (imagettftext())、画像ウォーターマーク (imagecopymerge())、画像圧縮、画像切り取り (imagecopyresampled()) です。残りの一般的なメソッドは次のとおりです。使用される GD 関数は繰り返しではありません。

コードに直接移動します:

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


声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。