The canvas in PHP is detailed

Source: Internet
Author: User
Tags imagejpeg
This article is mainly to share with you the canvas in PHP, the image processing technology in PHP GD, I hope to help everyone.

Create a new canvas- resource-based data (you can manipulate image resources)

Imagecreate (canvas width, canvas height); Create a palette-based canvas.

Imagecreatetruecolor (canvas width, canvas height); Create a true color canvas.

Create a canvas based on a picture

IMAGECREATEFROMFPG (image address);

Imagecreatefrompng (image address);

Imagecreatefromgif (image address);

Manipulating the canvas

Assign color: If you need to use a color on the canvas, you should first assign the color to the canvas.

Color identification:

$color =imagecolorallocate (canvas resource, r,g,b), RGB is three primary color, size is 0~255;

Fill the canvas:

Imagefill (canvas resource, fill position x, fill position y, color identification), fill with points that are contiguous and of the same color as the fill point.

Write the string to the canvas:

Imagestring (canvas resource, font, position x, position y, string content, character color);

The font is a built-in font, and the size is the number.

The character color is the variable that was written when the previous color was assigned.

Get the size of the canvas open:

Imagesx (canvas resource); Get the width of the canvas.

Imagesy (canvas resource); get the canvas high.

Get the size of the built-in font:

Imagefontwidth (font number); Gets the width of the built-in font.

Imagefontheight (font number); Gets the height of the built-in font.

Output canvas

1. Output to the picture file.

2. Direct output to the page.

Imagepng (canvas resource [, file address]);

imagejpeg (canvas resource [, file Address]);

imagegif (canvas resource [, file Address]);

Example: Verification code

<?phpheader (' content-type:image/jpg '); $chars = ' ABCDEFGHIJKLMNPQRSTUVWXYZ123456789 '; $chars _length=strlen ($ chars), $code _length=4; $code = ", for ($i =0; $i < $code _length; $i + +) {$rand _index=mt_rand (0, $chars _length-1); $code. = $chars [$rand _index];} stored in session for validation. Session_Start (); $_session[' Captcha_code ']= $code;//Background map. $BG _file= './captcha/captcha_bg '. Mt_rand (1,5). JPG '; $img =imagecreatefromjpeg ($BG _file);//randomly distribute color $chars_color=mt_rand (==1?imagecolorallocate) ($img, 0,0,0): Imagecolorallocate ($img, 255,255,255);//Writes a string to the canvas. $img _width=imagesx ($img), $img _height=imagesy ($img), $font _width=imagefontwidth (5), $font _height= Imagefontheight (5); $code _width= ($img _width-$font _width)/2; $code _height= ($img _height-$font _height)/2;i Magestring ($img, 5, $code _width, $code _height, $code, $chars _color); imagejpeg ($img);

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.