使用 PHP 生成图像

Sheeraz Gul 2024年2月15日
  1. 在 PHP 中使用 GD 库生成 JPEG 图像
  2. 在 PHP 中使用 GD 库生成 PNG 图像
  3. 在 PHP 中使用 GD 库绘制图像
使用 PHP 生成图像

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

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

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

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

<?php
// 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 图像,给定的内容为字符串,背景颜色为灰色。

输出:

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

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

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

<?php
// 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);
?>

输出:

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

在 PHP 中使用 GD 库绘制图像

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

绘制弧线:

<?php
$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);

?>

输出:

使用 GD 库在 PHP 中绘制笑脸弧形图像

让我们画一个多边形:

<?php
// 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);
?>

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

输出:

使用 GD 库在 PHP 中绘制多边形图像

让我们尝试绘制矩形。

<?php
$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 库在 PHP 中绘制矩形图像

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

作者: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

相关文章 - PHP Image