步骤:一、在网址http://phpqrcode.sourceforge.net/中下载phpqrcode.php文件,然后放到自己的项目中去;
二、编写代码并引入phpqrcode.php文件,实现生成二维码。
代码:
一、phpqrcode.php文件(下载即可)
二、测试代码(erweima.app.php)
display('erweima.html'); } /** * @param string $chl 二维码包含的信息,可以是数字、字符、二进制信息、汉字。 不能混合数据类型,数据必须经过utf-8 url-encoded * @param int $widhtheight 生成二维码的尺寸设置 * @param string $ec_level 可选纠错级别,qr码支持四个等级纠错,用来恢复丢失的、读错的、模糊的、数据。 * l-默认:可以识别已损失的7%的数据 * m-可以识别已损失15%的数据 * q-可以识别已损失25%的数据 * h-可以识别已损失30%的数据 * @param int $margin 生成的二维码离图片边框的距离 */ function credit_qrcode() { include '/includes/libraries/phpqrcode.php'; $value = isset($_post['url']) ? $_post['url'] : 'http://www.baidu.com'; //上传图片 if (isset($_files['image']) && $_files['image']['error'] == 0 ){ $image = $this->_upload_file('image', 'erweima/', date('ymdhis') . mt_rand(1000, 9999), 'index.php?app=credit&act=credit_qrcode'); if ($image){ $logo = $image; } } else { $logo = site_url . '/themes/mall/default/styles/default/images/001.jpg';//准备好的logo图片 } $errorcorrectionlevel = 'h';//容错级别 $matrixpointsize = 8;//生成图片大小 //生成二维码图片 qrcode::png($value, 'qrcode.png', $errorcorrectionlevel, $matrixpointsize, 2); $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'); echo ''; } /** * 上传文件 * @return mix false表示上传失败,空串表示没有上传,string表示上传文件地址 * $file_name 为上传文件name * $path_name 为上传路径 * $save_name 为保存文件名 * $ret_url 为回调url **/ function _upload_file($file_name, $path_name, $save_name, $ret_url = 'index.php') { $file = $_files[$file_name]; $message = array( '1' => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。', '2' => '上传文件的大小超过了 html 表单中 max_file_size 选项指定的值。', '3' => '文件只有部分被上传。' ); switch ($file['error']) { case upload_err_ini_size: case upload_err_form_size: case upload_err_partial: if ($ret_url) { $this->show_warning($message[$file['error']], 'go_back'); return false; } else { return array('done' => false, 'msg' => $message[$file['error']]); } break; } if ($file['error'] != upload_err_ok) { return ''; } import('uploader.lib'); $uploader = new uploader(); $uploader->allowed_type(image_file_type); $uploader->addfile($file); if ($uploader->file_info() === false) { if ($ret_url) { $this->show_warning($uploader->get_error(), 'go_back', $ret_url); return false; } else { return array('done' => false, 'msg' => $uploader->get_error()); } } $uploader->root_dir(root_path); return $uploader->save('data/files/mall/'.$path_name, $save_name); } }
三、模板文件(erweima.html)
请输入网址:
图片上传: