zoukankan      html  css  js  c++  java
  • PHP 生成二维码底部拼接文字和中间拼接logo

    图片合成

    /**
     * 图片合并
     * 将源图片覆盖到目标图片上
     * @param string $dstPath 目标图片路径
     * @param string $srcPath 源图片路径
     * @param int $dstX 源图片覆盖到目标的X轴坐标
     * @param int $dstY 源图片覆盖到目标的Y轴坐标
     * @param int $srcX
     * @param int $srcY
     * @param int $pct 透明度
     * @param string $filename 输出的文件名,为空则直接在浏览器上输出显示
     * @return string $filename 合并后的文件名
     */
    function picMerge($dstPath,$srcPath,$dstX=0,$dstY=0,$srcX=0,$srcY=0,$pct=100,$filename='')
    {
        //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dstPath));
        $src = imagecreatefromstring(file_get_contents($srcPath));
        //获取水印图片的宽高
        list($src_w, $src_h) = getimagesize($srcPath);
        //将水印图片复制到目标图片上,最后个参数50是设置透明度,这里实现半透明效果
        imagecopymerge($dst, $src, 165, 225, 0, 0, $src_w, $src_h, 100);
        //如果水印图片本身带透明色,则使用imagecopy方法
        //imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
        //输出图片
        list($dst_w, $dst_h, $dst_type) = getimagesize($dstPath);
        switch ($dst_type) {
            case 1://GIF
                if(!$filename){
                    header('Content-Type: image/gif');
                    imagegif($dst);
                }else{
                    imagegif($dst,$filename);
                }
                break;
            case 2://JPG
                if(!$filename){
                    header('Content-Type: image/jpeg');
                    imagejpeg($dst);
                }else{
                    imagejpeg($dst,$filename);
                }
                break;
            case 3://PNG
                if(!$filename){
                    header('Content-Type: image/png');
                    imagepng($dst);
                }else{
                    imagepng($dst,$filename);
                }
                break;
            default:
                break;
        }
        imagedestroy($dst);
        imagedestroy($src);
        return $filename;
    }

    文字添加

    /**
     * 添加文字到图片上
     * @param $dstPath 目标图片
     * @param $fontPath 字体路径
     * @param $fontSize 字体大小
     * @param $text 文字内容
     * @param $dstY 文字Y坐标值
     * @param string $filename 输出文件名,为空则在浏览器上直接输出显示
     * @return string 返回文件名
     */
    function addFontToPic($dstPath,$fontPath,$fontSize,$text,$dstY,$filename='')
    {
        //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dstPath));
        //打上文字
        $fontColor = imagecolorallocate($dst, 0x00, 0x00, 0x00);//字体颜色
        $width = imagesx ( $dst );
        $height = imagesy ( $dst );
        $fontBox = imagettfbbox($fontSize, 0, $fontPath, $text);//文字水平居中实质
        imagettftext ( $dst, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), $dstY, $fontColor, $fontPath, $text);
        //输出图片
        list($dst_w, $dst_h, $dst_type) = getimagesize($dstPath);
        switch ($dst_type) {
            case 1://GIF
                if(!$filename){
                    header('Content-Type: image/gif');
                    imagegif($dst);
                }else{
                    imagegif($dst,$filename);
                }
                break;
            case 2://JPG
                if(!$filename){
                    header('Content-Type: image/jpeg');
                    imagejpeg($dst);
                }else{
                    imagejpeg($dst,$filename);
                }
                break;
            case 3://PNG
                if(!$filename){
                    header('Content-Type: image/png');
                    imagepng($dst);
                }else{
                    imagepng($dst,$filename);
                }
                break;
            default:
                break;
        }
        imagedestroy($dst);
        return $filename;
    }
  • 相关阅读:
    技术领导要不要写代码?
    资深程序员告诉你:如何用五年时间攒够100万?
    mfc基于对话框的应用程序,如何设置初始对话框大小,移动控件位置
    zend studio,操作记录
    xampp怎么操作数据库mysql
    mysql-font的理解
    delphi 中配置文件的使用(*.ini)和TIniFile 用法
    delphi 创建服务,安装与卸载服务
    sublime Text的一些用法(emmet插件、)
    apache (web服务器) ->php->mysql,xampp与wamp比较,WAMP与WNMP有什么区别
  • 原文地址:https://www.cnblogs.com/chenkg/p/14529969.html
Copyright © 2011-2022 走看看