图片生成
1. 安装GD库
sudo apt-get install php-gd
2. 创建图片资源
<?php
$image = imagecreatetruecolor(100, 100);
?>
3. 设置图片颜色
<?php
$background_color = imagecolorallocate($image, 255, 255, 255);
?>
4. 绘制图形
PHP提供了丰富的绘图函数,例如imagefilledrectangle()、imagestring()、imageline()等。以下示例使用imagefilledrectangle()函数绘制一个红色矩形:
<?php
$rectangle_color = imagecolorallocate($image, 255, 0, 0);
imagefilledrectangle($image, 0, 0, 100, 100, $rectangle_color);
?>
5. 输出图片
<?php
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
?>
图片打印
1. 打印图片到文件
<?php
$image = imagecreatetruecolor(100, 100);
$background_color = imagecolorallocate($image, 255, 255, 255);
$rectangle_color = imagecolorallocate($image, 255, 0, 0);
imagefilledrectangle($image, 0, 0, 100, 100, $rectangle_color);
imagepng($image, 'output.png');
imagedestroy($image);
?>
2. 打印图片到PDF
<?php
require_once('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image('output.png', 10, 10, 80, 60, 'PNG');
$pdf->Output('output.pdf', 'F');
?>