当前位置:Gxlcms > PHP教程 > 用phpqrcode生成二维码并加上logo,为啥最后生成出来的LOGO变成黑白了?

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

时间:2021-07-01 10:21:17 帮助过:4人阅读

代码如下

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); // 新加这个。

人气教程排行