400-800-9385
网站建设资讯详细

php网站制作如何生成二维码

发表日期:2020-12-15 09:04:27   作者来源:刘红旺   浏览:2143   标签:php网站制作    
第一种方法找网上的api接口
 比如:联图

品味二维码:
 


草料:


其他都可以在网上搜到很多,但是这些api可能过一段时间会失效,以前就遇到过这样的情况,这就接口不可控性太大,所以我们要介绍第二种方法
二、用插件生成二维码
这里使用的是PHPQRcode 新建函数(这里用的thinkPhp框架)
 /**
 * 生成二维码
 * @param $save_path 二维码保存路径
 * @param string $qr_data 手机扫描后要跳转的网址
 * @param string $qr_level 默认纠错比例 分为L、M、Q、H四个等级,H代表最高纠错能力
 * @param int $qr_size 二维码图大小,1-10可选,数字越大图片尺寸越大
 * @param string $save_prefix 图片名称前缀
 * @return bool|string
 */
function createQRcode($save_path, $qr_data = 'PHP QR Code :)', $qr_level = 'L', $qr_size = 4, $save_prefix = 'qrcode') {
  if (!isset($save_path)) return '';
  //设置生成png图片的路径
  $PNG_TEMP_DIR = & $save_path;
  //导入二维码核心程序
  vendor('PHPQRcode.class#phpqrcode'); //PHPQRcode是文件夹名字,class#phpqrcode就代表class.phpqrcode.php文件名
  //检测并创建生成文件夹
  if (!file_exists($PNG_TEMP_DIR)) {
      mkdir($PNG_TEMP_DIR);
  }
  $filename = $PNG_TEMP_DIR . 'test.png';
  $errorCorrectionLevel = 'L';
  if (isset($qr_level) && in_array($qr_level, array('L', 'M', 'Q', 'H'))) {
      $errorCorrectionLevel = & $qr_level;
  }
  $matrixPointSize = 4;
  if (isset($qr_size)) {
      $matrixPointSize = & min(max((int)$qr_size, 1), 10);
  }
  if (isset($qr_data)) {
      if (trim($qr_data) == '') {
          die('data cannot be empty!');
      }
      //生成文件名 文件路径+图片名字前缀+md5(名称)+.png
      $filename = $PNG_TEMP_DIR . $save_prefix . md5($qr_data . '|' . $errorCorrectionLevel . '|' . $matrixPointSize) . '.png';
      //开始生成
      QRcode::png($qr_data, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
  } else {
      //默认生成
      QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2);
  }
  if (file_exists($PNG_TEMP_DIR . basename($filename))) {
      return basename($filename);
  } else {
      return FALSE;
  }
}
 
函数调用
$save_path = 'Qrcode/'; //图片存储的绝对路径
$qr_data = C('PROTOCOL') . $_SERVER['SERVER_NAME'] .  . '/Share/member.html?str=' . $member['mstr'];
$qr_level = 'H';
$qr_size = '10';
$save_prefix = 'ZETA';
$filename = createQRcode($save_path, $qr_data, $qr_level, $qr_size, $save_prefix);
if ($filename) {
    $pic =  . '/' . $save_path . $filename;
}
$this->pic = $pic;
 
如何在利用PHPQRcode生成的二维码中间加上logo图片呢
$logo =  . $member['picture']; //logo的图片地址
import("Org.Util.File");
$file = new \File($logo);
$logo = $file->getRealFile();
$file = new \File($pic);
$QR = $file->getRealFile();; //二维码图片地址
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 / 5;
    $scale = $logo_width / $logo_qr_width;
    $logo_qr_height = $logo_height / $scale;
    $from_width = ($QR_width - $logo_qr_width) / 2;
    imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
}
imagepng($QR, $save_path . $filename); //跟logo合并之后的地址
 
如没特殊注明,文章均为方维网络原创,转载请注明来自https://www.szfangwei.cn/news/5919.html