當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


PHP imagegd()用法及代碼示例

imagegd()函數是PHP中的內置函數,用於將GD圖像輸出到瀏覽器或文件。這對於將任何其他圖像類型轉換為gd最為有用。 imagecreatefromgd()函數可用於進一步讀取gd圖像。

用法:

bool imagegd( resource $image, float $to)

參數:該函數接受上述和以下描述的兩個參數:


  • $image:它指定要處理的圖像。
  • $to (Optional):它指定將文件保存到的路徑。

返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。

異常:該函數在出錯時引發Exception。

下麵給出的程序說明了PHP中的imagegd()函數:程序1(查看GD文件):

<?php 
// Create a blank image and add text 
$im = imagecreatetruecolor(100, 100); 
$text_color = imagecolorallocate($im, 10, 10, 51); 
imagestring($im, 0, 20, 20,  "GeeksforGeeks", $text_color); 
  
// Output the image as string 
imagegd($im); 
?>

輸出:

This will output the image in the form of string as gd isn't supported in browser.

程序2(將圖像轉換為gd):

<?php 
// Create a image from png 
$image = imagecreatefrompng('https://media.geeksforgeeks.org/wp-content/uploads/geeksforgeeks-13.png'); 
  
// Convert into GD and save to the same folder 
imagegd($image, 'geeksforgeeks.gd'); 
?>

輸出:

This will save a image with name geeksforgeeks.gd in the same folder.

參考: https://www.php.net/manual/en/function.imagegd.php



相關用法


注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | imagegd() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。