Home  >  Article  >  Backend Development  >  What is the use of php drawing function

What is the use of php drawing function

(*-*)浩
(*-*)浩Original
2019-09-25 14:56:503079browse

In PHP, using the PHP drawing function, the image we draw can appear when accessing the PHP file; the PHP drawing function can be applied to the development of reports, the design of verification codes, and the watermarks and thumbnails of pictures.

What is the use of php drawing function

Before introducing the php drawing function, we first need to understand the drawing coordinate system in php:

php coordinate system , the coordinate origin is at the upper left corner, in pixels. (Recommended learning: PHP Programming from entry to proficiency)

Coordinates (x, y) - the first one is the x coordinate, indicating that the current position is horizontal, x distance from the coordinate origin Pixel; the second one is the y coordinate, indicating that the current position is vertical and y pixels away from the coordinate point.

What is the use of php drawing function

Special note: Pixel is not a unit of length, but a unit of density. Theoretically, the smaller the resolution, the larger the length occupied by one pixel.

Basic principles and steps of php drawing

(1) Create canvas

(2) Draw various graphics required (circles, straight lines, Rectangle, arc, sector...)

(3) Output the image to the web page, or save it (it can be saved as several common image formats for website development, based on storage format issues)

(4) Destroy the image (release memory)

The following is the code for drawing the verification code:

".dechex(rand(1,15))."
"; session_start(); $checkCode=""; for($i=0;$i<4;$i++) { $checkCode.=dechex(rand(1,15)); } //讲随机验证码保存到session中 $_SESSION['myCheckCode']=$checkCode; //创建图片,并把随机数画上去 $img=imagecreatetruecolor(110, 30); //背景默认就是黑色 //你可以指定背景颜色 $bgcolor=imagecolorallocate($img, 0, 0, 0); imagefill($img, 0, 0, $bgcolor); //创建新的颜色 $white=imagecolorallocate($img, 255, 255, 255); $blue=imagecolorallocate($img, 0, 0, 255); $red=imagecolorallocate($img, 255, 0, 0); $green=imagecolorallocate($img, 0, 255, 0); //画出干扰线段 for($i=0;$i<20;$i++) { //更好的方法是颜色随机 imageline($img, rand(0,110), rand(0,30), rand(0,110), rand(0,30), imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255))); } //画出噪点,自己画 //for($i=0;$i<10;$i++) //把四个随机值画上去 imagestring($img, rand(1,5), rand(2,80), rand(2,10), $checkCode, $white); //如果要使用中文 //array imagefttext(string $font_file, string $text [,array $extrainfo) //imagettftext($img,15,10,20,25,$white,"STXINWET.TTF","北京你好"); //输出 header("content-type: image/png"); imagepng($img); ?>

is used for the login interface (only part of the code is provided here, but It does not affect understanding, you can delete irrelevant content and use it directly)

用户id"/>
密 码
验证码 What is the use of php drawing function
是否保存用户id

The above is the detailed content of What is the use of php drawing function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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