php实现 生成微信小程序二维码,进行自定义更替中间logo图(完整版)

小程序用户在生成分享二维码时,默认使用的是小程序设置的icon图片,但是我们有些时候往往想自定义图片进而换成我们自己想要的。微信小程序开发文档上也没有提供相应的方法,那么我们只能自行进行替换,下面开始代码:

一、代码入口文件,接受小程序二维码及需要替换成的logo图,并输出最终结果

/**
     * [GetBgCode 处理小程序二维码,中间logo自定义,入口文件]   
     */
    public function GetBgCode(){     
        $headimg=ROOT_PATH . 'upload/headimg.jpg';//要替换的logo地址,网图则需要保存到本地再进行处理,这里处理的本地图片
        $qrcode=ROOT_PATH . 'upload/qrcode.jpg';//小程序已经获取到的二维码图
$imgg = $this->GetYuanImg($headimg); //保存处理之后的圆图 $file_name = "yuan".time().rand(10000, 99999).".png"; imagepng($imgg,ROOT_PATH . 'upload' . DS . 'qrcode1_y1/'.$file_name); imagedestroy($imgg); //缩小圆图 $comp_path=$this->NarrowHeadImg($file_name); //拼接图片 $url =$this->CreateImgWatermark($qrcode,$comp_path,"center"); //处理完的新小程序码 保存在服务器,传回地址 $arr = array('ret'=>1, 'msg'=>'success', 'data'=>array('url'=>$url), ); echo json_encode($arr); }    

   二、剪切图片为原型 

/**
     * [GetYuanImg 编辑图片为原型,剪切图片为原型]
     * @param    [string] $imgpath [头像保存之后的图片名]
     */
    public function GetYuanImg($imgpath) {
        $ext     = pathinfo($imgpath);
        $src_img = null;
        switch ($ext['extension']) {
            case 'jpg':
                $src_img = imagecreatefromjpeg($imgpath);
                break;
            case 'png':
                $src_img = imagecreatefrompng($imgpath);
                break;
            case "jpeg":
                $img_r=imagecreatefromjpeg($imgpath);
                break;
            case "gif":
                $img_r=imagecreatefromgif($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;
    }

  三、缩小剪切之后的圆图

 

 1   /**
 2      * [NarrowHeadImg 缩小图片]
 3      * @param    [type]   $file_name [缩小图片的最终保存地址]
 4      */
 5     public function NarrowHeadImg($file_name){
 6         $target_width=192; //定义缩小图片的大小
 7         $target_height=192;
 8 
 9         $target_img=ROOT_PATH . 'upload' . DS . 'qrcode1_y1/'.$file_name;
10         $img_info=getimagesize($target_img);  // 获取原图尺寸  13         
14 
15         $original_width=$img_info[0];       //原图片宽度
16         $original_height=$img_info[1];       //原图片高度
17         $original_mime=$img_info['mime'];
19 
20         $target_scale = $target_height/$target_width; //目标图像长宽比 
22         $original_scale = $original_height/$original_width; // 原图片长宽比
24         if ($original_scale>=$target_scale){  // 过高
25             $w = intval($original_width);
26             $h = intval($target_scale*$w);
28             $x = 0;
29             $y = ($original_height - $h)/3;
30         } else {                              // 过宽
31             $h = intval($original_height);
32             $w = intval($h/$target_scale);
34             $x = ($original_width - $w)/2;
35             $y = 0;
36         }
38 
39         $target_im = imagecreatetruecolor($target_width,$target_height);//固定图片大小 192
40         imagesavealpha($target_im, true); 
41         $trans_colour = imagecolorallocatealpha($target_im, 0, 0, 0, 127); 
42         imagefill($target_im, 0, 0, $trans_colour);                
43         //获取上文已保存的修改之后头像的内容        
44         $o_image = imagecreatefrompng(ROOT_PATH . 'upload' . DS . 'qrcode1_y1/'.$file_name);   
45 
46         imagecopyresampled($target_im,$o_image, 0, 0,0, 0, $target_width,$target_height, $w, $h);
47         $file_head_name = "yuan".time().rand(10000, 99999).".png";
48         $comp_path =ROOT_PATH . 'upload' . DS . 'qrcode1_y2/'.$file_head_name;
49         imagepng($target_im,$comp_path);
50         imagedestroy($target_im);
51         return $comp_path;
52 
53     }

  四、拼接小程序二维码及自定义图片 ,获取最后的地址

 1 /**
 2       * [CreateImgWatermark 拼接头像贴在二维码中间] 5       * @param    [type]                   $dest_image [小程序二维码图片地址]
 6       * @param    [type]                   $watermark  [logo图]
 7       * @param    [type]                   $locate     [水印位置,center,left_buttom,right_buttom 三选一]
 8       */
 9     public function CreateImgWatermark($dest_image,$watermark,$locate){
11         list($dwidth,$dheight,$dtype)=getimagesize($dest_image);
12         list($wwidth,$wheight,$wtype)=getimagesize($watermark);
13         $types=array(1 => "GIF",2 => "JPEG",3 => "PNG",
14             4 => "SWF",5 => "PSD",6 => "BMP",
15             7 => "TIFF",8 => "TIFF",9 => "JPC",
16             10 => "JP2",11 => "JPX",12 => "JB2",
17             13 => "SWC",14 => "IFF",15 => "WBMP",16 => "XBM");
18         $dtype=strtolower($types[$dtype]);//原图类型
19         $wtype=strtolower($types[$wtype]);//水印图片类型
20         $created="imagecreatefrom".$dtype;
21         $createw="imagecreatefrom".$wtype;
22         $imgd=$created($dest_image);
23         $imgw=$createw($watermark);
24         switch($locate){
25             case 'center':
26                 $x=($dwidth-$wwidth)/2;
27                 $y=($dheight-$wheight)/2;
28                 break;
29             case 'left_buttom':
30                 $x=1;
31                 $y=($dheight-$wheight-2);
32                 break;
33             case 'right_buttom':
34                 $x=($dwidth-$wwidth-1);
35                 $y=($dheight-$wheight-2);
36                 break;
37             default:
38                 die("未指定水印位置!");
39                 break;
40         }
41         imagecopy($imgd,$imgw,$x,$y,0,0, $wwidth,$wheight);
42         $save="image".$dtype;
43         //保存到服务器
44         $f_file_name = "bgaa".time().rand(10000, 99999).".png";
45         imagepng($imgd,ROOT_PATH . 'upload' . DS . 'qrcode1_bg/'.$f_file_name); //保存
46         imagedestroy($imgw);
47         imagedestroy($imgd);
50         //传回处理好的图片
51         $url =str_replace('/opt/****baby/','',ROOT_PATH . 'upload' . DS . 'qrcode1_bg/'.$f_file_name);
52         return $url;
53     }

 

posted @ 2020-07-12 22:55  默宇成殇  阅读(523)  评论(0)    收藏  举报