Home > Backend Development > PHP Problem > How to use PHP to fill irregular graphics

How to use PHP to fill irregular graphics

PHPz
Release: 2023-04-12 09:58:54
Original
574 people have browsed it

In Web development, using graphics to improve the visual effect of the website has become a mainstream method. Dynamically generating and filling various irregular graphics is one of the mainstream technologies to achieve visual effects on web pages. This article will introduce how to use PHP to fill irregular graphics.

First, let’s take a look at the common types of filled graphics:

  1. Solid graphics

Solid graphics are graphics with actual colors and shapes. Such graphics are often used frequently in logos or other areas of a website to add visual interest.

  1. Gradient graphics

Gradient graphics are graphics that gradually change color. This kind of graphics makes the website look more artistic and contributes to a good experience for users.

  1. Pattern graphics

Pattern graphics are graphics interwoven with a series of curved, vertical, horizontal and other patterns in different directions. Pattern graphics are often used in background areas.

This article introduces readers to how to use PHP to fill these three different types of irregular graphics through the following content.

  1. Fill entity graphics

The basic idea of ​​filling entity graphics is to create a picture containing a pattern or color in PHP and set it as the background of the website.

The PHP code to create the fill is as follows:

//创建一个画布并设置画布的宽和高
$img = imagecreatetruecolor(200, 200);

//设置画布颜色RGB值
$color = imagecolorallocate($img, 255, 255, 255);

//将画布颜色填充到画布中
imagefill($img, 0, 0, $color);

//在画布上创建一个实体图形,设置其颜色
$rectColor = imagecolorallocate($img, 0, 0, 255);
imagefilledrectangle($img, 0, 0, 200, 200, $rectColor);

//将背景图像应用到网站的主体部分
echo '<body style="background: url(\&#39;&#39;.imagepng($img).&#39;\&#39;)">';
Copy after login

This PHP code will create a 200×200 canvas and fill it with white. Then, fill the canvas with a solid blue graphic, and finally apply the image as a background to the main body of the website.

  1. Fill gradient graphics

The basic idea of ​​filling gradient graphics is to create a picture in PHP that is similar to the solid graphics and apply different gradient colors on it .

The PHP code to create the fill is as follows:

//创建具有渐变颜色的实体图形
$gradImg = imagecreate(200, 200);
for($i=0;$i<=255;$i++){
    //设置渐变图形的颜色
    $color = imagecolorallocate($gradImg, $i, $i, $i);
    imagefilledrectangle($gradImg, 0, $i, 200, $i+1, $color);
}

//将渐变图像应用到网站的主体部分
echo &#39;<body style="background: url(\&#39;&#39;.imagepng($gradImg).&#39;\&#39;)">';
Copy after login

The above PHP code will create a canvas with a gradient graphic. Achieve color gradients through three loops with different colors. Finally, apply the gradient image as a background to the main body of the website.

  1. Fill Mode Graphics

The basic idea of ​​a Fill Mode Graphic is to create a similar image to a gradient graphic in PHP and apply different patterns on it to simultaneously Fill the canvas.

The PHP code to create the fill is as follows:

//创建一个具有纹理的画布
$textureImg = imagecreatefrompng("texture.png");
$img = imagecreatetruecolor(150,150);
imagecopyresampled($img, $textureImg, 0, 0, 0, 0, 150, 150, imagesx($textureImg), imagesy($textureImg));

//将画布背景应用在网站的主体部分
echo '<body style="background: url(\&#39;&#39;.imagepng($img).&#39;\&#39;)">';
Copy after login

The above PHP code creates a 150×150 canvas and applies texture resources to the canvas. Finally, apply the filled canvas as a background to the main part of the website.

Conclusion:

This article briefly introduces some methods of filling in PHP based on different types of irregular graphics. Although this is a basic method, it can be extended to generate more complex and interesting irregular shapes. Combined with other PHP functions for drawing graphics, website developers can create perfect, artistic page effects.

The above is the detailed content of How to use PHP to fill irregular graphics. For more information, please follow other related articles on the PHP Chinese website!

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