1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > PHP使用phpqrcode生成带LOGO或文字的二维码

PHP使用phpqrcode生成带LOGO或文字的二维码

时间:2020-01-27 07:36:02

相关推荐

PHP使用phpqrcode生成带LOGO或文字的二维码

一、加入文字的代码示例:

//如果没有文件夹 则自动创建$nowDay = date('Y-m-d');if(!is_dir($_SERVER['DOCUMENT_ROOT'].$path.$nowDay)){mkdir($_SERVER['DOCUMENT_ROOT'].$path.$nowDay,0777,true);}$file_path = $path.$nowDay.'/';//打样通知单二维码include_once('../app/Extensions/phpqrcode/phpqrcode.php');//二维码的内容 打样通知单的ID$bases64 = $no;//容错级别$errorCorrentionLevel = 'L';//生成图片大小$matrixPoinSize = 12;//生成二维码$file_id = base64_decode($no);$filename = $file_path.time().'_gourp_'.$file_id.'.png';\QRcode::png($bases64,'.'.$filename,$errorCorrentionLevel,$matrixPoinSize,2);//追加文字$image = imagecreatefrompng('.'.$filename);$font = $_SERVER['DOCUMENT_ROOT'].'/fonts/simhei.ttf'; // 字体文件 要上传到/public/fonts目录下$color = imagecolorallocate($image,0,0,0); // 文字颜色imagettftext($image, 16, 0, 100, 295, $color, $font, '编码: - 卧槽编码123456'); // 创建文字imagepng($image, '.'.$filename);//保存新生成的imagedestroy($image);//删除原来的图片return $filename;

二、加入LOGO的代码示例

include 'phpqrcode.php';$url = '需要跳转到的URL地址'; //二维码内容$errorCorrectionLevel = 'H'; //容错级别$matrixPointSize = 5; //生成图片大小ob_start();$QRcode::png($url, false, $errorCorrectionLevel, $matrixPointSize, 1);$ob_contents = ob_get_contents(); //读取缓存区数据ob_end_clean();`$myImage = ImageCreate(245,245); //参数为宽度和高度$qr = imagecreatefromstring($ob_contents);`$logo = 'logo.png'; //需要显示在二维码中的Logo图像$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, 'qr_logo.png' ); //带Logo二维码的文件名

或者

include 'phpqrcode.php'; //引入phpqrcode类文件(路径要对) 官网下载引用$value = ''; //二维码内容 可以文字、连接 都行$errorCorrectionLevel = 'L';//容错级别容错级别L(7%)、M(15%)、Q(25%)、H(30%) 容错级别越高 越容易扫描$matrixPointSize = 6;//生成图片大小//生成二维码图片QRcode::png($value, 'qrcode.png', $errorCorrectionLevel, $matrixPointSize, 2);$logo = 'as.jpg';//准备好的logo图片 需要加入到二维码中的logo$QR = 'qrcode.png';//已经生成的原始二维码图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图片宽度$logo_height = imagesy($logo);//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, 'helloweba.png'); //已经在本地生成

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。