PHP常用工具类之图片处理类

网站图片上传过程中,服务器端需要对上传图片做一些处理,如创建缩略图,给图片加水印等常规操作,于是需要一个图片工具类来处理这些操作。

关于图片工具类主要有两个功能:

1.创建图片缩略图

2.给图片加水印

class ImageTool{

    //获取图片相关信息
    protected static function imageInfo($image){
        //判断图片是否存在
        if(!file_exists($image) || !is_file($image)){
            return false;
        }

        $info = getimagesize($image); 
        if($info == false){
            return false;

        }

        //分析$info数组,重新组合
        $img['width'] = $info[0];
        $img['height'] = $info[1];
        $img['ext'] = substr($info['mime'],strpos($info['mime'],'/')+1);
        return $img;
    }

    /**
     * 创建略缩图函数
     * parm $dst string 待操作图
     * parm $save string 文件保存位置及文件名,不填默认,保存在原位置并且替换原始图
     * parm $width int 缩略后的宽度
     * parm $height int 缩略后的高度度
     * 
     * 
     */
    public static function thumb($dst,$save=null,$width=200,$height=200){
        //判断图片是否存在
        $dinfo = self::imageInfo($dst);
        if($dinfo == false){
            return false;
        }

        //计算缩放比例
        $calc = min($width/$dinfo['width'],$height/$dinfo['height']);
        $w = (int)$dinfo['width']*$calc;
        $h = (int)$dinfo['height']*$calc;

        //当图片为长方形时缩略时短的一边需要留白
        $paddingx = (int)($width-$w)/2;
        $paddingy = (int)($height-$h)/2;

        //创建原始图画布
        $dfunc = 'imagecreatefrom'.$dinfo['ext'];
        $dim = $dfunc($dst);

        //创建缩略图画布
        $tim = imagecreatetruecolor($width,$height);

        //创建白色填充颜色
        $white = imagecolorallocate($tim,255,255,255);

        //填充略缩图画布
        imagefill($tim,0,0,$white);

        //复制并略缩
        imagecopyresampled($tim,$dim,$paddingx,$paddingy,0,0,$w,$h,$dinfo['width'],$dinfo['height']);

        //保存
        if(!$save){
            $save = $dst;
            unlink($dst);
        }

        $createfunc = 'image'.$dinfo['ext'];

        $createfunc($tim,$save);

        //创建图片失败时(此步主要是保存图片后缀或者路径有问题)
        if(!file_exists($save)){
            return false;
        }

        imagedestroy($tim);
        imagedestroy($dim);
        
        return true;

    }
        


    /**
     * 加水印函数
     * parm $dst string 待操作图
     * parm $water string 水印图
     * parm $save string 文件保存位置,不填默认,保存在原位置并且替换原始图
     * parm $pos int 水印存在位置,默认右上角
     * parm $alpha string 图片透明度,默认为50
     * 
     * 
     */
    public static function addWater($dst,$water,$save=null,$pos=2,$alpha=50){

        //判断两个图片是否存在
        if(!file_exists($dst) || !file_exists($water)){
            return false;
        }

        //获取两张图片的相关信息
        $dinfo = self::imageInfo($dst);
        $winfo = self::imageInfo($water);

        //判断两张图片的宽高的大小关系
        if($winfo['height']>$dinfo['height'] || $winfo['width']>$dinfo['width']){
            return false;
        }

        //获取图片后缀,定义相关读取函数
        $dfunc = 'imagecreatefrom'.$dinfo['ext'];
        $wfunc = 'imagecreatefrom'.$winfo['ext'];

        //如果根据后缀拼接的函数不存在
        if(!function_exists($dfunc) || !function_exists($wfunc)){
            return false;
        }

        //动态加载函数来创建画布
        $dim = $dfunc($dst);
        $wim = $wfunc($water);

        //定义水印位置
        switch($pos){
            case 1: //左上角
                $posx = 0;
                $posy = 0;
                break;

            case 2: //右上角
                $posx = $dinfo['width']-$winfo['width'];
                $posy = 0;
                break;

            case 3: //左下角
                $posx = 0;
                $posy = $dinfo['height']-$winfo['height'];
                break;
            case 4: //正中央
                $posx = ($dinfo['width']-$winfo['width'])/2;
                $posy = ($dinfo['height']-$winfo['height'])/2;
                $alpha = 15; //处于中间时透明度修改
                break;

            default: //其他数值则默认为右下角
                $posx = $dinfo['width']-$winfo['width'];
                $posy = $dinfo['height']-$winfo['height'];

        }

        //加水印
        imagecopymerge($dim,$wim,$posx,$posy,0,0,$winfo['width'],$winfo['height'],$alpha);

        //保存
        if(!$save){
          $save = $dst;
          unlink($dst) ;//删除原图 
        }

        //创建保存输出
        $createfunc = 'image'.$dinfo['ext'];
        $createfunc($dim,$save);

        //创建图片失败时(此步主要是保存图片后缀或者路径有问题)
        if(!file_exists($save)){
            return false;
        }

        //销毁
        imagedestroy($dim);
        imagedestroy($wim);

        return true;
    }


}

 调用示例

ImageTool::thumb('./images/new006.jpg','./images/new008.jpg',600,400);

ImageTool::addWater('./images/new006.jpg','./images/meow.jpg','./images/new007.jpg',4);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值