资讯专栏INFORMATION COLUMN

PHP_GD库

KevinYan / 1943人阅读

摘要:库画图的典型流程创建画布创建各种颜料绘画如,写字,画线,画矩形等形状保存成图片清理画布画线保存图片保存成功保存失败输出图片字母数字验证码画布中文验证码中文验证码实际项目中抽取几百个,几千个常用汉字,放数组里,随机选取的一是在了不和有

GD库画图的典型流程

创建画布

创建各种颜料

绘画(如,写字,画线,画矩形等形状)

保存成图片

清理画布


字母数字验证码
中文验证码
扭曲验证码
 扭曲验证码
    
    class Image {
        public static function code() {
            
            $str = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjklmnpqrstuvwxyz23456789";
            $text = substr(str_shuffle($str), 0, 4);
            
            $src = imagecreatetruecolor(50, 25);
            $dst = imagecreatetruecolor(50, 25);
            
            $src_gray = imagecolorallocate($src, 200, 200, 200);
            $dst_gray = imagecolorallocate($dst, 200, 200, 200);
            
            imagefill($src, 0, 0, $src_gray);
            imagefill($dst, 0, 0, $dst_gray);
            
            $color = imagecolorallocate($src, mt_rand(50, 255), mt_rand(50, 150), mt_rand(50, 200));
            
            imagestring($src, 6, 7, 5, $text, $color);
            
            // 扭曲
            for ( $i=0; $i<60; $i++ ) {
                
                $offsetY = 3; // 最大波动像素  (px)
                $round = 2; // 2个周期  即 (4π)
                $posY = round(sin( ($round * 2 * M_PI / 60) * $i ) * $offsetY); // 根据正选曲线计算上下波动的 posY 
                
                imagecopy($dst, $src, $i, $posY, $i, 0, 1, 25);
                
            }
            
            // 显示
            header("Content-type: image/png");
            imagepng($dst);
            
            imagedestroy($src);
            imagedestroy($dst);
            
        } 
    }      
    
    echo Image::code();        

?>
图片处理类

水印 : 把指定的水印复制到目标上,并加透明效果

缩略图 : 把大图片复制到小尺寸画面上

图片信息

通过getimagesize() 获取图片的大小,和后缀名.

判断图片是否存在

getimagesize() 是否是有解析图片信息

处理图片的大小和后缀信息.

    

水印

从一张图片中读取到另一张图片上,通过imagecopymerge()实现

判断图片是否存在

水印小图片是否比原始图片大

水印图的位置

处理水印图

 $dstInfo["height"] || $waeterInfo["width"] > $dstInfo["width"] ) {
            return false;
        }
            
        // 两张图片读取到画布上, 使用处理  动态函数读取
        $dFun = "imagecreatefrom" . $dstInfo["ext"];
        $wFun = "imagecreatefrom" . $dstInfo["ext"];
        
        // 是否存在函数
        if ( !function_exists($dFun) || !function_exists($wFun) ) {
            return false;
        }
        
        // 动态加载函数创建画布
        $dIm = $dFun($dst); // 创建待操作的画布
        $wIm = $wFun($water); // 创建水印画布
        
        // 处理水印的位置 计算粘贴的坐标
        switch ($pos) {
            case 0: // 左上角
                $posX = 0;
                $posY = 0;
                break;
            case 1: // 右上角
                $posX = $dstInfo["width"] - $waeterInfo["width"];
                $poxY = 0;
                break;
            case 2: // 居中
                $posX = ($dstInfo["width"] - $waeterInfo["width"]) / 2;
                $posY = ($dstInfo["height"] - $waeterInfo["height"]) / 2;
                break; 
            case 3: // 左下角
                $posX = 0;
                $posY = $dstInfo["height"] - $waeterInfo["height"];
                break;
            case 4: // 右下角
                $posX = $dstInfo["width"] - $waeterInfo["width"];
                $posY = $dstInfo["height"] - $waeterInfo["height"];
                break;
            
            case 5: // 底部中间
                $posX = ($dstInfo["width"] - $waeterInfo["width"]) / 2;
                $posY = $dstInfo["height"] - $waeterInfo["height"];
                break;
        }
                                
        // 加水印
        imagecopymerge($dIm, $wIm, $posX, $posY, 0, 0, $waeterInfo["width"], $waeterInfo["height"], $alpha);
                    
        // 保存
        if (!$save) {
            $save = $dst;
            unlink($dst); // 删除原图片
        }
        
        // 生成水印
        $createFun = "image" . $dstInfo["ext"];
        $createFun($dIm, $save);
            
        imagedestroy($dIm);
        imagedestroy($wIm);
        
        return true;            
    }    

?>
生成缩略图

创建画布生成缩略图,通过imagecopyresampled()

判断文件是否存在

图片信息是否为假

计算缩放比例

创建画布

计算留白和宽高

处理缩略图

    /**
     * thumb 生成缩略图 
     * 等比例缩放,两边留白
     * @param {String} $dst 原始路径
     * @param {String} $save 保存路径
     * @param {Int} $width 缩略图 宽度
     * @param {Int} $height 缩略图 高度
     * @return {Boolen} 生成缩略图是否成功  
     */
    public static function thumb( $dst, $save=NULL, $width=200, $height=200 ) {
        
        // 判断路径是否存在
        if ( !file_exists($dst) ) {
            return false;
        }
        
        $dinfo = self::imageInfo($dst);
        // 图片信息为假
        if ( $dinfo == false ) {
            return false;
        }
        
        // 计算缩放比例
        $calc = min($width / $dinfo["width"], $height / $dinfo["height"]);
        
        // 创建原始图画布
        $dfunc = "imagecreatefrom" . $dinfo["ext"];
        $dim = $dfunc($dst);
        
        // 创建缩略画布
        $tim = imagecreatetruecolor($width, $height);
        
        // 创建白色填充缩略画布
        $while = imagecolorallocate($tim, 255, 255, 255);
        
        imagefill($tim, 0, 0, $while);
        
        // 复制并缩略
        $dwidth = (int)$dinfo["width"] * $calc;
        $dheight = (int)$dinfo["height"] * $calc;
        
        $paddingx = (int)($width - $dwidth) / 2;
        $paddingy = (int)($height - $dheight) / 2 ;
        imagecopyresampled($tim, $dim, $paddingx, $paddingy, 0, 0, $dwidth, $dheight, $width, $height);
        
        // 保存图片
        if ( !$save ) {
            $save = $dst;
            unlink($dst);
        }
        
        $createfun = "image" . $dinfo["ext"];
        $createfun($tim, $save);
        
        // 销毁
        imagedestroy($dim);
        imagedestroy($tim);
        return true;
        
    }     
?>
ImageTool.class
class ImageTool {
    
    /**
     * 分析图片的信息
     * @param {String} $image 图片路径 
     * @return {mixin} Array Boolean
     */
    protected static function imageInfo( $image ) {
        
        // 判断图片是否存在
        if(!file_exists($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;            
    }    
    
    /**
     * 加水印
     * @param {String} $dst 目标图片
     * @param {String} $water 水印小图片
     * @param {String} $save 存储图片位置   默认替换原始图
     * @param {Int} $alpha 透明度
     * @param {Int} $pos 水印位置
     * @return {Boolean} 添加水印是否成功
     */
    public static function addWater($dst, $water, $save=NULL, $pos=4, $alpha=50) {
         
        // 保证二个文件是否存在
        if(!file_exists($dst) || !file_exists($water)) {
            return false;
        }
        
        $dstInfo = self::imageInfo($dst); // 读取图片信息
        $waeterInfo = self::imageInfo($water); // 读取图片信息 
        
        // 水印不能比待操作图片大
        if( $waeterInfo["height"] > $dstInfo["height"] || $waeterInfo["width"] > $dstInfo["width"] ) {
            return false;
        }
            
        // 两张图片读取到画布上, 使用处理  动态函数读取
        $dFun = "imagecreatefrom" . $dstInfo["ext"];
        $wFun = "imagecreatefrom" . $dstInfo["ext"];
        
        // 是否存在函数
        if ( !function_exists($dFun) || !function_exists($wFun) ) {
            return false;
        }
        
        // 动态加载函数创建画布
        $dIm = $dFun($dst); // 创建待操作的画布
        $wIm = $wFun($water); // 创建水印画布
        
        // 处理水印的位置 计算粘贴的坐标
        switch ($pos) {
            case 0: // 左上角
                $posX = 0;
                $posY = 0;
                break;
            case 1: // 右上角
                $posX = $dstInfo["width"] - $waeterInfo["width"];
                $poxY = 0;
                break;
            case 2: // 居中
                $posX = ($dstInfo["width"] - $waeterInfo["width"]) / 2;
                $posY = ($dstInfo["height"] - $waeterInfo["height"]) / 2;
                break; 
            case 3: // 左下角
                $posX = 0;
                $posY = $dstInfo["height"] - $waeterInfo["height"];
                break;
            case 4: // 右下角
                $posX = $dstInfo["width"] - $waeterInfo["width"];
                $posY = $dstInfo["height"] - $waeterInfo["height"];
                break;
            
            case 5: // 底部中间
                $posX = ($dstInfo["width"] - $waeterInfo["width"]) / 2;
                $posY = $dstInfo["height"] - $waeterInfo["height"];
                break;
        }
                                
        // 加水印
        imagecopymerge($dIm, $wIm, $posX, $posY, 0, 0, $waeterInfo["width"], $waeterInfo["height"], $alpha);
                    
        // 保存
        if (!$save) {
            $save = $dst;
            unlink($dst); // 删除原图片
        }
        
        // 生成水印
        $createFun = "image" . $dstInfo["ext"];
        $createFun($dIm, $save);
            
        imagedestroy($dIm);
        imagedestroy($wIm);
        
        return true;            
    }    
    
    /**
     * thumb 生成缩略图 
     * 等比例缩放,两边留白
     * @param {String} $dst 原始路径
     * @param {String} $save 保存路径
     * @param {Int} $width 缩略图 宽度
     * @param {Int} $height 缩略图 高度
     * @return {Boolen} 生成缩略图是否成功  
     */
    public static function thumb( $dst, $save=NULL, $width=200, $height=200 ) {
        
        // 判断路径是否存在
        if ( !file_exists($dst) ) {
            return false;
        }
        
        $dinfo = self::imageInfo($dst);
        // 图片信息为假
        if ( $dinfo == false ) {
            return false;
        }
        
        // 计算缩放比例
        $calc = min($width / $dinfo["width"], $height / $dinfo["height"]);
        
        // 创建原始图画布
        $dfunc = "imagecreatefrom" . $dinfo["ext"];
        $dim = $dfunc($dst);
        
        // 创建缩略画布
        $tim = imagecreatetruecolor($width, $height);
        
        // 创建白色填充缩略画布
        $while = imagecolorallocate($tim, 255, 255, 255);
        
        imagefill($tim, 0, 0, $while);
        
        // 复制并缩略
        $dwidth = (int)$dinfo["width"] * $calc;
        $dheight = (int)$dinfo["height"] * $calc;
        
        $paddingx = (int)($width - $dwidth) / 2;
        $paddingy = (int)($height - $dheight) / 2 ;
        imagecopyresampled($tim, $dim, $paddingx, $paddingy, 0, 0, $dwidth, $dheight, $width, $height);
        
        // 保存图片
        if ( !$save ) {
            $save = $dst;
            unlink($dst);
        }
        
        $createfun = "image" . $dinfo["ext"];
        $createfun($tim, $save);
        
        // 销毁
        imagedestroy($dim);
        imagedestroy($tim);
        return true;
        
    } 
    
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/22068.html

相关文章

  • 用PHP读取Excel、CSV文件

    摘要:读取文件的库有很多,但用的比较多的有,现在已经不再维护了,最新的一次提交还是在年月号,建议直接使用,而且这两个项目都是同一个组织维护的,本文介绍的使用。 showImg(https://segmentfault.com/img/bVbl9a1?w=1308&h=240); PHP读取excel、csv文件的库有很多,但用的比较多的有: PHPOffice/PHPExcel、PHPOff...

    hiYoHoo 评论0 收藏0
  • MixPHP 环境搭建之 Apache + PHP

    摘要:在多种环境中迁移,代码无需修改,是无缝迁移的。由于大部分用户开发是在中进行,因此开发阶段我们推荐使用部署方案,因为更简单快速,下面整体演示一下的环境搭建。安装解压至指定安装目录。先不要启动,这会启动会报错,没加环境变量。 MixPHP 是一款基于 Swoole 的常驻内存型 PHP 高性能框架。 MixPHP 同时支持多种环境中执行: Nginx + mix-httpd (使用到 S...

    Barrior 评论0 收藏0
  • MixPHP 环境搭建之 Apache + PHP

    摘要:在多种环境中迁移,代码无需修改,是无缝迁移的。由于大部分用户开发是在中进行,因此开发阶段我们推荐使用部署方案,因为更简单快速,下面整体演示一下的环境搭建。安装解压至指定安装目录。先不要启动,这会启动会报错,没加环境变量。 MixPHP 是一款基于 Swoole 的常驻内存型 PHP 高性能框架。 MixPHP 同时支持多种环境中执行: Nginx + mix-httpd (使用到 S...

    microelec 评论0 收藏0
  • 64位windows7安装apache2.4+php7.1+mysql5.7+apcu

    摘要:表示需要运行库,缺少它将会在接来下的过程中弹出类似的提示运行库下载如果以前安装过,则不必再装。回车回车回车即可将服务创建,关闭窗口。下的安装有一个临时密码问题。有空再把下的安装记录一下。 最近想更新Web服务器上的软件,查了一下apache、php、mysql版本都很高了,有些变动还很大,所以先在Win上安装熟悉一下,下面是安装配置记录: 系统:64位Windows7时间:2017年3...

    pingink 评论0 收藏0
  • 64位windows7安装apache2.4+php7.1+mysql5.7+apcu

    摘要:表示需要运行库,缺少它将会在接来下的过程中弹出类似的提示运行库下载如果以前安装过,则不必再装。回车回车回车即可将服务创建,关闭窗口。下的安装有一个临时密码问题。有空再把下的安装记录一下。 最近想更新Web服务器上的软件,查了一下apache、php、mysql版本都很高了,有些变动还很大,所以先在Win上安装熟悉一下,下面是安装配置记录: 系统:64位Windows7时间:2017年3...

    _ivan 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<