在开发中写了一个生成商品推广海报的功能,代码记录到这里以备后面有需求的时候直接用就可以:


        $mod = new Model_Product();
        $item = $mod->getProduct($this->_input->id);
        if (empty($item)) {
            return $this->warning('商品已删除');
        }

        $conf   = Sr::arrayGet($this->conf, 'wxapp');
        $page   = 'p/index';

        if (false) {
            //test app
            $conf['app_id'] = 'wx499be8478f6cd037';
            $conf['secret'] = '9c0e112b45c108f39c7976ca8a36359e';
            $page = '';
        }

        $wxapp    = new SrWxApp($conf);
        $images = explode(',', $item['image']);
        $dst_path = $images[0]; // 背景图
        $user_id  = $this->_userId;
        $filepath = 'product/'.$item['id'].'_' . $user_id . '_' . md5($item['image']) . '.png';
        $outpath  = $this->conf['upload_path'] . $filepath;
        if (is_file($outpath)) {
            //邀请海报存在,直接返回
            $url = str_replace('http:', 'https:', $this->conf['upload_domain']);
            return $this->success(['url'=>$url. $filepath]);
        }

        if (!is_dir(dirname($outpath))) {
            @mkdir(dirname($outpath), 0766, true);
        }

        $qrcodepath    = "product/{$item['id']}_{$user_id}_". md5($page) .'.qrcode.png';
        $qrcodefile = $this->conf['upload_path'] . $qrcodepath;
        if (!is_file($qrcodefile)) {
            //创建二维码
            if (!is_dir(dirname($qrcodefile))) {
                @mkdir(dirname($qrcodefile), 0766, true);
            }
            $res = $wxapp->getUnlimit('iid='.$item['id'].'&uid='.$user_id, ['page'=>$page], $qrcodefile);
            if (!$res) {
                $this->logger->log($wxapp->error, 'create_invite_image_err');
                $this->error = '获取二维码失败';
                return false;
            }
        }

        //配置
        $qrw = 150; //码宽高
        $qrh = 150;
        $bgx = 575; //码坐标
        $bgy = 775;

        //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        $src = imagecreatefromstring(file_get_contents($qrcodefile));
        //将二维码白色背景透明,接口有参数可以做到透明的
        //imagecolortransparent($src, imagecolorallocate($src, 255, 255, 255));
        //获取水印图片的宽高
        list($dst_w, $dst_h) = getimagesize($dst_path);
        list($src_w, $src_h) = getimagesize($qrcodefile);
        //创建图像,将二维码和商品图片复制到图片上
        $newimg = imagecreatetruecolor(750, 750+200);
        //将图片设置成白色
        $bgcolor = imagecolorallocate($newimg, 255, 255, 255);
        $txtcolor = imagecolorallocate($newimg, 50, 50, 50);
        imagefill($newimg, 0,0, $bgcolor);
        //写文本
        $fontfile = SOTER_APP_PATH . '/data/Alibaba-PuHuiTi-Medium.ttf';
        imagefttext($newimg, 24, 0, 25, 750 + 25 + 24, $txtcolor, $fontfile, $item['title']);
        imagefttext($newimg, 14, 0, 450, 915, $txtcolor, $fontfile, '长按扫码购买');
        imagecopyresampled($newimg, $dst, 0, 0, 0, 0, 750, 750, $dst_w, $dst_h);
        imagecopyresampled($newimg, $src, $bgx, $bgy, 0, 0, $qrw, $qrh, $src_w, $src_h);
        //将水印图片复制到目标图片上
        //imagecopymerge($dst, $src, $bgx, $bgy, 0, 0, $qrw, $qrh, 100);
        //生成图片
        $uu       = imagepng($newimg, $outpath);
        //销毁
        imagedestroy($dst);
        imagedestroy($src);
        imagedestroy($newimg);
        if ($uu) {
            $url = str_replace('http:', 'https:', $this->conf['upload_domain']);
            return $this->success(['url' => $url . $filepath]);
        }

        return $this->warning('创建海报失败');

标签: none

添加新评论