使用 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);
?>
上面的代码生成一个 400×400 的 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);
?>
输出:
在 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);
?>
输出:
让我们画一个多边形:
<?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);
?>
上面的代码将生成一个具有给定随机值的多边形。
输出:
让我们尝试绘制矩形。
<?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
库有更多的功能来绘制不同的图像。这些函数可以很容易地与给定的参数一起使用。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。