Thinkphp5 生成二维码并与背景图、文字组合生成分享海报

自己做个记录,省的以后要去做过的项目里扒

1.下载安装包,地址:
链接:https://pan.baidu.com/s/1EBbgJYHXJp-4YQA7zDT5gw
提取码:qboc

2.下载完放到了vendor目录下,就可以直接用了
2.1如果只是单纯生成二维码,这一块即可

//生成二维码
public function qrcode(){
        $result = ['status'=>false,  'msg'=>'操作失败'];
        header("Content-type:image/png");
        require "../vendor/phpqrcode/phpqrcode.php";
        $qRcode = new \QRcode();


        $url = 'www.baidu.com';
        $qrcode_path = 'uploads/qocode/';
        is_dir($qrcode_path) OR mkdir($qrcode_path, 0777, true);
        $qrcode_img = $qrcode_path.'1.jpg';

        // 纠错级别:L、M、Q、H
        $level = 'L';
        // 点的大小:1到10,用于手机端4就可以了
        $size = 5;

        $qRcode->png($url, $qrcode_img, $level, $size);
        $imagestring = base64_encode(ob_get_contents());
        ob_end_clean();
        
        $result = ['status'=>true,  'data'=>$dis_img];
        return $result;
    }

一个普通的二维码就出来了
在这里插入图片描述
2.2 并与背景图、文字组合生成分享海报
控制器里

public function bgQrode(){
        $tmp_bg_image = 'static/img/template.png';//背景图路径

        //获取二维码,调用了上边那个方法
        $qrcode_img = '';
        $qr_res = $this->extendQrcode();
        if($qr_res['status']){
            $qrcode_img = $qr_res['data'];
        }

        //新文件名
        $share_path = 'uploads/share/';
        is_dir($share_path) OR mkdir($share_path, 0777, true);
        $share_img = $share_path.'1.jpg';
		
        composite_picture($tmp_bg_image, $qrcode_img, $share_img, false, '', '', false, '', 150, 510); 
        //模板背景, 二维码, 海报, 二维码是否缩小, 二维码缩小的宽度,二维码缩小的高度,是否等比例缩放, 文字, 二维码在x轴的位置, 二维码在y轴的位置

       
        $result = ['status'=>true, 'data'=>$share_img];
        return json_encode($result, 320);
    }

一些图片组合方法写到了common.php中,二维码的位置和文字的位置可以根据需要改动

/*
* 合并图片
* @ $bg_img 背景图片
* @ $qrcode_img 二维码图片
* @ $new_filename 新文件名
* @ $is_suoxiao 组合的图片是否缩小
* @ $n_w 缩小的宽
* @ $n_h 缩小的高
* @ $is_per 是否按比例缩小
* @ $text 文字
* @ $s_width 要组合的图片在x轴的位置
* @ $s_height 要组合的图片在y轴的位置
*/
function composite_picture($bg_img, $qrcode_img, $new_filename, $is_suoxiao, $n_w='', $n_h='', $is_per=false, $text='', $s_width='0', $s_height='0'){
    
    if($is_suoxiao){
        $src_im = imgsuoxiao($qrcode_img, $n_w, $n_h, $is_per);
    }else{
        $src_im = $qrcode_img;
    }
    
    $bgimg = imagecreatefromstring(file_get_contents($bg_img));//背景图
    $src = imagecreatefromstring(file_get_contents($src_im));//组合图
    list($src_w, $src_h) = getimagesize($src_im);

    imagecopy($bgimg, $src, $s_width, $s_height, 0, 0, $src_w, $src_h);

    list($bgimg_w, $bgimg_h, $bgimg_type) = getimagesize($bg_img);

    switch ($bgimg_type) {
        case 1://GIF
            header('Content-Type: image/gif');
            header('Content-Disposition: inline; filename="image.gif"');
            $result = imagegif($bgimg, $new_filename);
            break;
        case 2://JPG
            header('Content-Type: image/jpeg');
            header('Content-Disposition: inline; filename="image.jpg"');
            
            imagejpeg($bgimg, $new_filename);
            break;
        case 3://PNG
            header('Content-Type: image/png');
            header('Content-Disposition: inline; filename="image.png"');
            imagepng($bgimg, $new_filename);
            break;
        default:
            break;
    }
    imagedestroy($bgimg);
    imagedestroy($src);
    if($text){
        $newss = numimage($text,$new_filename,15,3,230,720);
        return $newss;
    }else{
        return $new_filename;
    }
    
    return $new_filename;
    // exit;
}

//缩小图片
function imgsuoxiao($filename, $n_w, $n_h, $is_per=false){
    list($width, $height, $dst_type)=getimagesize($filename);
    if($is_per){
        $per=0.3;
        $n_w=$width*$per;
        $n_h=$height*$per;
    }
    switch ($dst_type) {
        case 2://JPG
            $img=imagecreatefromjpeg($filename);
            break;
        case 3://PNG
            $img = imagecreatefrompng($filename);
            break;
        default:
            break;
    }
    $new=imagecreatetruecolor($n_w, $n_h);
    //copy部分图像并调整
    imagecopyresized($new, $img,0, 0,0, 0,$n_w, $n_h, $width, $height);
    //图像输出新图片、另存为
    imagejpeg($new, $filename);
    imagedestroy($new);
    imagedestroy($img);
    return $filename;
}


/**
 * 像图片中添加文字
  * @param $txt 文本文字
  * @param $image 图片路径
  * @param $size  文字大小
  * @param $scale 文字旋转度
  * @param $x 在x轴上的位置
  * @param $y 在y轴上的位置
  * @param $color 字体颜色
28  */
function numimage($txt,$image,$size,$scale,$x,$y, $color="黑色")
{
    list($dst_w, $dst_h, $dst_type) = getimagesize($image);
    switch ($dst_type) {
        case 2://JPG
            $im = imagecreatefromjpeg($image);
            break;
        case 3://PNG
            $im = imagecreatefrompng($image);
            break;
        default:
            break;
    }
    $textcolor = imagecolorallocate($im, 0, 0, 0);
    if($color=="白色"){
        $textcolor = imagecolorallocate($im, 255, 255, 255);
    }
    $qr_size = imagesx($im);
    $font = realpath('static/STSONG.TTF'); //引入字体
    imagettftext($im, $size,0,$x,$y, $textcolor, $font, $txt);
    $myImage = ImageCreate(245,245); //参数为宽度和高度
    imagecopyresampled($myImage, $im, 0, 0, 0, 0, 0, 80, 10, 10); //重新组合图片并调整大小
    header("Content-type: image/jpeg");
    imagejpeg($im, $image);
    imagedestroy($im);
    return $image;
}

海报就完成了
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值