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:503216browse

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:

<?php
    //echo rand(2,9);
    //echo "<br/>".dechex(rand(1,15))."<br/>";
    session_start();
    $checkCode="";
    for($i=0;$i<4;$i++) {
        $checkCode.=dechex(rand(1,15));
    }
    
    //讲随机验证码保存到session中
    $_SESSION[&#39;myCheckCode&#39;]=$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)

<form action="loginProcess.php" method="post">
<table>
<tr><td>用户id</td><td><input type="text" name="id" value="<?php echo getCookieVal("id"); ?>"/></td></tr>
<tr><td>密 码</td><td><input type="password" name="password"/></td></tr>
<tr><td>验证码</td><td><input type="text" name="checkCode"/>
<img  src="checkCode.php" onClick="this.src=&#39;checkCode.php?aa=&#39;+Math.random()"/ alt="What is the use of php drawing function" ></td></tr>
<tr><td colspan="2">是否保存用户id<input type="checkbox" value="yes" name="keep"></td></tr>
<tr><td><input type="submit" value="用户登录"/></td>
<td><input type="reset" value="重新填写"/></td></tr>
</table>
</form>

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