Image processing-how does PHP remove some colors from an image to make it transparent?

Source: Internet
Author: User
Make an image transparent (based on the specified RGB color range. But in practice, the following code value is removed from white. What is the problem? {Code...} transparency of an image (based on the specified RGB color range. But in practice, the following code value is removed from white. What is the problem?

$ O_pic = '1.jpg '; // The starting Color Order to be processed $ begin_r = 215; $ begin_g = 215; $ begin_ B = 215; list ($ src_w, $ src_h, $ src_type) = getimagesize ($ o_pic); // get the original image information $ file_ext = get_ext ($ o_pic); // get the extension $ target_im = imagecreatetruecolor ($ src_w, $ src_h ); // new image if ($ file_ext = 'jpg ') // convert jpg to start {$ src_im = ImageCreateFromJPEG ($ o_pic); imagecopymerge ($ target_im, $ src_im, 0, 0, 0, 0, 0, $ src_w, $ src_h, 100); for ($ x = 0; $ x <$ src_w; $ x ++) {for ($ y = 0; $ y <$ src_h; $ y ++) {$ rgb = imagecolorat ($ src_im, $ x, $ y); $ r = ($ rgb> 16) & 0xFF; $ g = ($ rgb> 8) & 0xFF; $ B = $ rgb & 0xFF; if ($ r> $ begin_r & $ g> $ begin_g & $ B> $ begin_ B) {imagecolortransparent ($ target_im, imagecolorallocate ($ target_im, $ r, $ g, $ B ));}}}}

Reply content:

Make an image transparent (based on the specified RGB color range. But in practice, the following code value is removed from white. What is the problem?

$ O_pic = '1.jpg '; // The starting Color Order to be processed $ begin_r = 215; $ begin_g = 215; $ begin_ B = 215; list ($ src_w, $ src_h, $ src_type) = getimagesize ($ o_pic); // get the original image information $ file_ext = get_ext ($ o_pic); // get the extension $ target_im = imagecreatetruecolor ($ src_w, $ src_h ); // new image if ($ file_ext = 'jpg ') // convert jpg to start {$ src_im = ImageCreateFromJPEG ($ o_pic); imagecopymerge ($ target_im, $ src_im, 0, 0, 0, 0, 0, $ src_w, $ src_h, 100); for ($ x = 0; $ x <$ src_w; $ x ++) {for ($ y = 0; $ y <$ src_h; $ y ++) {$ rgb = imagecolorat ($ src_im, $ x, $ y); $ r = ($ rgb> 16) & 0xFF; $ g = ($ rgb> 8) & 0xFF; $ B = $ rgb & 0xFF; if ($ r> $ begin_r & $ g> $ begin_g & $ B> $ begin_ B) {imagecolortransparent ($ target_im, imagecolorallocate ($ target_im, $ r, $ g, $ B ));}}}}

Do not use jpg. Because jpg does not support transparency, it is white. Use imagecreatetruecolor or imagecreatefrompng.

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.