当前位置: 代码网 > it编程>编程语言>Php > PHP实现图片合并的示例详解

PHP实现图片合并的示例详解

2024年05月19日 Php 我要评论
业务需求我们需要一个微信小程序码,但是是需要提供给别人扫码的但是只有一个纯粹的小程序码是不好看的,所以需要推广的海报图片。再结合文字最终效果准备工作1、需要海报的底图2、小程序码的图片代码部分结合yi

业务需求

我们需要一个微信小程序码,但是是需要提供给别人扫码的但是只有一个纯粹的小程序码是不好看的,所以需要推广的海报图片。再结合文字

最终效果

准备工作

1、需要海报的底图

2、小程序码的图片 

代码部分结合yii2但不影响使用

完整过程

第一步:生成小程序码图片

第二步:缩放小程序码的图片大小  (如果尺寸符合海报大小可省略) 280-1280px

第三步:将缩放后的小程序图片合成到背景图片

第四步:合成文字信息

第一步:生成小程序码图片 (我使用的场景是无限制小程序码code地址 三种自行选择)

//微信小程序 小程序码
    public static function getwechatsmallprogramcode($scene)
    {
        $accesstoken = self::getaccesstoken();
        $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $accesstoken;
        $postdata = [
            'scene' => $scene,
            'page' => 'pages/index/index',
            'width'=>930
        ];
        $postdata = json_encode($postdata);
        $contentdata = self::sendpost($url, $postdata);
        return $contentdata; //如果图片大小符合这开启base64位图片地址也可以完成图片的合并合文字的合并
//        return self::base64urlcode($contentdata, 'image/png');
    }

    protected static function sendpost($url, $post_data)
    {
        $options = array(
            'http' => array(
                'method' => 'post',
                'header' => 'content-type:application/json',
                //header 需要设置为 json
                'content' => $post_data,
                'timeout' => 60
                //超时时间
            )
        );
        $context = stream_context_create($options);
        return file_get_contents($url, false, $context);
    }

    //二进制转图片image/png
    public static function base64urlcode($contents, $mime)
    {
        $base64 = base64_encode($contents);
        return ('data:' . $mime . ';base64,' . $base64);
    }

第二步:缩放小程序码的图片大小 

/**
     * 缩放图片尺寸
     * @param $img_path string 图片地址
     * @param $new_width
     * @param $new_height
     * @param $new_img_path string 新的图片地址
     */
    public static function piczoom($img_path,$new_width,$new_height,$new_img_path)
    {
        //获取尺寸
        list($width, $height, $img_type, $attr) = getimagesize($img_path);
        $imageinfo = [
            'width' => $width,
            'height' => $height,
            'type' => image_type_to_extension($img_type, false),
            'attr' => $attr
        ];
        $fun = "imagecreatefrom" . $imageinfo['type'];
        $image = $fun($img_path);
        //创建新的幕布
        $image_thump = imagecreatetruecolor($new_width, $new_height);
        //复制源文件
        imagecopyresampled($image_thump, $image, 0, 0, 0, 0, $new_width, $new_height, $imageinfo['width'], $imageinfo['height']);
        imagedestroy($image);

        $image = $image_thump;
        $func = 'image' . $imageinfo['type'];
        $func($image, $new_img_path);
    }

第三步:将缩放后的小程序图片合成到背景图片

/**
     * 图片合并
     * 将源图片覆盖到目标图片上
     * @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 合并后的文件名
     */
    public static 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, 80, 125, 0, 0, $src_w, $src_h, 100);
        imagecopymerge($dst, $src, $dstx, $dsty, $srcx, $srcy, $src_w, $src_h, $pct);
        //如果水印图片本身带透明色,则使用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);
    }

第四步:合成文字信息

/**
     *  添加文字到图片上
     * @param $dstpath string 目标图片
     * @param $fontpath string 字体路径
     * @param $fontsize string 字体大小
     * @param $text string 文字内容
     * @param $dsty string 文字y坐标值
     * @param string $filename 输出文件名,为空则在浏览器上直接输出显示
     * @return string 返回文件名
     */
    public static function addfonttopic($dstpath, $fontpath, $fontsize, $text, $dsty, $filename = '')
    {
        ob_end_clean();

        //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dstpath));
        //打上文字
        $fontcolor = imagecolorallocate($dst, 255, 255, 255);//字体颜色
        $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;
    }

外部的调用

/**
     * 根据店铺id 和名称 合成a5 图片小程序图片
     * @param $shop_id
     * @param $shop_name
     * @return array
     */
    public static function generatewechatappletimage($shop_id, $shop_name)
    {
        //1 生成小程序码
        //2 合成小程序码到背景图片
        $scenestr = '?shop_id=' . $shop_id;
        $wechatappimgbasedata = wxtools::getwechatsmallprogramcode($scenestr);
        $wechatappimgpath = './wechatappimg/shop_code_' . $shop_id . '.jpg';
        file_put_contents($wechatappimgpath, $wechatappimgbasedata);

        //合并到背景图片中
        $beijinimgpath = './wechatappimg/wechatbj.jpg';
        $mergeimgfile = './wechatappimg/shop_mini_program' . $shop_id . '.jpg';
        generatecodeimg::picmerge($beijinimgpath, $wechatappimgpath, 408, 714, $srcx = 0, $srcy = 0, $pct = 100, $mergeimgfile);

        //3 合成文字
        $fontpath = './plus/fonts/sourcehansanscn-bold.ttf';
        $fontsize = 40;
        $dsty = 640;
        generatecodeimg::addfonttopic($mergeimgfile, $fontpath, $fontsize, $shop_name, $dsty, $mergeimgfile);

        $wechatcodeimgurl = \yii::$app->request->hostinfo . '/wechatappimg/shop_code_' . $shop_id . '.jpg';
        $wechatappimgurl = \yii::$app->request->hostinfo . '/wechatappimg/shop_mini_program' . $shop_id . '.jpg';
        return [
            'wechatcodeimgurl' => $wechatcodeimgurl,
            'wechatappimgurl' => $wechatappimgurl,
        ];
    }

常见的问题

1文字合并的时候出现乱码?

第一检测一下字体是否是正常tff字体  如果不知道去c://windows/fonts 随便找一个 微软雅黑都行

2、英文阿拉布数字正常 中文乱码

$text = mb_convert_encoding("呵呵呵","utf-8","gbk");

$text = mb_convert_encoding("呵呵呵","html-entities","utf-8"); 

设置看看

到此这篇关于php实现图片合并的示例详解的文章就介绍到这了,更多相关php图片合并内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com