/** * 生成二维码 * * @param int $id * @param string $file * @param boolean $is_download */ public function qrcode($id, $file = '', $is_download = false) { //跳转地址 $url = 'http://local.xxx.net/fgdgdgf'; // 纠错级别:L、M、Q、H $errorCorrectionLevel = 'L'; // 点的大小:1到10 $matrixPointSize = 10; $qrcode = QRcode::png($url, false, $errorCorrectionLevel, $matrixPointSize, 2); //获取会议室信息和二维码code $room=$this->_meetingRoomServ->get($id); //创建背景,并将二维码贴到左边 $bk = imagecreate(750, 470); imagecolorallocate($bk, 255, 255, 255); imagecopy($bk, $qrcode, 0, 0, 0, 0, 430, 430); //设置字体颜色 $black = imagecolorallocate($bk, 0, 0, 0); // 字体 $font = APP_PATH . "../www/YaHei.ttf"; // 写入文字 $arr = $this->mbstringtoarray($room['mr_address'],"utf-8"); $mr_address=$this->_getstr($arr); // 会议室地点 $arr_name = $this->mbstringtoarray($room['mr_name'],"utf-8"); $mr_name=$this->_getstr($arr_name); // 区域 $arr_mr_device = $this->mbstringtoarray($room['mr_device'],"utf-8"); $mr_device=$this->_getstr($arr_mr_device,18); imagettftext($bk, 20, 0, 410, 40, $black, $font, $mr_name); imagettftext($bk, 12, 0, 410, 70, $black, $font, '会议室地点:' . $mr_address); imagettftext($bk, 12, 0, 410, 90, $black, $font, '会议室区域:' .$room['mr_area']); imagettftext($bk, 12, 0, 410, 150, $black, $font, '预订时间:' . $room['mr_timestart'] . '-' . $room['mr_timeend']); imagettftext($bk, 12, 0, 410, 190, $black, $font, '容纳人数:' . $room['mr_galleryful']); imagettftext($bk, 12, 0, 410, 240, $black, $font, '可用设备:' .$mr_device); if ($file) { //生成文件 imagepng($bk, $file); } else if ($is_download) { //直接下载 Header("Content-type: application/octet-stream"); Header("Accept-Ranges: bytes"); Header("Accept-Length:6000"); Header("Content-Disposition: attachment; filename={$room['mr_address']}-{$room['mr_name']}.png"); imagepng($bk); } else { //直接输出图片 header('Content-Type: image/png'); imagepng($bk); } } /** * 将字符拆份成段 * @param $str * @param $charset * @return array */ protected function mbstringtoarray($str,$charset) { $array=array(); $strlen=mb_strlen($str); while($strlen){ $array[]=mb_substr($str,0,1,$charset); $str=mb_substr($str,1,$strlen,$charset); $strlen=mb_strlen($str); } return $array; } /** * 拼接超出字符串 * @param $arr * @return string */ protected function _getstr($arr,$leng=10){ $temp=''; for($i=0;$i0) { $temp=$temp."\n"; } $temp=$temp.$arr[$i]; } return $temp; } 引入qrcode文件就可以生成如下的二维码图片
注意:$font这里字体文件路径要使用绝对路径,不能使用相对路径,否则右边的字会出不来。