Home > Backend Development > PHP Tutorial > phpqrcode logo为什么变成黑白了

phpqrcode logo为什么变成黑白了

PHPz
Release: 2020-09-04 16:15:38
Original
2644 people have browsed it

phpqrcode logo变成黑白是因为url获取的远端的图像是一个jpeg图像,也就是所谓truecolor的图像,而png是所谓的调色板图像,其解决办法就是将logo变成调色板图像。

phpqrcode logo为什么变成黑白了

用phpqrcode生成二维码并加上logo,为啥最后生成出来的LOGO变成黑白了?

代码如下:

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 &#39;<img src="&#39;.$PNG_WEB_DIR.basename($filename).&#39;"><hr>&#39;;
Copy after login

因为你用url获取的远端的图像是一个jpeg图像。也就是所谓truecolor的图像。而png是所谓的调色板图像,

需要先将 logo变成调色板图像,才能在copy时不丢失 颜色信息

代码如下:

$logo = imagecreatefromstring(file_get_contents($logo));
if (imageistruecolor($logo)) imagetruecolortopalette($logo, false, 65535); // 新加这个
Copy after login

更多相关知识,请访问PHP中文网

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template