PHP draws a straight line based on known points_PHP tutorial
Sometimes we need to draw lines for hot spots in images, then we have to use PHP’s GD library. The number of points in the hot zone is variable, and the size of the image is also variable. We can use the following method to generate the hot zone of the image.
<?php header("Content-type: image/jpeg"); $width = 400; $height = 300; $image = imagecreate(400, 300); $white = imagecolorallocate($image, 0xf5, 0xf5, 0xf5); $red = imagecolorallocate($image, 0xff, 0x00, 0x00); $blue = imagecolorallocate($image, 204, 255, 0); $blue2 = imagecolorallocate($image, 0, 0, 0); $line_string ="65.616350%,6.142129%,50.894733%,6.142129%,49.632880%,12.764112%,17.665940%,12.764112%,16.544293%,6.142129%,2.663912%,6.142129%,2.663912%,29.558995%,65.616350%,29.558995%"; $line_array = explode(",",$line_string); for($i = 0; $i < count($line_array); $i=$i+2) { if($i <= count($line_array) - 4) { imageline($image, $line_array[$i] / 100 * $width, $line_array[$i+1] / 100 * $height, $line_array[$i+2] / 100 * $width, $line_array[$i+3] / 100 * $height, $blue); } else { imageline($image, $line_array[$i] / 100 * $width, $line_array[$i+1] / 100 * $height, $line_array[0] / 100 * $width, $line_array[1] / 100 * $height, $blue); } } imagejpeg($image); //imagejpeg($image,"test.jpg",80); //保存图片.80为图片质量 //推荐用ImagePNG()输出,这样图片质量要好些,文件大小也小些 ?>
imageline() function
Syntax: int imageline(int im, int x1, int y1, int x2, int y2, int col);
This function will draw a solid line on the graph. Connect from x1, y1 to x2, y2, with the origin (0,0) being the upper left corner of the graph. The parameter col is the color of the solid line.
Reference example:
<?php function imagelinethick ( $image , $x1 , $y1 , $x2 , $y2 , $color , $thick = 1 ) { /* 下面两行只在线段直角相交时好使 imagesetthickness($image, $thick); return imageline($image, $x1, $y1, $x2, $y2, $color); */ if ( $thick == 1 ) { return imageline ( $image , $x1 , $y1 , $x2 , $y2 , $color ); } $t = $thick / 2 - 0.5 ; if ( $x1 == $x2 || $y1 == $y2 ) { return imagefilledrectangle ( $image , round ( min ( $x1 , $x2 ) - $t ), round ( min ( $y1 , $y2 ) - $t ), round ( max ( $x1 , $x2 ) + $t ), round ( max ( $y1 , $y2 ) + $t ), $color ); } $k = ( $y2 - $y1 ) / ( $x2 - $x1 ); //y = kx + q $a = $t / sqrt ( 1 + pow ( $k , 2 )); $points = array( round ( $x1 - ( 1 + $k )* $a ), round ( $y1 + ( 1 - $k )* $a ), round ( $x1 - ( 1 - $k )* $a ), round ( $y1 - ( 1 + $k )* $a ), round ( $x2 + ( 1 + $k )* $a ), round ( $y2 - ( 1 - $k )* $a ), round ( $x2 + ( 1 - $k )* $a ), round ( $y2 + ( 1 + $k )* $a ), ); imagefilledpolygon ( $image , $points , 4 , $color ); return imagepolygon ( $image , $points , 4 , $color ); } ?>
imagecolorallocate() function
assigns a color to an image.
imagecolorallocate() Returns an identifier representing a color consisting of the given RGB components. The image parameter is the return value of the imagecreatetruecolor() function. red , green and blue are the red, green and blue components of the desired color respectively. These parameters are integers from 0 to 255 or hexadecimal 0x00 to 0xFF. imagecolorallocate() must be called to create each color used in the image represented by image.
The first call to imagecolorallocate() will fill the background color.
<?php $im = imagecreatetruecolor ( 'example.jpg' ); // 背景设为红色 $background = imagecolorallocate ( $im , 255 , 0 , 0 ); // 设定一些颜色 $white = imagecolorallocate ( $im , 255 , 255 , 255 ); $black = imagecolorallocate ( $im , 0 , 0 , 0 ); // 十六进制方式 $white = imagecolorallocate ( $im , 0xFF , 0xFF , 0xFF ); $black = imagecolorallocate ( $im , 0x00 , 0x00 , 0x00 ); ?>
This function is used to match the color of graphics and is used by other drawing functions. The parameter im represents the handle of the graphic. The parameters red, green, and blue are the three primary colors, and their values range from 0 to 255.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
