首頁  >  文章  >  後端開發  >  PHP中的GD函式庫操作指南

PHP中的GD函式庫操作指南

王林
王林原創
2023-05-20 14:40:403646瀏覽

一、什麼是GD庫?

GD庫是一組用於建立和處理各種影像格式的函式庫函數,是PHP中最常用的影像處理庫之一。

二、安裝GD庫

在CentOS/RedHat下安裝GD庫

1.安裝PHP的GD擴充庫

yum install php-gd

2.重啟網頁伺服器

service httpd restart

3.查看PHP支援的GD函式庫版本

php -i | grep -i gd

在Ubuntu/Debian下安裝GD函式庫

1.安裝php5-gd模組

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

2.重啟web伺服器

service apache2 restart

3.查看PHP支援的GD庫版本

php -i | grep -i gd

#三、GD庫的基本操作

1.創建圖像

1)創建一個200X200像素的黑色圖像
$image = imagecreate(200,200);
$black = imagecolorallocate( $image,0,0,0);
imagefill($image,0,0,$black);

2)在圖像中加入文字
$white = imagecolorallocate($image, 255,255,255);
$text = 'Hello, GD!';
imagettftext($image,20,0,70,100,$white,'arial.ttf',$text);

#3 )儲存映像到檔案
imagepng($image,'test.png');

4)釋放記憶體
imagedestroy($image);

#2.映像處理

1)縮放映像
$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_width,$new_height);
imagecopyresampled($new_image,0,0,0,0 0,0,$new_width,$new_height,$src_width,$src_height);

imagepng($new_image,'test-resized.png');


#2)加入邊框
#$border_color = imagecolorallocate($new_image,128,128,128);
imagerectangle($new_image,0,0,$new_width-1,$new_height-1,$border_color);

imagepng($new_image,'ptest-bordered. ;


3)裁切影像
$cropped_image = imagecrop($new_image,['x'=>40,'y'=>40,'width'=>100,'height' =>100]);

imagepng($cropped_image,'test-cropped.png');


4)模糊映像
$blurred_image = imagefilter($new_image,IMG_FILTER_GAUSSIAN_BLUR);

imagepng($blurred_image,'test-blurred.png');

#3.操作映像元素


1)取得像素RGB值
$pixel = imagecolorat($new_image,50 ,50);
$red = ($pixel >> 16) & 0xFF;
$green = ($pixel >> 8) & 0xFF;

$blue = $pixel & 0xFF ;


2)修改像素RGB值
$new_color = imagecolorallocate($new_image,255,0,0);
imagesetpixel($new_image,50,50,$new_color);

imagepng($new_image,'test-pixel.png');


3)填滿映像
$fill_color = imagecolorallocate($new_image,0,255,0);
imagefill($new_image,0, 0,$fill_color);

imagepng($new_image,'test-filled.png');

四、GD庫的高階操作

1.水印處理


1)新增文字浮水印
$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)新增圖片浮水印
$watermark_image = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark_image);
$watermark_height = imagesy($watermark_image);
pos_x = (0_dpos_xd $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.畫圖操作


1)畫直線
$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)畫出矩形
$rect_color = imagecolorallocate($new_image,0,255,0);
imagerectangle($new_image,20,20,$new_width-20,$new_height-20,$rect_color);#new_width-20,$new_height-20,$rect_color);

imagepng($ new_image,'test-rectangle.png');


3)畫圓
$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_center_xir_; ,$circle_diameter,$circle_color);

imagepng($new_image,'test-circle.png');

五、總結

#本文介紹了GD庫的基本操作和高級操作,包括影像建立、影像處理、操作影像元素、浮水印處理、畫圖操作等內容。 GD庫是PHP開發中非常實用的影像處理工具之一,可用於製作圖片驗證碼、產生二維碼、圖表、海報等。掌握GD庫的使用技能可以幫助PHP開發者更有效率地完成業務需求。 ###

以上是PHP中的GD函式庫操作指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn