代碼如下
include_once WEB_ROOT_PATH.'/classes/phpqrcode.php';
//set it to writable location, a place for temp generated PNG files
$PNG_TEMP_DIR = WEB_ROOT_PATH.DIRECTORY_SEPARATOR.'userTgCache'.DIRECTORY_SEPARATOR;
//html PNG location prefix
$PNG_WEB_DIR = '/userTgCache/';
if (!file_exists($PNG_TEMP_DIR))
mkdir($PNG_TEMP_DIR);
$filename = $PNG_TEMP_DIR.'test.png';
$errorCorrectionLevel = 'H';
$matrixPointSize = 10;
QRcode::png("http://wwwww.xxxx.com/weixin/testXXXX", $filename, $errorCorrectionLevel, $matrixPointSize, 2);
$QR = $filename;
$logo = "http://wx.qlogo.cn/mmopen/LIUI5tJGiauCMgcRkLbYk7VfUaTdzcbTiaASdCMKj1rFicYTPyL0qibONtUuF9MQmpfRvABlPqJ2xsUbiaSL0q3t6iaF357Vg1PW7U/0";
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 / 2.5;
$scale = $logo_width/$logo_qr_width;
$logo_qr_height = $logo_height/$scale;
$from_width = ($QR_width - $logo_qr_width) / 2;
//重新組合圖片并調(diào)整大小
imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,
$logo_qr_height, $logo_width, $logo_height);
}
//display generated file
imagepng($QR, $filename);
echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>';
人生最曼妙的風景,竟是內(nèi)心的淡定與從容!
因為你用url獲取的遠端的圖像是一個jpeg圖像。也就是所謂truecolor的圖像。而png是所謂的調(diào)色板圖像,
需要先將 logo變成調(diào)色板圖像,才能在copy時不丟失 顏色信息
代碼如下:
$logo = imagecreatefromstring(file_get_contents($logo));
if (imageistruecolor($logo)) imagetruecolortopalette($logo, false, 65535); // 新加這個。