关于PHP的GD库合成图片

 

<?php

namespace Topxia\MobileBundleV3\Processor\Impl;

use Qiniu\Http\Request;
use Topxia\Common\ArrayToolkit;
use Topxia\Common\bpToolKit;
use Topxia\MobileBundleV3\Processor\BaseProcessor;
use Topxia\MobileBundleV3\Processor\ExamPaperProcessor;
use Endroid\QrCode\QrCode;
use Symfony\Component\BrowserKit\Response;


class ParentTestProcessorImpl extends BaseProcessor implements ExamPaperProcessor
{
    public function testQrcode(){
        array('lion','crane','fox','kangaroo','parrot','rabbit');
        $resultType = 'rabbit';
        $backgroundPath = 'assets/img/parent-test/'.$resultType.'.png';
        $image_1 = imagecreatefrompng($backgroundPath);   //背景图
        $qrcodePath = $this->createQrcode($this->request,'http://www.baidu.com', array('type'=>$resultType));
        $image_2 = imagecreatefrompng($qrcodePath);

        // 创建真彩画布
        $image_3 = imageCreatetruecolor(imagesx($image_1),imagesy($image_1));
        // 为真彩画布创建白色背景
        $color = imagecolorallocate($image_3, 255, 255, 255);

        imagefill($image_3, 0, 0, $color);
        //设置透明
        imageColorTransparent($image_3, $color);

        //把背景复制到画布之上
        imagecopyresampled($image_3, $image_1, 0, 0, 0, 0, imagesx($image_1), imagesy($image_1), imagesx($image_1), imagesy($image_1));

        $user = $this->getUserService()->getUser(10);
        $path_4 = $user['mediumAvatar'];
        $image_4 = $this->yuanjiao(str_replace('public://','files/',$path_4));   //头像
        imagepng($image_4,'tt.png');
        $image_4 = imagecreatefrompng('tt.png');

        //设置头像的背景色透明
        $color = imagecolorat ($image_4,0,0);    //吸颜色
        imageColorTransparent($image_4, $color);
        //把二维码黏贴到画布之上
        imagecopymerge($image_3, $image_2, 570, 1170, 0, 0, imagesx($image_2), imagesy($image_2), 100);
        //把头像黏贴到画布之上
        imagecopymerge($image_3, $image_4, 315, 50, 0, 0, imagesx($image_4), imagesy($image_4), 100);
        // 输出合成图片
        imagepng($image_3,'files/parent-test/result.png');

    }


     /**
      * 将图片切成圆角
      */
      public function yuanjiao($imgpath){
        $ext  = pathinfo($imgpath);
        $src_img = null;
        switch ($ext['extension']) {
          case 'jpg':
                $src_img = imagecreatefromjpeg($imgpath);
            break;
          case 'png':
                $src_img = imagecreatefrompng($imgpath);
            break;
        }
        $wh = getimagesize($imgpath);
        $w = $wh[0];
        $h = $wh[1];
        $w = min($w, $h);
        $h = $w;
        $img = imagecreatetruecolor($w, $h);
        //这一句一定要有
        imagesavealpha($img, true);
        //拾取一个完全透明的颜色,最后一个参数127为全透明
        $bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
        imagefill($img, 0, 0, $bg);
        $r = $w / 2; //圆半径
        $y_x = $r; //圆心X坐标
        $y_y = $r; //圆心Y坐标
        for ($x = 0; $x < $w; $x++) {
          for ($y = 0; $y < $h; $y++) {
                $rgbColor = imagecolorat($src_img, $x, $y);
                if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
                   imagesetpixel($img, $x, $y, $rgbColor);
                }
          }
        }
        return $img;
      }


    public function createQrcode($request,$url,$conditions){
        $host = $request->getHttpHost();
        if(strpos($host,'feimooc') !== false) {
            $urlHost = 'https://m.feimooc.com';
        }else{
            $urlHost = 'http://edureact.bp1010.cn';
        }
        $qrCode = new QrCode();
        $qrCode->setText($url);
        $qrCode->setSize(100);
        $qrCode->setPadding(10);
        switch ($conditions['type']){
            //狮子
            case 'lion':
                $qrCode->setForegroundColor(array('r' => 52, 'g' => 38, 'b' => 150, 'a' => 0));
                $qrCode->setBackgroundColor(array('r' => 134, 'g' => 116, 'b' => 254, 'a' => 0));
                break;
            //野鹤
            case 'crane':
                $qrCode->setForegroundColor(array('r' => 25, 'g' => 114, 'b' => 135, 'a' => 0));
                $qrCode->setBackgroundColor(array('r' => 89, 'g' => 208, 'b' => 240, 'a' => 0));
                break;
            //狐狸
            case 'fox':
                $qrCode->setForegroundColor(array('r' => 128, 'g' => 68, 'b' => 15, 'a' => 0));
                $qrCode->setBackgroundColor(array('r' => 255, 'g' => 168, 'b' => 92, 'a' => 0));
                break;
            //袋鼠
            case 'kangaroo':
                $qrCode->setForegroundColor(array('r' => 105, 'g' => 77, 'b' => 46, 'a' => 0));
                $qrCode->setBackgroundColor(array('r' => 254, 'g' => 207, 'b' => 97, 'a' => 0));
                break;
            //鹦鹉
            case 'parrot':
                $qrCode->setForegroundColor(array('r' => 11, 'g' => 107, 'b' => 91, 'a' => 0));
                $qrCode->setBackgroundColor(array('r' => 82, 'g' => 217, 'b' => 195, 'a' => 0));
                break;
            //兔子
            case 'rabbit':
                $qrCode->setForegroundColor(array('r' => 150, 'g' => 48, 'b' => 76, 'a' => 0));
                $qrCode->setBackgroundColor(array('r' => 252, 'g' => 131, 'b' => 166, 'a' => 0));
                break;
        }

        $img = $qrCode->get('png');
        $filePathDir = 'files/parent-test/';
        if (!is_dir($filePathDir)) {
            mkdir($filePathDir, 0777, true);
        }
        $filePath = $filePathDir.'qrcodeTest.png';
        file_put_contents($filePath,$img);
        $headers = array(
            'Content-Type'     => 'image/png',
            'Content-Disposition' => 'inline; filename="image.png"',
        );
        return $filePath;
//        return new Response($img, 200, $headers);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值