Home > Backend Development > PHP Tutorial > GD library and related functions in PHP

GD library and related functions in PHP

WBOY
Release: 2023-06-22 20:54:01
Original
2770 people have browsed it

GD library and related functions in PHP

The GD library is a very powerful graphics library in the PHP language. It can be used to process pictures, dynamically generate images and thumbnails, etc. This article will introduce the relevant concepts and common functions of the GD library.

  1. Installation of GD library

Before using the GD library, you need to ensure that the library has been installed on the system. You can enter the following command on the command line to check whether the GD library has been installed:

php -m | grep -i gd
Copy after login

If "gd" is output, it means that the GD library has been installed; if the output is empty, you need to use the following command to replace the GD library. Installed into the system:

sudo apt-get install php7.2-gd
Copy after login
  1. Basic concepts of the GD library

The GD library is an open source cross-platform graphics library that can be used to process pictures and generate images. and thumbnails etc. When the GD library processes an image, it converts the image into a collection of pixels and processes each pixel. A pixel usually includes three color values: red, green, and blue. By processing these three color values, images of various colors can be obtained.

  1. Commonly used functions of the GD library

The GD library provides many very useful functions. Several commonly used functions will be introduced below.

3.1. imagecreatetruecolor()

The imagecreatetruecolor function is used to create a true color image, which is declared as follows:

resource imagecreatetruecolor ( int $width , int $height )
Copy after login

where $width and $height are the width of the image and height, the return value is an image resource handle.

The following is an example of using the imagecreatetruecolor function to create a 300x200 red rectangle:

<?php
// 创建一个300x200的真彩色图像
$img = imagecreatetruecolor(300, 200);

//定义红色
$red = imagecolorallocate($img, 255, 0, 0);

//在图像上画一个填充了红色的矩形
imagefilledrectangle($img, 0, 0, 300, 200, $red);

//将图像输出到浏览器
header('Content-type: image/png');
imagepng($img);

//释放图像资源
imagedestroy($img);
?>
Copy after login

3.2. imagecreatefromjpeg()

The imagecreatefromjpeg function is used to create a 300x200 red rectangle from a JPG image file The read image resource handle is declared as follows:

resource imagecreatefromjpeg ( string $filename )
Copy after login

Among them, $filename is the JPG image file name, and the return value is an image resource handle.

The following is an example of using the imagecreatefromjpeg function to read a JPG image file and scale it:

<?php
//从文件中创建一个图像资源
$src_image = imagecreatefromjpeg('source.jpg');

//获取原始图像的宽和高
list($src_width, $src_height) = getimagesize('source.jpg');

//创建一个新的缩放后的图像资源
$dst_image = imagecreatetruecolor(100, 100);

//将原始图像按照比例缩放到新的图像资源中
imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, 100, 100, $src_width, $src_height);

//将图像输出到浏览器
header('Content-type: image/png');
imagepng($dst_image);

//释放图像资源
imagedestroy($src_image);
imagedestroy($dst_image);
?>
Copy after login

3.3. imagecopymerge()

The imagecopymerge function is used to overlay an image on On another image, and set transparency, its declaration is as follows:

bool imagecopymerge ( resource $dst_im , resource $src_im , int $dst_x , int $dst_y , int $src_x , int $src_y , int $src_w , int $src_h , int $pct )
Copy after login

Where, $dst_im is the target image resource handle, $src_im is the source image resource handle, $dst_x and $dst_y are the starting points in the target image Coordinates, $src_x and $src_y are the starting coordinates in the source image, $src_w and $src_h are the width and height of the source image, $pct is the transparency, the range is 0-100.

The following is an example of using the imagecopymerge function to cover a circular image in another basemap:

<?php
//从文件中创建一个底图
$bg_image = imagecreatefrompng('bg.png');

//从文件中创建一个圆形图像
$circle_image = imagecreatefrompng('circle.png');

//获取圆形图像的宽和高
list($circle_width, $circle_height) = getimagesize('circle.png');

//定义透明度为60%
$pct = 60;

//将圆形图像复制到底图中
imagecopymerge($bg_image, $circle_image, 100, 100, 0, 0, $circle_width, $circle_height, $pct);

//将图像输出到浏览器
header('Content-type: image/png');
imagepng($bg_image);

//释放图像资源
imagedestroy($bg_image);
imagedestroy($circle_image);
?>
Copy after login
  1. Summary

The GD library is A very useful graphics library that allows easy processing and generation of images. This article introduces the installation, basic concepts and common functions of the GD library. By learning and using the GD library, we can make our PHP applications more flexible and powerful.

The above is the detailed content of GD library and related functions in PHP. For more information, please follow other related articles on the PHP Chinese website!

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