GD庫(kù)實(shí)現(xiàn)webp轉(zhuǎn)換jpg的PHP程序
PHP程序來(lái)執(zhí)行webp格式轉(zhuǎn)換成jpg格式有幾種方法:一是安裝imagemagick實(shí)現(xiàn),二是安裝GD庫(kù)實(shí)現(xiàn),可以直接用dwebp命令。本文我們將介紹使用PHP的圖像處理庫(kù)GD,編寫一個(gè)簡(jiǎn)單的PHP程序來(lái)完成這個(gè)任務(wù)。
首先,確保你的PHP環(huán)境已經(jīng)安裝了GD庫(kù)。你可以通過(guò)運(yùn)行`php -m`命令來(lái)檢查是否已安裝。
接下來(lái),在你的PHP代碼中,你需要使用`imagecreatefromwebp()`函數(shù)來(lái)創(chuàng)建一個(gè)GD圖像資源,將webp格式的圖片加載進(jìn)來(lái)。然后,你可以使用`imagejpeg()`函數(shù)將該GD圖像資源以jpg格式保存到指定路徑。
webp轉(zhuǎn)換jpg的PHP程序
$webpPath = 'input.webp'; // webp圖片的路徑 $jpgPath = 'output.jpg'; // 轉(zhuǎn)換后的jpg圖片的保存路徑 // 創(chuàng)建GD圖像資源 $image = imagecreatefromwebp($webpPath); // 保存為jpg圖片 imagejpeg($image, $jpgPath, 100); // 第三個(gè)參數(shù)是JPG圖片質(zhì)量,范圍為0-100,100表示最高質(zhì)量 // 釋放資源 imagedestroy($image); echo "轉(zhuǎn)換完成!";
將上述代碼保存為一個(gè)PHP文件(比如`webp2jpg.php`),然后在瀏覽器中訪問(wèn)該文件,即可執(zhí)行webp格式轉(zhuǎn)換成jpg格式的任務(wù)。請(qǐng)確保在`$webpPath`中填寫正確的webp圖片路徑以及在`$jpgPath`中指定保存路徑。
需要注意的是,使用GD庫(kù)進(jìn)行webp到j(luò)pg格式轉(zhuǎn)換可能會(huì)導(dǎo)致一些質(zhì)量損失,因?yàn)閣ebp(有損壓縮)和jpg(有損壓縮)采用了不同的壓縮算法。如果你需要更高質(zhì)量的轉(zhuǎn)換,建議安裝libwebp擴(kuò)展或使用其他專門處理webp格式的工具。
希望這個(gè)簡(jiǎn)單的示例能幫助你理解如何編寫用PHP將webp格式轉(zhuǎn)換成jpg格式的程序。
PHP imagecreatefromwbmp()
imagecreatefromwbmp()函數(shù)是PHP中的內(nèi)置函數(shù),用于從WBMP文件或URL創(chuàng)建新圖像。 WBMP(無(wú)線應(yīng)用協(xié)議位圖格式)是為移動(dòng)計(jì)算設(shè)備優(yōu)化的單色圖形文件格式??梢栽诔绦蛑羞M(jìn)一步處理此加載的圖像。從WBMP文件加載圖像后要編輯圖像時(shí),通常使用此函數(shù)??梢允褂胕magewbmp()函數(shù)將圖像轉(zhuǎn)換為WBMP。
用法:
resource imagecreatefromwbmp( string $filename )
參數(shù):該函數(shù)接受單個(gè)參數(shù)$filename,該參數(shù)保存圖像的名稱。
返回值:成功時(shí)此函數(shù)返回圖像資源標(biāo)識(shí)符,錯(cuò)誤時(shí)返回FALSE。
gd庫(kù)
一、什么是gd庫(kù)?
GD庫(kù)是一組用于創(chuàng)建和處理各種圖像格式的庫(kù)函數(shù),是PHP中最為常用的圖像處理庫(kù)之一。
二、安裝GD庫(kù)
在CentOS/RedHat下安裝GD庫(kù)
1.安裝PHP的GD擴(kuò)展庫(kù)
yum install php-gd
2.重啟web服務(wù)器
service httpd restart
3.查看PHP支持的GD庫(kù)版本
php -i | grep -i gd
在Ubuntu/Debian下安裝GD庫(kù)
1.安裝php5-gd模塊
apt-get update && apt-get install php5-gd
2.重啟web服務(wù)器
service apache2 restart
3.查看PHP支持的GD庫(kù)版本
php -i | grep -i gd
三、GD庫(kù)的基本操作
1.創(chuàng)建圖像
1)創(chuàng)建一個(gè)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)釋放內(nèi)存
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_image,$src_image,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,'test-bordered.png');
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庫(kù)的高級(jí)操作
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 = ($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.畫圖操作
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); 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_x,$circle_center_y,$circle_diameter,$circle_diameter,$circle_color); imagepng($new_image,'test-circle.png');
總結(jié)
到此這篇關(guān)于GD庫(kù)實(shí)現(xiàn)webp轉(zhuǎn)換jpg的PHP程序的文章就介紹到這了,更多相關(guān)PHP的GD庫(kù)實(shí)現(xiàn)webp轉(zhuǎn)換jpg內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
zend optimizer在wamp的基礎(chǔ)上安裝圖文教程
在用wampserver集成開(kāi)發(fā)環(huán)境下,有時(shí)會(huì)碰到一些開(kāi)源程序需要zend optimizer的支持,下面我用的wamp的版本是2.0,optimizer的版本是ZendOptimizer-3.3.3-Windows-i3862013-10-10php mysql_real_escape_string addslashes及mysql綁定參數(shù)防SQL注入攻擊
這篇文章主要介紹了php mysql_real_escape_string addslashes及mysql綁定參數(shù)防SQL注入攻擊的相關(guān)資料,需要的朋友可以參考下2016-12-12Yii模型操作之criteria查找數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Yii模型操作之criteria查找數(shù)據(jù)庫(kù)的方法,結(jié)合實(shí)例形式分析了Yii模型中criteria的實(shí)例化與查詢操作相關(guān)技巧,需要的朋友可以參考下2016-07-07