This article mainly shares with you the detailed explanation of canvas in PHP and the image processing technology GD in PHP. I hope it can help you.
ImageCreate (canvas width, canvas height); create a palette-based canvas.
ImageCreateTrueColor(canvas width, canvas height); Create a true color canvas.
ImageCreateFromFPG (image address);
ImageCreateFromPNG (image address);
ImageCreateFromGIF (image address);
Assign color: If you need to use a color on the canvas, you should assign the color to the canvas first.
Graphid:
# ImageFill
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, with sizes ranging from 1 to 5.
The character color is the variable written when assigning the previous color.
##:
# iMAGESX; get the width of the canvas.
ImageSY (canvas resource); get the height of the canvas.
Get the size of the built-in font:
ImageFontWidth (font number); Get the width of the built-in font.
ImageFontHeight (font number); Get the height of the built-in font.
Output canvas
1. Output to image file.
2. Output directly to the page.
ImagePNG(canvas resource[, file address]);
## ImageJPEG(canvas Resource [, file address]);
## ImageGIF(canvas resource[, File address]);
Example: Verification code
##<?php
header('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];
}
//存储于session,用于验证。
session_start();
$_SESSION['captcha_code']=$code;
//背景图。
$bg_file='./captcha/captcha_bg'.mt_rand(1,5).'.jpg';
$img=ImageCreateFromJPEG($bg_file);
//随机分发颜色
$chars_color=mt_rand(1,2)==1?imagecolorallocate($img,0,0,0):imagecolorallocate($img,255,255,255);
//将字符串写到画布上。
$img_width=ImageSX($img);
$img_height=ImageSY($img);
$font_width=ImageFontWidth(5)*4;
$font_height=ImageFontHeight(5);
$code_width=($img_width-$font_width)/2;
$code_height=($img_height-$font_height)/2;
ImageString($img,5,$code_width,$code_height,$code,$chars_color);
ImageJPEG($img);
The above is the detailed content of Detailed explanation of canvas in PHP. For more information, please follow other related articles on the PHP Chinese website!