PHP 中的 imagecreate() 函数

phpserver side programmingprogramming更新于 2025/7/6 14:52:17

imagecreate() 函数用于创建新图像。建议使用 imagecreatetruecolor() 而不是 imagecreate() 来创建图像。这是因为图像处理会以 imagecreatetruecolor() 能够创建的最高质量图像为基础。

语法

imagecreate( $width, $height )

参数

  • width:图像宽度

  • height:图像高度

返回

imagecreate() 函数成功时返回图像资源标识符,失败时返回 FALSE。

示例

以下是示例:

<?php
   $img = imagecreate(500, 300);
   $bgcolor = imagecolorallocate($img, 150, 200, 180);
   $fontcolor = imagecolorallocate($img, 120, 60, 200);
   imagestring($img, 12, 150, 120, "Demo Text1", $fontcolor);
   imagestring($img, 3, 150, 100, "Demo Text2", $fontcolor);
   imagestring($img, 9, 150, 80, "Demo Text3", $fontcolor);
   imagestring($img, 12, 150, 60, "Demo Text4", $fontcolor);
   header("Content-Type: image/png");
   imagepng($img);
   imagedestroy($img);
?>

输出

输出结果如下:


相关文章