php图片处理

图片处理的四个步骤

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值