// 方法一:使用PHP QR Code类库创建二维码
<?php
include "phpqrcode/phpqrcode.php";
$value="http://www.baidu.com"; // 生成的源代码
$errorCorrectionLevel = "L"; // 错误率
$matrixPointSize = "20"; // 大小
if (isset($_GET['logo'])) {
$logo = 'firefox.png'; // 二维码中间的logo
$QR = 'baidu_map.png'; // 目标生成的二维码
QRcode::png($value, 'baidu_map.png', $errorCorrectionLevel, $matrixPointSize, 2);
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,'baidu_map.png');
}else {
$file_name = "baidu.jpg"; // 是否生成图片, false 输出到浏览器
QRcode::png($value, $file_name, $errorCorrectionLevel, $matrixPointSize);
}
?>
// 方法二:Google提供了较为完善的二维码生成接口,调用API接口(本人测试未成功)
<?php
header("Content-Type:text/html;charset=utf-8");
if (isset($_GET['content'])) {
$content = $_GET['content'];
$width = $_GET['width']?$_GET['width']:300;
$height = $_GET['height']?$_GET['height']:300;
echo "你输入的内容是: <span>$content </span><BR />";
echo "你选择的宽度是: <span>$width </span><BR />";
echo "你选择的高度是: <span>$height </span><BR /> ";
echo "生成的二维码图像是:<BR /> ";
$wen = urlencode(mb_convert_encoding($content, 'utf-8', 'gb2312'))."";
echo "<img id=qrcode_img src=https://chart.googleapis.com/chart?cht=qr&chld=H&chs={$width}x{$height}&chl={$wen} /><br />
图片地址:<a href='https://chart.googleapis.com/chart?cht=qr&chld=H&chs={$width}x{$height}&chl={$wen}' target='_blank'>点击跳转</a> ";
}
?>
<form action="" method="get" οnsubmit="return post();">
<h1>二维码生成工具</h1>
<p>
width:
<select name="width">
<option value="100">100</option>
<option value="200">200</option>
<option value="300" Selected>300</option>
<option value="400">400</option>
<option value="500">500</option>
</select>
height:
<select name="height">
<option value="100">100</option>
<option value="200">200</option>
<option value="300" Selected>300</option>
<option value="400">400</option>
<option value="500">500</option>
</select>
</p>
输入网址或者文字:<br />
<textarea rows="5" cols="30" name="content" id="content" ></textarea><br /><br />
<input type="submit" value="生成图片" />
</form>