PHP operation Image (GD function library)

Source: Internet
Author: User

PHP.ini change; extension=php_gd2.dll

Creating an image in PHP should be done in four steps:

• Create a background image • Draw a graphic on the background or enter text • Output final graphics • Clear all resources

<?php

$width = 200; Image width

$height = 400; Image Height

$img = Imagecreatetruecolor ($width, $height);

?>

Read an existing image file by Imagecreatefrompng (), Imagecreatejpeg (), or imagecreatefromgif (), then filter it and add additional images to it.

<?php

$imgTemp = imagecreatefrompng (' abc.png ');

?>

<?php

$width = 200; Image width

$height = 400; Image Height

$img = Imagecreatetruecolor ($width, $height);

$white = Imagecolorallocate ($img, 255, 255, 255); RGB value

$blue = imagecolorallocate ($img, 0, 0, 64);

Imagefill ($img, 0, 0, $blue); Fill $blue in the background map

Imageline ($img, 0, 0, $width, $height, $white); /* Here there are 2 pairs of coordinates, "0, 0" is the coordinates of the starting point, and $width, $height is the coordinates of the end, the last parameter is the color. */

Imagestring ($img, 3, +, ' abc ', $white);//write a string on the image

?>

<?php

$width = 200; Image width

$height = 400; Image Height

$img = Imagecreatetruecolor ($width, $height);

$white = Imagecolorallocate ($img, 255, 255, 255); RGB value

$blue = imagecolorallocate ($img, 0, 0, 64);

Imagefill ($img, 0, 0, $blue); Fill $blue in the background map

Imageline ($img, 0, 0, $width, $height, $white); /* Here there are 2 pairs of coordinates, "0, 0" is the coordinates of the starting point, and $width, $height is the coordinates of the end, the last parameter is the color. */

Imagestring ($img, 3, +, ' abc ', $white);//write a string on the image

Header (' content-type:image/png '); Specifies the MIME type of the image

Imagepng ($IMG); Output image data

?>

Similarly, if you want to eventually output a JPEG image, you can use imagejpeg () to output it, and also replace the header () function parameter with Image/jpeg.

In summary, we are sending the image directly to the browser, if we want to automatically create the image generated file, you can add the second parameter in the Imagepng () function, this parameter is the name of the file you want to generate, such as logo.png. Note that the file suffix name remains in the format, and the filename is enclosed in quotation marks.

-------------------------

Freeing and destroying resources

<?php

$width = 200; Image width

$height = 400; Image Height

$img = Imagecreatetruecolor ($width, $height);

$white = Imagecolorallocate ($img, 255, 255, 255); RGB value

$blue = imagecolorallocate ($img, 0, 0, 64);

Imagefill ($img, 0, 0, $blue); Fill $blue in the background map

Imageline ($img, 0, 0, $width, $height, $white); /* Here there are 2 pairs of coordinates, "0, 0" is the coordinates of the starting point, and $width, $height is the coordinates of the end, the last parameter is the color. */

Imagestring ($img, 3, +, ' abc ', $white);//write a string on the image

Header (' content-type:image/png '); Specifies the MIME type of the image

Imagepng ($IMG); Output image data

Imagedestroy ($IMG); Release and Destroy

?>

Call an automatically generated image

Summarize:

Create a background Image: Imagecreatetruecolor ()

Drawing or entering text on the background: Imagecolorallocate (), Imagefill (), Imageline (), imagestring ()

Output Final Graphics: header (), Imagepng ()

Clear All resources: Imagedestroy ()

PHP operation Image (GD function library)

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.