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');
$blurred_image = imagefilter($new_image,IMG_FILTER_GAUSSIAN_BLUR);
imagepng($blurred_image,'test-blurred.png');
$pixel = imagecolorat($new_image,50 ,50);
$red = ($pixel >> 16) & 0xFF;
$green = ($pixel >> 8) & 0xFF;
$blue = $pixel & 0xFF ;
$new_color = imagecolorallocate($new_image,255,0,0);
imagesetpixel($new_image,50,50,$new_color);
imagepng($new_image,'test-pixel.png');
$fill_color = imagecolorallocate($new_image,0,255,0);
imagefill($new_image,0, 0,$fill_color);
imagepng($new_image,'test-filled.png');
$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');
$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');
$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');
$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');
$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');
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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function