使用 PHP 生成图像

使用 PHP 生成图像

在 PHP 中使用 GD 库生成 JPEG 图像

在 PHP 中使用 GD 库生成 PNG 图像

在 PHP 中使用 GD 库绘制图像

PHP 有一个用于图像处理操作的内置库 GD。该库还可以生成不同格式的图像。

GD 库默认不启用;你需要从 php.ini 启用它。

在 PHP 中使用 GD 库生成 JPEG 图像

首先,检查 GD 库是否启用;可以从 phpinfo() 或 php.ini 检查。一旦从 php.ini 启用,我们就可以开始创建图像了。

// Create a blank image and add some content

$new_image = imagecreatetruecolor(400, 400);

$bg_color = imagecolorallocate($new_image, 125, 125, 125);

// Set the background color

imagefill($new_image, 0, 0, $bg_color);

$text = imagecolorallocate($new_image, 225, 225, 225);

imagestring($new_image, 25, 50, 200, 'Welcome to Delftstack Tutorial.', $text);

imagestring($new_image, 15, 50, 240, 'This is JPEG image generation', $text);

// Save the image as jpeg

imagejpeg($new_image, 'delftstack.jpeg');

// Free the memory

imagedestroy($new_image);

?>

上面的代码生成一个 400x400 的 jpeg 图像,给定的内容为字符串,背景颜色为灰色。

输出:

在 PHP 中使用 GD 库生成 PNG 图像

生成 png 图像的过程几乎相同,只是将图像保存为 png。

// Create a blank image and add some content

$new_image = imagecreatetruecolor(400, 400);

$bg_color = imagecolorallocate($new_image, 125, 125, 125);

// Set the background color

imagefill($new_image, 0, 0, $bg_color);

$text = imagecolorallocate($new_image, 225, 225, 225);

imagestring($new_image, 25, 50, 200, 'Welcome to Delftstack Tutorial.', $text);

imagestring($new_image, 15, 50, 240, 'This is PNG image generation', $text);

// Save the image as png

imagepng($new_image, 'delftstack.png');

// Free the memory

imagedestroy($new_image);

?>

输出:

在 PHP 中使用 GD 库绘制图像

GD 库几乎没有内置函数来绘制特定图形,包括 imagearc()、imagefilledpolygon()、imagechar() 和 imageellipse()。

绘制弧线:

$new_image = imagecreatetruecolor(200, 200);

$color_white = imagecolorallocate($new_image, 255, 255, 255);

$color_red = imagecolorallocate($new_image, 255, 0, 0);

$color_green = imagecolorallocate($new_image, 0, 255, 0);

$color_blue = imagecolorallocate($new_image, 0, 0, 255);

$bg_color = imagecolorallocate($new_image, 125, 125, 125);

imagefill($new_image, 0, 0, $bg_color);

// drawing a smiling face

//head

imagearc($new_image, 100, 100, 200, 200, 0, 360, $color_white);

// smile

imagearc($new_image, 100, 100, 150, 150, 25, 155, $color_red);

// eyes

imagearc($new_image, 60, 75, 50, 50, 0, 360, $color_green);

imagearc($new_image, 140, 75, 50, 50, 0, 360, $color_blue);

//save image

imagepng($new_image,'arcimage.png');

// free memory

imagedestroy($new_image);

?>

输出:

让我们画一个多边形:

// value points for polygon

$polygon_values = array(

30, 80,

50, 240,

29, 80,

240, 80,

58, 60,

100, 100

); // The random (x,y) points

$new_image = imagecreatetruecolor(250, 250);

$bg_color = imagecolorallocate($new_image, 125, 125, 125);

$color_blue = imagecolorallocate($new_image, 0, 0, 255);

// Fill the background color

imagefilledrectangle($new_image, 0, 0, 249, 249, $bg_color);

// draw the polygon

imagefilledpolygon($new_image, $polygon_values, 6, $color_blue);

//save image

imagepng($new_image, 'polygon.png');

imagedestroy($new_image);

?>

上面的代码将生成一个具有给定随机值的多边形。

输出:

让我们尝试绘制矩形。

$new_image = imagecreatetruecolor(400, 400);

$bg_color = imagecolorallocate($new_image, 255, 255, 255);

// Set the background color

imagefill($new_image, 0, 0, $bg_color);

// colors of rectangles

$color_magenta = imagecolorallocate($new_image, 255,0,255);

$color_olive = imagecolorallocate($new_image, 128,128,0);

$color_green = imagecolorallocate($new_image, 0,128,0);

//three rectangles with different size and color

imagerectangle($new_image, 100, 100, 300, 300, $color_magenta);

imagerectangle($new_image, 90, 120, 240, 200, $color_olive );

imagerectangle($new_image, 200, 240, 150, 320, $color_green);

//save image

imagepng($new_image, 'rectangles.png');

imagedestroy($new_image);

?>

上面的代码将在一张图像中生成三个矩形。

输出:

GD 库有更多的功能来绘制不同的图像。这些函数可以很容易地与给定的参数一起使用。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe

相关发现

演员张钧甯的身材和颜值,并没有电视上那么美,她43岁太显老
国足为什么要进世界杯
best365官网登录入口

国足为什么要进世界杯

🌼 07-12 🌻 340
韩媒:韩国队在国内突遭兴奋剂检测,事先未通知且影响训练
简单的装备出行才是最理想的选择!
best365官网登录入口

简单的装备出行才是最理想的选择!

🌼 01-31 🌻 9180