PHP Primer 023 : 图像处理(一)

Ezra PHP

这一篇主要介绍 PHP 中的 Grahpic Device 库的使用。

CD 已经在 PHP 中集成,我们要做的很简单:

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

新建画布

接下来,要画图首先当然要有画布:

$img = imagecreatetruecolor(100, 100);

颜色设定

为画笔设定颜色吧:

$red = imagecolorallocate($img, 0xFF, 0x00, 0x00);
$black = imagecolorallocate($img, 0x00, 0x00, 0x00);

填充画布

imagefill($img, 0, 0, $red);

线条绘制

imageline($img, 0, 0, 100, 100, $black);

输出图像

输出到浏览器中:

imagepng($img);

释放内存

imagedestroy($img);
?>