腾讯云618限量抢购
  1. PHP+MySQL数据库教程
  2. PHP进阶教程
  3. ThinkPHP 3.0/3.1 教程

PHP 创建图像、销毁图像 imagecreate 与 imagedestroy 函数

最后更新:2010-11-08 18:02阅读:37835

imagecreate()

如果我们要对图像进行处理,就如其它图像处理软件一样,需要创建一块画布。imagecreate() 和 imagecreatetruecolor() 函数用于创建一幅空白图像。

语法:

resource imagecreate( int x, int y )

参数 x ,y 分别为要创建图像的宽度和高度像素值,返回一个图像资源。

例子:

<?
header("Content-type: image/png");

//创建图像
$im = @imagecreate(200, 50) or die("创建图像资源失败");

//图片背景颜色
$bg = imagecolorallocate($im, 255, 255, 255);

//文字颜色
$text_color = imagecolorallocate($im, 0, 0, 255);

//水平画一行字,要输出中文等需要 TTF 字体支持的请使用 magettftext() 函数
imagestring($im, 5, 0, 0, "Hello world!", $text_color);

//以PNG格式输出图像
imagepng($im);

//销毁图像资源
imagedestroy($im);
?>

该例子以图像格式输出一行文字:Hello world! 。例子中用到的其他函数,将在后面逐一介绍。

imagecreatetruecolor()

imagecreatetruecolor() 功能与 imagecreate() 类似,创建一幅真彩色的图像,从而支持更为丰富的色彩。

语法:

resource imagecreatetruecolor( int x, int y )

注意:本函数不能用于 GIF 文件格式。

imagedestroy()

图像处理完成后,使用 imagedestroy() 指令销毁图像资源以释放内存,虽然该函数不是必须的,但使用它是一个好习惯。

语法:

bool imagedestroy( resource image )

具体使用可见上面创建图像例子。


本章节内容共分 10 部分:
  1. 1. PHP 获取图像信息 getimagesize 函数
  2. 2. PHP 创建图像、销毁图像 imagecreate 与 imagedestroy 函数
  3. 3. PHP 分配、取消图像颜色 imagecolorallocate 与 imagecolordeallocate 函数
  4. 4. PHP 拷贝图像 imagecopy 与 imagecopyresized 函数
  5. 5. PHP 绘制线段与圆弧 imageline、imagesetstyle 与 imagearc 函数
  6. 6. PHP 图像处理
  7. 7. PHP 载入图像 imagecreatefrom_gif_jpeg_png 系列函数
  8. 8. PHP 合并图像 imagecopymerge 函数(水印制作实例)
  9. 9. PHP 输出图像 imagegif 、imagejpeg 与 imagepng 函数
  10. 10. PHP 图像填充 imagefill、imagefilledarc 与 imagefilledrectangle() 函数
腾讯云618限量抢购