Detailed explanation of canvas in PHP

小云云
Release: 2023-03-22 13:10:01
Original
5225 people have browsed it

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.

Create a new canvas--Resource data (can operate image resources)

ImageCreate (canvas width, canvas height); create a palette-based canvas.

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

Create canvas based on image

ImageCreateFromFPG (image address);

ImageCreateFromPNG (image address);

ImageCreateFromGIF (image address);

Operation canvas

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(&#39;Content-Type:image/jpg&#39;);
$chars=&#39;ABCDEFGHIJKLMNPQRSTUVWXYZ123456789&#39;;
$chars_length=strlen($chars);
$code_length=4;
$code=&#39;&#39;;
for($i=0;$i<$code_length;$i++){
	$rand_index=mt_rand(0,$chars_length-1);
	$code.=$chars[$rand_index];
}

//存储于session,用于验证。
session_start();
$_SESSION[&#39;captcha_code&#39;]=$code;


//背景图。
$bg_file=&#39;./captcha/captcha_bg&#39;.mt_rand(1,5).&#39;.jpg&#39;;
$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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!