PHP uses GD library to merge simple images and change parts of colors

Source: Internet
Author: User
Tags transparent image

Recently saw a lot of big companies have started to do a variety of pet chain, the most special is that the pet has a variety of parts and then different combinations and produce the corresponding pet pictures, looks relatively tall, but found that some are using SVG vector pictures, such pictures theoretically no distortion can be arbitrarily amplified performance slightly affected, The editor is easy to adjust, but the operation is cumbersome, if you use the picture directly then it will be easier.

PHP's GD library provides a number of basic image manipulation functions, which can be divided into two main categories:

True Color chart operation : supports direct transparent image processing, but does not support color transformations, allowing new content to be drawn.
Palette Chart Operations : supports specifying colors as transparent, and supports color transformations, allowing new content to be drawn.

Two types of pictures can be converted to each other, if the original picture has transparent blocks as far as possible to avoid directly to the palette map (transparent blocks prone to unknown exceptions) but can be merged into a palette map to preserve the transparency of the original image, if you specify a color value in the palette map is transparent, the color value is transparent after the picture is generated.

If you use only the GD library when you do not need to change the color of the picture, you basically do not need to use a color palette, but instead need to change the color of the picture can only use color palette.

This is the goal of creating a small monster to manipulate the color of the small monster:

You first need to prepare 5 basic picture elements:

Picture requirements:

    1. All pictures are best to be painted in new (preferably using vector graphics).
    2. Eyes.png In addition to the eyes of the body all transparent treatment.
    3. Fleck.png all the transparent treatment except the stripes.
    4. Mouth.png In addition to the mouth of the body all transparent treatment.
    5. The shadow.png is not color-transparent in body shape, and all white is used outside of body shape.
    6. Shape.png do not have transparent content.
    7. All color values that need to be replaced are best not to appear in other locations.

Below is a sample code to generate a different color pet:

    $image = imagecreatefrompng (' shape.png '); Take body image list ($src _w, $src _h) = getimagesize (' shape.png '); Get wide height Imagetruecolortopalette ($image, False, 256); Convert to palette image $color _index = Imagecolorat ($image, 276, 621); Get color index values (body color) imagecolorset ($image, $color _index, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255)); Modify the color $color _index = Imagecolorat ($image, 450, 780); Get color index values (belly color) imagecolorset ($image, $color _index, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255)); Modify color//This process is very important, if directly converted to True color map will cause subsequent picture merge exception $_image = Imagecreatetruecolor ($src _w, $src _h); Create a true Color chart $color = imagecolorallocate ($_image, 255, 255, 255); Assign Color Imagefill ($_image, 0, 0, $color); Filled imagecopyresampled ($_image, $image, 0, 0, 0, 0, $src _w, $src _h, $src _w, $src _h);    Merge modified picture $image = $_image; /* Stripe handling */$image _fleck = imagecreatefrompng (' fleck.png '); Stripe image imagecopyresampled ($image, $image _fleck, 0, 0, 0, 0, $src _w, $src _h, $src _w, $src _h); Merge $color _index = Imagecolorat ($image, 385, 925); Get color index values (stripe color) imagecolorset ($image, $color _index, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255)); Modify Color/* body Shadow Processing */imagecopyresampled ($image, Imagecreatefrompng (' test1/shadow.png '), 0, 0, 0, 0, $src _w, $src _h, $    Src_w, $src _h); /* Mouth handling */imagecopyresampled ($image, Imagecreatefrompng (' test1/mouth.png '), 0, 0, 0, 0, $src _w, $src _h, $src _w, $src _h    ); /* Eye Treatment */$image _eyes = imagecreatefrompng (' eyes.png '); Stripe image imagecopyresampled ($image, $image _eyes, 0, 0, 0, 0, $src _w, $src _h, $src _w, $src _h); Merge $color _index = Imagecolorat ($image _eyes, 285, 335); Get color index values (eye color) imagecolorset ($image, $color _index, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 255)); Modify color//Add background $color _index = Imagecolorat ($image, 435, 300); Get color index value (background color) imagefilltoborder ($image, 0, 0, $color _index, Imagecolorallocate ($image, Mt_rand (0, 255), Mt_rand (0, 25    5), Mt_rand (0, 255)); Imagesavealpha ($image, true); Saving alpha channel Information   Header (' content-type:image/png ');    Imagepng ($image, NULL, 9); Imagedestroy ($image);

Note: The index value function that needs to take out the palette color when replacing the picture color Imagecolorat is the index value of the color (want to get which color gives any coordinate value of the color), because I test the picture 1304 X 1412 Therefore, the coordinate values in the code are relatively large.

The results of the implementation are as follows:

PHP uses GD library to merge simple images and change parts of colors

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.