分享

生成二维码 合成图片 添加文字

 小马哥技术屋 2017-11-08
Loader::import('phpqrcode.phpqrcode',VENDOR_PATH);//导入qrcode类
$level=3;
$size=4;
$errorCorrectionLevel =intval($level) ;//容错级别
$matrixPointSize = intval($size);//生成图片大小
$data = [$meeting_id.',3,'.$openid];
$value = json_encode($data);
$object = new \QRcode();
$filename = time().rand(10,99).'.png';
$filepath = 'static/uploads/qrcode/'.$filename;
$object::png($value, $filepath, $level = QR_ECLEVEL_L, $size = 3, $margin = 4,$saveandprint=false); //生成的二维码 既logo图片
//说明:如果要把生成的二维码保存到某位置,最后一个参数必要要有。
$logo =$filepath;//准备好的logo图片
$QR = $yimg;//原图像
if ($logo !== FALSE) {
  $QR = imagecreatefromstring(file_get_contents($QR));//读取原图像
  $logo = imagecreatefromstring(file_get_contents($logo));//读取二维码图像
  $QR_width = imagesx($QR);//原图像宽度
  $QR_height = imagesy($QR);//原图像高度
  $logo_width = imagesx($logo);//二维码图片宽度
  $logo_height = imagesy($logo);//二维码图片高度
  $logo_qr_width = $QR_width / 4; //二维码的宽度
  $scale = $logo_width/$logo_qr_width; //比例
  $logo_qr_height = $logo_height/$scale; //二维码高度
  $from_width = ($QR_width - $logo_qr_width) / 2; //距离左边距的宽度
  $from_height = ($QR_height - 300); //距离左边距的宽度
  //重新组合图片并调整大小
  imagecopyresampled($QR, $logo, $from_width, $from_height, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height);
}
//输出图片
$lasefilename = time().rand(100,999).'.png';//原图
imagepng($QR, 'static/uploads/qrcode/'.$lasefilename);
unlink($filepath);//删除原图片
//将微信头像与其合成图片
 $headlogo = time().rand(100,999).'.png';//logo
 $image = file_get_contents($user_img); //获取微信头像
 file_put_contents('static/uploads/qrcode/'.$headlogo, $image);//微信头像生成图片
 $yheadlogo = 'static/uploads/qrcode/'.$headlogo;
 //将头像处理成圆形
 $ename=getimagesize($yheadlogo);
 $ename=explode('/',$ename['mime']);
 $ext=$ename[1];
 $src_img = null;
 switch ($ext) {
    case 'jpg':
     $src_img = imagecreatefromjpeg($yheadlogo);
     break;
    case 'jpeg':
     $src_img = imagecreatefromjpeg($yheadlogo);
     break;
   case 'png':
    $src_img = imagecreatefrompng($yheadlogo);
     break;
}      
$wh  = getimagesize($yheadlogo);
$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);
    }
  }
}
$finalhead = time().rand(100,999).'.png';//logo
imagepng($img, 'static/uploads/qrcode/'.$finalhead);
//删除原微信头像
unlink($yheadlogo);
$wxlogo = 'static/uploads/qrcode/'.$finalhead;
$QR2 = 'static/uploads/qrcode/'.$lasefilename;//原图
if ($wxlogo !== FALSE) {
  $QR2 = imagecreatefromstring(file_get_contents($QR2));//读取原图像
  $wxlogo = imagecreatefromstring(file_get_contents($wxlogo));//读取二维码图像
  $QR2_width = imagesx($QR2);//原图像宽度
  $QR2_height = imagesy($QR2);//原图像高度
  $wxlogo_width = imagesx($wxlogo);//二维码图片宽度
  $wxlogo_height = imagesy($wxlogo);//二维码图片高度
  $wxlogo_qr_width = $QR2_width / 4; //二维码的宽度
  $scale1 = $wxlogo_width/$wxlogo_qr_width; //比例
  $wxlogo_qr_height = $wxlogo_height/$scale1; //二维码高度
  $from1_width = ($QR2_width - $wxlogo_qr_width) / 2; //距离左边距的宽度
  $from1_height = ($QR2_height - 725); //距离上边距的高度
  //重新组合图片并调整大小
  imagecopyresampled($QR2, $wxlogo, $from1_width, $from1_height, 0, 0, $wxlogo_qr_width,$wxlogo_qr_height, $wxlogo_width, $wxlogo_height);
}
//输出图片
 $finalimage = time().rand(10,99).'.png';//原图
 imagepng($QR2, 'static/uploads/qrcode/'.$finalimage);
 //删除原拼装的图片
 unlink('static/uploads/qrcode/'.$finalhead);//删除处理过的微信头像
 unlink('static/uploads/qrcode/'.$lasefilename);//删除瓶装后的图片
 $file = 'static/uploads/qrcode/'.$finalimage;
 $fontfile = '../vendor/topthink/think-captcha/assets/zhttfs/1.ttf';//文本字体-微软雅黑
 $head = '邀请您参加';//头部
 $title = $meetinginfo['title'];
 $start_time = date('Y/m/d H:i:s',$meetinginfo['meeting_start']);
 $end_time = date('Y/m/d H:i:s',$meetinginfo['meeting_end']);
 $time = '时间: '.$start_time.'~'.$end_time;
 $address = '地址: '.$meetinginfo['province'].$meetinginfo['city'].$meetinginfo['country'].$meetinginfo['address'];
 //获取图片信息
 $info = getimagesize($file);
 //获取图片扩展名
 $type = image_type_to_extension($info[2],false);
 //动态的把图片导入内存中
 $fun = "imagecreatefrom{$type}";
 $image = $fun($file);
 //指定字体颜色
 //指定字体内容
 //给图片添加文字
 $color = array(255,243,173,127);//最后一个参数是透明度,127是完全透明
 $fontsize = 20;
 $col = imagecolorallocatealpha($image,$color[0],$color[1],$color[2],$color[3]);
 $hcolor = imagecolorallocate($image,255,243,173); // 文字颜色
 imagettftext($image,20,0,270,620,$hcolor,$fontfile,$head);//写入邀请
 imagettftext($image,20,0,180,660,$hcolor,$fontfile,$title);//写入邀请
 imagettftext($image,16,0,80,700,$hcolor,$fontfile,$time);//写入邀请
 imagettftext($image,16,0,80,740,$hcolor,$fontfile,$address);//写入邀请
//imagettftext参数:第一个参数是字体大小,第二个是旋转角度,第三个是横坐标,第四个是纵坐标。
 $perferimg = time().rand(100,999).'.png';
 imagepng($image, 'static/uploads/qrcode/'.$perferimg);
 unlink($file);
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多