TP5生成海报源码

             PHP生成海报

php生成海报,包括背景图片和小程序二维码、文字、用户头像等相互合并。

php代码示例:

TP5生成海报源码_第1张图片

//海报生成
   public function poster($adminList){
      $adminList=json_decode($adminList,true);
        $data=[
          'adminId'=>$adminList['id'],
          'type'=>'adminIdPoster',
        ];
        //获取二维码
       $qrcode=action('home/Qrcode/getQRcode',json_encode($data));
      if(!$qrcode){
         return 0;
      }
      //二维码和背景图片生成(图一)
      $oneImg=$this->oneImg($qrcode,$adminList['image'],$adminList['location']);
      //头像下载本地
       $avater=$this->posterHeadimg($adminList['head_img']);
        //合并图片(头像和图一)   图二
      $twoImg=$this->twoImg($avater,$oneImg,$adminList['location']);
       //文字生成图片
        $texts=$this->textImgs($adminList['realname']); 
     合并图片(文字生成图片和图二) 海报
        $poster=$this->threeImg($texts,$twoImg,$adminList['location']);//$adminList['location'] 二维码位置
        if($poster){
           return $poster;
        }else{
           return 0;
        }
   }

   public function threeImg($image_2,$image_1,$location){
       if($location == 2){//二维码右下角
         imagecopymerge($image_1, $image_2, 30, 1140, 0,0, imagesx($image_2),imagesy($image_2),100);
       }else{//二维码左下角
            imagecopymerge($image_1, $image_2, 280, 1140, 0,0, imagesx($image_2),imagesy($image_2),100);
       }
       $path = DOC_ROOT.'upload/poster';
       if(!file_exists($path)) {
              mkdir($path, 0777, true);
           }
        // 输出合成图片
        $merge = 'poster_'.time().'.png';
        imagepng($image_1,'./upload/poster/'.$merge);
        imagedestroy($image_1);
        $newsImg=IMG_PATH.'upload/poster/'.$merge;
        return $newsImg;
     }

     //二维码和背景图片生成
    public function oneImg($qrcode,$path_2,$location){
      $image_2=$this->resizeimage($qrcode,220,220,'jpeg');
   //下载阿里图片
       $path_2=$this->posterHeadimg($path_2);
       $imgInfo=getimagesize($path_2);
       if(explode('/',$imgInfo['mime'])[1] == 'jpeg'){
            $image_1=imagecreatefromjpeg($path_2);
       }else{
            $image_1 = imagecreatefrompng($path_2);
       }
        //合成图片
        //将 src_im 图像中坐标从 src_x,src_y 开始,宽度为 src_w,高度为 src_h 的一部分拷贝到 dst_im 图像中坐标为 dst_x 和 dst_y 的位置上。两图像将根据 pct 来决定合并程度,其值范围从 0 到 100。当 pct = 0 时,实际上什么也没做,当为 100 时对于调色板图像本函数和 imagecopy() 完全一样,它对真彩色图像实现了 alpha 透明。
        if($location == 2){
           imagecopymerge($image_1, $image_2, 500, 1110, 0,0, imagesx($image_2),imagesy($image_2),100);
        }else{
           imagecopymerge($image_1, $image_2, 50, 1110, 0,0, imagesx($image_2),imagesy($image_2),100);
        }
        return $image_1;
     }

     //图片合成生成
      public function twoImg($avater,$image_1,$location){
       $image_2=$this->yuanjiao($avater);
        imagecopymerge($image_1, $image_2, 450, 1660, 0,0, imagesx($image_2),imagesy($image_2),100);
        return $image_1;
     }

     //下载阿里图片到本地
    public function posterHeadimg($imgurl){
       $imgs=Cache::get($imgurl);
       // $imgs='';
       if(!$imgs){
          $path = DOC_ROOT.'upload/detele';
           if(!file_exists($path)) {
               mkdir($path, 0777, true);
           }
           ob_clean();
           ob_start();
           $ext="/".time().".png";
           readfile($imgurl);     //读取图片
           $img=ob_get_contents(); //得到缓冲区中保存的图片
           ob_end_clean();     //清空缓冲区
           $fp = fopen($path.$ext,'w');    //写入图片
           if(fwrite($fp,$img)){
               fclose($fp);
               $imgs=IMG_PATH.'upload/detele'.$ext;
               Cache::set($imgurl,$imgs);
               return $imgs;
           }
           return false;
       }else{
          return $imgs;
       }
    }

    //文字生成图片
    public function textImgs($text){
        // header("Content-type:image/png");
        $im = imagecreatetruecolor(450,300); 
        $white = imagecolorallocate($im, 255, 255, 255); 
        $grey = imagecolorallocate($im, 128, 128, 128); 
        $black = imagecolorallocate($im, 0, 0, 0); 
        imagefilledrectangle($im, 0, 0, 450,300, $white); 
        // Replace path by your own font path 
        $font = DOC_ROOT.'static/font/fonts/simkai.ttf';
        $txt='欢迎来到
'.$text.'
提供的多元智慧测评服务';
        imagettftext($im, 27, 0, 20, 40, $black, $font, $txt); 
        return $im;  
    }

    public function yuanjiao($avatar){
       $imgInfo=getimagesize($avatar);
          if(explode('/',$imgInfo['mime'])[1] == 'jpeg'){
          $imgpath=$this->resizeimage($avatar,150,150,'jpeg');
          }else{
              $imgpath=$this->resizeimage($avatar,150,150,'png');
          }
        $ext= pathinfo($imgpath);
        $src_img = null;
        switch ($ext['extension']) {
            case 'jpeg':
                $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 resizeimage($srcfile,$mySize,$myHeight,$type='png'){
        //因为PHP只能对资源进行操作,所以要对需要进行缩放的图片进行拷贝,创建为新的资源
        if($type === 'png'){
            $src=imagecreatefrompng($srcfile);
        }else{
            $src=imagecreatefromjpeg($srcfile);
        }
        //取得源图片的宽度和高度
        $size_src=getimagesize($srcfile);
        //指定缩放出来的最大的宽度(也有可能是高度)
        $max=$mySize;
        //根据最大值为300,算出另一个边的长度,得到缩放后的图片宽度和高度
        $h=$myHeight;
        $w=$max;
        $path = DOC_ROOT.'upload/detele';
       if(!file_exists($path)) {
              mkdir($path, 0777, true);
           }
        //声明一个$w宽,$h高的真彩图片资源
        $image=imagecreatetruecolor($w, $h);
        //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
        imagecopyresampled($image, $src, 0, 0, 0, 0, $w, $h, $size_src['0'], $size_src['1']);
        return $image; 
    }

已测试。生成速度很快。

你可能感兴趣的:(php,TP5,生成海报,缩放图片,圆角图片)