PHP generates GIF and PNG transparent data streams

Source: Internet
Author: User
Tags transparent color
<textarea cols="50" rows="15" name="code" class="php:nogutter:nocontrols"><? Php <br/> header ("Content-Type: image/PNG"); <br/> $ IMG = imagecreate (100,35 ); <br/> $ bgcolor = imagecolorallocate ($ IMG, 0); <br/> $ Red = imagecolorallocate ($ IMG, 0 ); <br/> $ bgcolortrans = imagecolortransparent ($ IMG, $ bgcolor); <br/> imagestring ($ IMG, 5, 10, "png php", $ red ); <br/> imagepng ($ IMG); <br/> imagedestroy ($ IMG); <br/>?></textarea>

The imagecreate () function creates an image. The created image has two parameters: Image Width and Image Height. Imagecreate (100, 80) creates an image of pixels in width and 80 pixels in height. The image format can be PNG, JPEG, or GIF.

Using the imagecreate () function to create an image is actually converting the TrueType font text (text) into the image format. Therefore, we need to write text to the image variable. The function used is imagestring, the usage of this function is as follows:

Imagestring (, 3, 10, 10 ,,);

Explanation:

-Image object;
Numeric values: 3 respectively, the text part size, take the HTML standard font size. The two 10 values are the x and y values of the text in the image respectively;
-The string of the image content;
-Preset color variables.

We know that only PNG and GIF images can be transparent to the background, so we will specify the image format in the header. The statement is as follows: Header ("Content-Type: image/PNG (or GIF) "). To make the image background transparent, we first need to specify the background color for the image and then make it transparent. The required function is described as follows:

Imagecolorallocate (): sets the image background and foreground color. Parameter: The set image object and RGB color value. For example, set the foreground or background color of the image object to red in the format of = imagecolorallocate (, 0, 0;

Imagecolortransparent (): Specifies the transparent color. Parameter: The image object to be set and the color variable to be transparent. For example, = imagecolortransparent (,)

The following example changes "Hello PHP" to a transparent background color image. The background color of the image is black and the foreground color is red. As the background is transparent, we cannot see the black color in the effect.

 

 

To view the effect, click → transparent Effect

Compare the effect of not performing transparent processing → opacity Effect

In this exampleCodeThe second to the last line (imagepng () is used to draw an image. If the image is in GIF format, imagegif () is used (). The function of the last sentence (imagedestroy () is to release the memory allocated to the image, which cannot be discarded-as long as the image is drawn at any time, after work is done, you should habitually clear it from memory to reclaim resources.

[Description]

· Texts used as image content do not support Chinese characters or are not supported properly;

· Exercise caution when disputes over the copyright of GIF images;

· images generated by the preceding code can be saved as valid graph files on the web page.

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.