Blogger Information
Blog 55
fans 0
comment 0
visits 59434
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 使用 GD 生成推广图
南鸢离梦的博客
Original
981 people have browsed it

// GD生成推广图
private function CreateTg($user)
{
$bg = Db::name(‘bg’)->find();
//图片使用本地的绝对路径

  1. $dst_path = '1.jpg';//背景图
  2. $src_path= '2.gpg'; //二维码
  3. // 获取图片后缀
  4. $hz = substr(strrchr($dst_path, '.'), 1);
  5. // 保存的本地绝对路径文件夹
  6. $path = getcwd().'/Tg/';
  7. //生成新图片名
  8. $image = $path.$user['openid'].".".$hz;
  9. //创建图片的实例
  10. $dst = imagecreatefromstring(file_get_contents($dst_path));
  11. $src = imagecreatefromstring(file_get_contents($src_path));
  12. //获取二维码图片的宽高
  13. list($src_w,$src_h) = getimagesize($src_path);
  14. // 自定义缩放二维码图片的宽高
  15. $new_src_w =$src_w *0.2 ; $new_src_h=$src_h*0.2;
  16. // 合并图片
  17. imagecopyresampled($dst, $src, 155,850, 0, 0, $new_src_w, $new_src_h,$src_w, $src_h);
  18. // 引入字体文件,服务器没有字体文件的可以去window电脑里面控制面板->个性化里找到字体文件复制到服务器
  19. $fonts = getcwd().'/MSYH.TTF';
  20. $col = imagecolorallocatealpha($dst,0,0,0,0);
  21. imagettftext($dst,12,0,245,890,$col,$fonts,$user['nickname']);
  22. imagettftext($dst,12,0,245,920,$col,$fonts,'邀请您加入津门读书会');
  23. //获取合成图片的宽高
  24. list($src_w, $src_h, $dst_type) = getimagesize($dst_path);
  25. //保存图片
  26. switch ($dst_type) {
  27. case 1://GIF
  28. imagegif($dst, $image);
  29. //保存后,销毁
  30. imagedestroy($dst);
  31. imagedestroy($src);
  32. break;
  33. case 2://JPG
  34. imagejpeg($dst,$image);
  35. imagedestroy($dst);
  36. imagedestroy($src);
  37. break;
  38. case 3://PNG
  39. imagepng($dst, $image);
  40. imagedestroy($dst);
  41. imagedestroy($src);
  42. break;
  43. default:
  44. break;
  45. }

}

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post