php图片处理

来源:互联网 发布:betterment mac 编辑:程序博客网 时间:2024/05/21 20:28

图片处理的四个步骤

1.打开图片2.操作图片3.输出图片4.销毁图片

<?php/** * Created by PhpStorm. * User: leyan * Date: 2016/6/10 * Time: 19:42 */require_once('image_interface.php');class image implements image_face{    private $info = array();//图片详细信息    var $type = '';//图片类型    private $image = '';//内存中的图片    var $font = 'images/msyh.ttf';//水印字体    var $image_mark = "images/logo.jpeg";//水印图片    var $image_thumb = '';    var $thumb = false;//销毁缩略图    function __construct($src)    {        $version = $this->check_gd();        if($version == 0){            exit('gd版本不存在');        }        /*1.打开图片*/        //2.获取图片信息        $this->info = getimagesize($src);        //3.通过图像的编号来获取图像的类型        $this->type = image_type_to_extension($this->info[2],false);//jpeg        //4.在内存中创建一个和我们图像类型一样的图像        $fun = "imagecreatefrom{$this->type}";        //imagecreatefromjpeg($src);imagecreatefrompng($src);        $this->image = $fun($src);    }    //文字水印    function add_watermark_character($content){        /*二.操作图片*/        //1.设置字体库的路径        //2.填写水印的内容        //3.设置字体的颜色和透明度        $col = imagecolorallocatealpha($this->image,255,255,255,50);        //4.写入文字        imagettftext($this->image,30,0,20,70,$col,$this->font,$content);    }    /**图片水印     * @param $image_mark     */    function add_watermark_pic($image_mark='')    {        /*二.操作图片*/        if(!empty($image_mark)){            $this->image_mark = $image_mark;        }        $info2 = getimagesize($this->image_mark);        $type2 = image_type_to_extension($info2[2],false);        $func2 = "imagecreatefrom{$type2}";        $water = $func2($this->image_mark);        //合并图片        imagecopymerge($this->image,$water,20,70,0,0,$info2[0],$info2[1],100);    }    //缩略图    function add_watermark_thumb($width=300,$height=200){        $this->image_thumb = imagecreatetruecolor($width,$height);        imagecopyresampled($this->image_thumb,$this->image,0,0,0,0,$width,$height,$this->info[0],$this->info[1]);        //销毁原始图片        imagedestroy($this->image);        $this->thumb = true;    }    //检测gd库的版本    function check_gd(){        if(!extension_loaded('gd')){            exit('gb库未加载');        }        if(PHP_VERSION>4.3){            if(function_exists('gd_info')){                $ver_info = gd_info();                preg_match('/\d/',$ver_info['GD Version'],$match);                return $match[0];            }        }    }    //保存以及销毁图片    public function __destruct()    {        // TODO: Implement __destruct() method.        /*三.输出图片*/        //1.浏览器输出(显示图片)        header('Content-type:'.$this->info['mime']);        $func = "image{$this->type}";        if($this->thumb){            //保存和销毁的图片是缩略图            $this->image = $this->image_thumb;        }        $func($this->image);        //2.保存图片到硬盘        $func($this->image,"images/newimage.{$this->type}");        /*四.销毁图片*/        imagedestroy($this->image);    }}#测试用例$img = new image('images/pic.jpg');$img -> add_watermark_pic();
0 0
原创粉丝点击