用phpqrcode產生二維碼並加上logo,為啥最後產生出來的LOGO變成黑白了?

來源:互聯網
上載者:User

代碼如下

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); // 新加這個。

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.