Use phpqrcode to generate a QR code and add a logo. Why did the generated LOGO become black and white?

Source: Internet
Author: User
The Code is as follows {code ...}

The Code is as follows:

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 = comment'; $ 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 code Image Width $ QR_height = imagesy ($ QR); // QR code Image Height $ logo_width = imagesx ($ logo ); // logo image width $ logo_height = imagesy ($ logo); // logo image height $ 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; // re-combine the image and resize 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'
  ';

Reply content:

The Code is as follows:

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 = comment'; $ 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 code Image Width $ QR_height = imagesy ($ QR); // QR code Image Height $ logo_width = imagesx ($ logo ); // logo image width $ logo_height = imagesy ($ logo); // logo image height $ 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; // re-combine the image and resize 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'
  ';

Because the remote image you obtain with a url is a jpeg image. That is, the image of truecolor. While png is a so-called palette image,
The logo must be converted into a color palette before color information is not lost during copy.
The Code is as follows:
$ Logo = imagecreatefromstring (file_get_contents ($ logo ));
If (imageistruecolor ($ logo) imagetruecolortopalette ($ logo, false, 65535); // Add this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.