生成微信小程序二维码的代码(注意:ROOT_PATH是本地绝对路径,请勿使用url)

注意:如果二维码是动态生成本地,要注意文件权限!!!

  1. public static function createWxQrcode($params)
  2. {
  3. //配置APPID、APPSECRET
  4. $APPID = "";
  5. $APPSECRET = "";
  6. //获取access_token
  7. $access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
  8. //缓存access_token
  9. session_start();
  10. $_SESSION['access_token'] = "";
  11. $_SESSION['expires_in'] = 0;
  12. if (!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() > $_SESSION['expires_in'])) {
  13. $json = httpRequest($access_token, 'POST');
  14. $json = json_decode($json, true);
  15. // var_dump($json);
  16. $_SESSION['access_token'] = $json['access_token'];
  17. $_SESSION['expires_in'] = time() + 7200;
  18. $ACCESS_TOKEN = $json["access_token"];
  19. } else {
  20. $ACCESS_TOKEN = $_SESSION["access_token"];
  21. }
  22. //构建请求二维码参数
  23. //path是扫描二维码跳转的小程序路径,可以带参数?id=xxx
  24. //width是二维码宽度
  25. $qcode = "https://api.weixin.qq.com/wxa/getwxacode?access_token=$ACCESS_TOKEN";
  26. $param = json_encode(array("path" => "pages/routeDetail/index?id={$params['line_id']}&recom_uid={$params['user_id']}", "width" => 100));
  27. //POST参数
  28. $result = httpRequest($qcode, "POST", $param);
  29. $filename = ROOT_PATH . 'public/uploads/qrcode/' . $params['line_id'] . '_' . $params['user_id'] . '_qrcode_wx.png';
  30. //生成二维码
  31. file_put_contents($filename, $result);
  32. $image = '/uploads/qrcode/' . $params['line_id'] . '_' . $params['user_id'] . '_qrcode_wx.png';
  33. $base64_image = "data:image/jpeg;base64," . base64_encode($result);
  34. #echo $base64_image;
  35. return $image;
  36. }

PHP合并图片生成海报的代码

  1. public static function createMiniWechat($params)
  2. {
  3. $config = array(
  4. 'image' => array(
  5. array(
  6. 'url' => ROOT_PATH . 'public' . DS . $params['wx_image'], //二维码地址
  7. 'is_yuan' => true, //true图片圆形处理
  8. 'stream' => 0,
  9. 'left' => 280, //小于0为小平居中
  10. 'top' => 400,
  11. 'right' => 0,
  12. 'width' => 100, //图像宽
  13. 'height' => 100, //图像高
  14. 'opacity' => 100 //透明度
  15. ),
  16. array(
  17. 'url' => $image_arr[0], //素材地址
  18. 'is_yuan' => false, //true图片圆形处理
  19. 'stream' => 0,
  20. 'left' => 0, //小于0为小平居中
  21. 'top' => 10,
  22. 'right' => 0,
  23. 'width' => 400, //图像宽
  24. 'height' => 250, //图像高
  25. 'opacity' => 100 //透明度
  26. ),
  27. ),
  28. 'text' => array(
  29. array(
  30. 'text' => $lineInfo->title, //文字内容
  31. 'left' => 10, //小于0为小平居中
  32. 'top' => 290,
  33. 'fontSize' => 14, //字号
  34. 'fontColor' => '0,0,0', //字体颜色
  35. 'angle' => 0,
  36. 'fontPath' => ROOT_PATH . 'public/assets/fonts/fzltxh.ttf', //字体文件
  37. ),
  38. array(
  39. 'text' => $date, //文字内容
  40. 'left' => 10, //小于0为小平居中
  41. 'top' => 340,
  42. 'fontSize' => 12, //字号
  43. 'fontColor' => '169,169,169', //字体颜色
  44. 'angle' => 0,
  45. 'fontPath' => ROOT_PATH . 'public/assets/fonts/fzltxh.ttf', //字体文件
  46. ),
  47. array(
  48. 'text' => '¥' . $price, //文字内容
  49. 'left' => 10, //小于0为小平居中
  50. 'top' => 365,
  51. 'fontSize' => 14, //字号
  52. 'fontColor' => '255,20,147', //字体颜色
  53. 'angle' => 0,
  54. 'fontPath' => ROOT_PATH . 'public/assets/fonts/fzltxh.ttf', //字体文件
  55. ),
  56. array(
  57. 'text' => '长按识别小程序码访问', //文字内容
  58. 'left' => 10, //小于0为小平居中
  59. 'top' => 435,
  60. 'fontSize' => 14, //字号
  61. 'fontColor' => '0,0,0', //字体颜色
  62. 'angle' => 0,
  63. 'fontPath' => ROOT_PATH . 'public/assets/fonts/fzltxh.ttf', //字体文件
  64. ),
  65. array(
  66. 'text' => '全新体验', //文字内容
  67. 'left' => 10, //小于0为小平居中
  68. 'top' => 470,
  69. 'fontSize' => 13, //字号
  70. 'fontColor' => '169,169,169', //字体颜色
  71. 'angle' => 0,
  72. 'fontPath' => ROOT_PATH . 'public/assets/fonts/fzltxh.ttf', //字体文件
  73. )
  74. ),
  75. 'background' => ROOT_PATH . 'public/assets/img/bj.jpg', //背景图
  76. );
  77. $filename = ROOT_PATH . 'public/uploads/qrcode/' . $params['line_id'] . '_' . $params['user_id'] . '_qrcode.png';
  78. //echo createPoster($config);
  79. //$filename为空是真接浏览器显示图片
  80. $rest = createPoster1($config, $filename);
  81. if ($rest) {
  82. $image = '/uploads/qrcode/' . $params['line_id'] . '_' . $params['user_id'] . '_qrcode.png';
  83. db('qrcode_record')->insert(['user_id' => $params['user_id'], 'line_id' => $params['line_id'], 'image' => $image, 'create_time' => time()]);
  84. return $image;
  85. }
  86. return false;
  87. }

合成宣传海报

  1. /**
  2. * 生成宣传海报
  3. * @param array 参数,包括图片和文字
  4. * @param string $filename 生成海报文件名,不传此参数则不生成文件,直接输出图片
  5. * @return [type] [description]
  6. */
  7. function createPoster1($config = array(), $filename = "")
  8. {
  9. //如果要看报什么错,可以先注释调这个header
  10. //if(empty($filename)) header("content-type: image/png");
  11. if (empty($filename)) header("content-type: image/png");
  12. $imageDefault = array(
  13. 'left' => 0,
  14. 'top' => 0,
  15. 'right' => 0,
  16. 'bottom' => 0,
  17. 'width' => 100,
  18. 'height' => 100,
  19. 'opacity' => 100
  20. );
  21. $textDefault = array(
  22. 'text' => '',
  23. 'left' => 0,
  24. 'top' => 0,
  25. 'fontSize' => 32, //字号
  26. 'fontColor' => '255,255,255', //字体颜色
  27. 'angle' => 0,
  28. );
  29. $background = $config['background']; //海报最底层得背景
  30. //背景方法
  31. $backgroundInfo = getimagesize($background);
  32. $backgroundFun = 'imagecreatefrom' . image_type_to_extension($backgroundInfo[2], false);
  33. $background = $backgroundFun($background);
  34. $backgroundWidth = imagesx($background); //背景宽度
  35. $backgroundHeight = imagesy($background); //背景高度
  36. $imageRes = imageCreatetruecolor($backgroundWidth, $backgroundHeight);
  37. $color = imagecolorallocate($imageRes, 0, 0, 0);
  38. imagefill($imageRes, 0, 0, $color);
  39. imagecopyresampled($imageRes, $background, 0, 0, 0, 0, imagesx($background), imagesy($background), imagesx($background), imagesy($background));
  40. //处理了图片
  41. if (!empty($config['image'])) {
  42. foreach ($config['image'] as $key => $val) {
  43. $val = array_merge($imageDefault, $val);
  44. $info = getimagesize($val['url']);
  45. $function = 'imagecreatefrom' . image_type_to_extension($info[2], false);
  46. if ($val['stream']) { //如果传的是字符串图像流
  47. $info = getimagesizefromstring($val['url']);
  48. $function = 'imagecreatefromstring';
  49. }
  50. $res = $function($val['url']);
  51. $resWidth = $info[0];
  52. $resHeight = $info[1];
  53. //建立画板 ,缩放图片至指定尺寸
  54. $canvas = imagecreatetruecolor($val['width'], $val['height']);
  55. imagefill($canvas, 0, 0, $color);
  56. //如果是透明的gif或png做透明处理
  57. $ext = pathinfo($val['url']);
  58. if (array_key_exists('extension', $ext)) {
  59. if ($ext['extension'] == 'gif' || $ext['extension'] == 'png') {
  60. imageColorTransparent($canvas, $color); //颜色透明
  61. }
  62. }
  63. //关键函数,参数(目标资源,源,目标资源的开始坐标x,y, 源资源的开始坐标x,y,目标资源的宽高w,h,源资源的宽高w,h)
  64. imagecopyresampled($canvas, $res, 0, 0, 0, 0, $val['width'], $val['height'], $resWidth, $resHeight);
  65. //$val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']) - $val['width']:$val['left'];
  66. //如果left小于-1我这做成了计算让其水平居中
  67. if ($val['left'] < 0) {
  68. $val['left'] = ceil($backgroundWidth - $val['width']) / 2;
  69. }
  70. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) - $val['height'] : $val['top'];
  71. //放置图像
  72. imagecopymerge($imageRes, $canvas, $val['left'], $val['top'], $val['right'], $val['bottom'], $val['width'], $val['height'], $val['opacity']); //左,上,右,下,宽度,高度,透明度
  73. }
  74. }
  75. //处理文字
  76. if (!empty($config['text'])) {
  77. foreach ($config['text'] as $key => $val) {
  78. $val = array_merge($textDefault, $val);
  79. list($R, $G, $B) = explode(',', $val['fontColor']);
  80. $fontColor = imagecolorallocate($imageRes, $R, $G, $B);
  81. //$val['left'] = $val['left']<0?$backgroundWidth- abs($val['left']):$val['left'];
  82. //如果left小于-1我这做成了计算让其水平居中
  83. $text = autowrap($val['fontSize'], 0, $val['fontPath'], $val['text'], 390);
  84. if ($val['left'] < 0) {
  85. $fontBox = imagettfbbox($val['fontSize'], 0, $val['fontPath'], $text); //文字水平居中实质
  86. $val['left'] = ceil(($backgroundWidth - $fontBox[2]) / 2); //计算文字的水平位置
  87. }
  88. $val['top'] = $val['top'] < 0 ? $backgroundHeight - abs($val['top']) : $val['top'];
  89. imagettftext($imageRes, $val['fontSize'], $val['angle'], $val['left'], $val['top'], $fontColor, $val['fontPath'], $text);
  90. }
  91. }
  92. //生成图片
  93. if (!empty($filename)) {
  94. $res = imagejpeg($imageRes, $filename, 90); //保存到本地
  95. imagedestroy($imageRes);
  96. if (!$res) return false;
  97. return $filename;
  98. } else {
  99. header("Content-type:image/png");
  100. imagejpeg($imageRes); //在浏览器上显示
  101. imagedestroy($imageRes);
  102. }
  103. }
  1. function autowrap($fontsize, $angle, $fontface, $string, $width)
  2. {
  3. // 这几个变量分别是 字体大小, 角度, 字体名称, 字符串, 预设宽度
  4. $content = "";
  5. // 将字符串拆分成一个个单字 保存到数组 letter 中
  6. for ($i = 0; $i < mb_strlen($string); $i++) {
  7. $letter[] = mb_substr($string, $i, 1);
  8. }
  9. foreach ($letter as $l) {
  10. $teststr = $content . " " . $l;
  11. $testbox = imagettfbbox($fontsize, $angle, $fontface, $teststr);
  12. // 判断拼接后的字符串是否超过预设的宽度
  13. if (($testbox[2] > $width) && ($content !== "")) {
  14. $content .= "\n";
  15. }
  16. $content .= $l;
  17. }
  18. return $content;
  19. }

fzltxh.rar

效果图:

image.png