Home > Backend Development > PHP Tutorial > PHP Image Processing (Part 1) - Basics

PHP Image Processing (Part 1) - Basics

齐天大圣
Release: 2020-05-04 14:41:58
Original
197 people have browsed it

I encountered a project before that used image processing technology. Similar to the picture below

PHP Image Processing (Part 1) - Basics

The picture is divided into 3 parts, the background picture, the QR code picture, and the text under the QR code picture. Among them, the QR code image and text are dynamically generated, and the QR code image and text description are different for different activities.

How to complete this requirement, we need to use PHP image processing technology to achieve it. PHP's image processing is very powerful and can do many things. Common ones include verification code images, image watermarks, thumbnails, etc.

Basic knowledge

First we need to install the extended GD library of php. After you have him, you can proceed to the following steps. The following mainly focuses on the code. Please refer to the documentation for the specific usage of the function.

Create canvas

The main function imagecreatetruecolor—creates a new true color image.

imagecreatetruecolor    ( int $width   , int $height   ) : resource
Copy after login
<?php
// 创建一个100*100的画布
$im = imagecreatetruecolor(100, 100);
// 生成png图片
header("Content-type:image/png");
imagepng($im);
imagedestroy($im);
Copy after login

Set the color of the canvas

Main function

  • imagecolorallocate — Assign a color to an image

  • imagefill — Area filling

imagecolorallocate ( resource $image , int $red , int $green , int $blue ) : int

imagecolorallocate () returns an identifier representing a color consisting of the given RGB components. 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 from 0x00 to 0xFF.

imagecolorallocate() must be called to create each color used in the image represented by image.

imagefill ( resource $image , int $x , int $y , int $color ): bool

imagefill() in image The coordinates x, y of the image (the upper left corner of the image For 0, 0), use color to perform area filling (that is, points with the same color as the x, y point and adjacent points will be filled).

<?php
header("Content-type:image/png");
// 创建一个100*100的画布
$im = imagecreatetruecolor(100, 100);
// 设置红包
$color = imagecolorallocate($im, 255, 0, 0);
// 填充画布
imagefill($im, 0, 0, $color);
// 生成图片
imagepng($im);
// 销毁资源
imagedestroy($im);
Copy after login

This code will generate a 100*100 red background image

Draw the image

Draw points and lines

Main functions:

  • imagesetpixel — draw a single pixel

  • imageline — draw a line segment

imagesetpixel (resource $image, int $x, int $y, int $color): bool

imagesetpixel() uses color in the image. The color is at the x, y coordinates (upper left corner of the image) Draw a point on 0,0).

imageline (resource $image, int $x1, int $y1, int $x2, int $y2, int $color): bool

imageline() uses color to color in the image image Draw a line segment from coordinates x1,y1 to x2,y2 (0,0 in the upper left corner of the image).

<?php
$imgHandler = imagecreatetruecolor(100,100);
// 填充背景
$bgColor = imagecolorallocate($imgHandler, 200, 30, 40);
imagefill($imgHandler,0, 0, $bgColor);
// 绘制点
for ($i = 0; $i < 100; $i++) {
   $pointColor = imagecolorallocate($imgHandler, rand(0,200), rand(0,200), rand(0,200));
   imagesetpixel($imgHandler, rand(0, 100), rand(0, 100), $pointColor);
}
// 绘制线
for ($i = 0; $i < 10; $i++) {
   $lineColor = imagecolorallocate($imgHandler, rand(100, 225), rand(100, 225), rand(0, 50));
   imageline($imgHandler, 
       rand(0, 100), 
       rand(0, 100),
       rand(0, 100),
       rand(0, 100), $lineColor);
}
header("Content-Type:image/png");
imagepng($imgHandler);
imagedestroy($imgHandler);
Copy after login

Draw a rectangle

<?php
$imgHandler = imagecreatetruecolor(100,100);
// 填充背景
$bgColor = imagecolorallocate($imgHandler, 200, 30, 40);
imagefill($imgHandler,0, 0, $bgColor);
// 绘制矩形边框
$borderCol = imagecolorallocate($imgHandler, 23, 32, 200);
imagerectangle($imgHandler, 0, 0, 99, 99, $borderCol);
header("Content-Type:image/png");
imagepng($imgHandler);
imagedestroy($imgHandler);
Copy after login

The above is the detailed content of PHP Image Processing (Part 1) - Basics. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
1
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