Home > Backend Development > PHP Tutorial > php添加文字水印/图片水印,压缩,剪切的封装类

php添加文字水印/图片水印,压缩,剪切的封装类

WBOY
Release: 2016-06-20 13:03:30
Original
926 people have browsed it

本文PHP图片操作封装类里面的四种方法,文字水印(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 /> ?>
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template