当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


PHP imagegd2()用法及代码示例

imagegd2()函数是PHP中的内置函数,用于将GD2图像输出到浏览器或文件。这对于将任何其他图像类型转换为gd2最有用。 imagecreatefromgd2()函数可用于进一步读取gd2图像。

用法:

bool imagegd2( resource $image, float $to, 
             int $chunk_size, int $type )

参数:该函数接受上述和以下所述的四个参数:


  • $image:它指定要处理的图像。
  • $to (Optional):它指定将文件保存到的路径。
  • $chunk_size(可选):它指定块的大小。
  • $type (Optional):它指定要使用的压缩类型,可以是IMG_GD2_RAW或IMG_GD2_COMPRESSED中的一种。

返回值:如果成功,则此函数返回TRUE;如果失败,则返回FALSE。

异常:该函数在出错时引发Exception。

以下示例说明了PHP中的imagegd2()函数:

范例1:查看GD2文件。

<?php 
  
// Create a blank image and add text 
$im = imagecreatetruecolor(200, 200); 
$text_color = imagecolorallocate($im, 233, 14, 91); 
imagestring($im, 0, 30, 30,  "GeeksforGeeks", $text_color); 
  
// Output the image as string 
imagegd2($im); 
?>

输出:

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

范例2:将图像转换为GD2。

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

输出:

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

参考: https://www.php.net/manual/en/function.imagegd2.php



相关用法


注:本文由纯净天空筛选整理自gurrrung大神的英文原创作品 PHP | imagegd2() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。