代碼如下
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; //重新組合圖片並調整大小 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 '';
回複內容:
代碼如下
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; //重新組合圖片並調整大小 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 '';
因為你用url擷取的遠端的映像是一個jpeg映像。也就是所謂truecolor的映像。而png是所謂的調色盤映像,
需要先將 logo變成調色盤映像,才能在copy時不丟失 顏色資訊
代碼如下:
$logo = imagecreatefromstring(file_get_contents($logo));
if (imageistruecolor($logo)) imagetruecolortopalette($logo, false, 65535); // 新加這個。