Home > Backend Development > PHP Tutorial > GD library operation guide in PHP

GD library operation guide in PHP

王林
Release: 2023-05-20 14:42:01
Original
3786 people have browsed it

1. What is GD library?

The GD library is a set of library functions for creating and processing various image formats. It is one of the most commonly used image processing libraries in PHP.

2. Install the GD library

Install the GD library under CentOS/RedHat

1. Install the GD extension library of PHP

yum install php-gd

2. Restart the web server

service httpd restart

3. Check the GD library version supported by PHP

php -i | grep -i gd

Install GD library under Ubuntu/Debian

1. Install php5-gd module

apt-get update && apt-get install php5-gd

2. Restart the web server

service apache2 restart

3. Check the GD library version supported by PHP

php -i | grep -i gd

三, Basic operations of GD library

1. Create an image

1) Create a 200X200 pixel black image
$image = imagecreate(200,200);
$black = imagecolorallocate( $image,0,0,0);
imagefill($image,0,0,$black);

2) Add text to the image
$white = imagecolorallocate($image, 255,255,255);
$text = 'Hello, GD!';
imagettftext($image,20,0,70,100,$white,'arial.ttf',$text);

3 ) Save the image to a file
imagepng($image,'test.png');

4) Release memory
imagedestroy($image);

2. Image processing

1) Scale the image
$src_image = imagecreatefrompng('test.png');
$src_width = imagesx($src_image);
$src_height = imagesy($src_image);
$new_width = $src_width * 0.5;
$new_height = $src_height * 0.5;
$new_image = imagecreatetruecolor($new_width,$new_height);
imagecopyresampled($new_image,$src_image,0,0, 0,0,$new_width,$new_height,$src_width,$src_height);
imagepng($new_image,'test-resized.png');

2) Add border
$border_color = imagecolorallocate($new_image,128,128,128);
imagerectangle($new_image,0,0,$new_width-1,$new_height-1,$border_color);
imagepng($new_image,'test-bordered.png') ;

3) Cropped image
$cropped_image = imagecrop($new_image,['x'=>40,'y'=>40,'width'=>100,'height' =>100]);
imagepng($cropped_image,'test-cropped.png');

##4) Blurred image

$blurred_image = imagefilter($new_image,IMG_FILTER_GAUSSIAN_BLUR);
imagepng($blurred_image,'test-blurred.png');

3. Manipulate image elements

1) Get the pixel RGB value

$pixel = imagecolorat($new_image,50 ,50);
$red = ($pixel >> 16) & 0xFF;
$green = ($pixel >> 8) & 0xFF;
$blue = $pixel & 0xFF ;

2) Modify the pixel RGB value

$new_color = imagecolorallocate($new_image,255,0,0);
imagesetpixel($new_image,50,50,$new_color);
imagepng($new_image,'test-pixel.png');

3) Fill image

$fill_color = imagecolorallocate($new_image,0,255,0);
imagefill($new_image,0, 0,$fill_color);
imagepng($new_image,'test-filled.png');

4. Advanced operations of GD library

1. Watermark processing

1) Add text watermark

$watermark_text = 'COPYRIGHT';
$font_size = 20;
$font_color = imagecolorallocate($new_image,0,0,0);
imagettftext($new_image ,$font_size,0,10,20,$font_color,'arial.ttf',$watermark_text);
imagepng($new_image,'test-watermark.png');

2) Add image Watermark

$watermark_image = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark_image);
$watermark_height = imagesy($watermark_image);
$pos_x = ($new_width - $watermark_width) / 2;
$pos_y = ($new_height - $watermark_height) / 2;
imagecopy($new_image,$watermark_image,$pos_x,$pos_y,0,0,$watermark_width,$watermark_height);
imagepng($new_image,'test-watermark.png');

2. Drawing operation

1) Draw a straight line

$line_color = imagecolorallocate($new_image,0, 0,255);
imageline($new_image,0,0,$new_width,$new_height,$line_color);
imagepng($new_image,'test-line.png');

2) Draw a rectangle

$rect_color = imagecolorallocate($new_image,0,255,0);
imagerectangle($new_image,20,20,$new_width-20,$new_height-20,$rect_color);
imagepng($ new_image,'test-rectangle.png');

3) Draw a circle

$circle_color = imagecolorallocate($new_image,255,0,0);
$circle_center_x = $new_width/2 ;
$circle_center_y = $new_height/2;
$circle_diameter = $new_height * 0.8;
$circle_radius = $circle_diameter / 2;
imageellipse($new_image,$circle_center_x,$circle_center_y,$circle_diameter ,$circle_diameter,$circle_color);
imagepng($new_image,'test-circle.png');

5. Summary

This article introduces the basic operations and advanced operations of the GD library Operations, including image creation, image processing, operating image elements, watermark processing, drawing operations, etc. The GD library is one of the most practical image processing tools in PHP development. It can be used to create image verification codes, generate QR codes, charts, posters, etc. Mastering the skills to use the GD library can help PHP developers complete business needs more efficiently.

The above is the detailed content of GD library operation guide 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