引言

准备工作

在开始之前,请确保你的环境中已经安装了PHP和GD库。GD库是PHP的一个扩展,它提供了图像处理的功能。

步骤一:获取和加载图片

$src_image = imagecreatefromjpeg('path/to/image.jpg');

步骤二:创建文本标签

$tag_image = imagecreatetruecolor(100, 50);

步骤三:设置字体和颜色

选择一个合适的字体和颜色来显示标签。可以使用PHP的imagettftext()函数来设置字体和颜色。

$font_file = 'path/to/font.ttf';
$font_size = 14;
$color = imagecolorallocate($tag_image, 255, 255, 255); // 白色

步骤四:添加标签文本

将标签文本添加到标签图像上。

$text = 'Example Tag';
$angle = 0;
$x = 10;
$y = 30;
imagettftext($tag_image, $font_size, $angle, $x, $y, $color, $font_file, $text);

步骤五:合并图片

imagecopymerge($src_image, $tag_image, 50, 50, 0, 0, 100, 50, 100);

步骤六:输出图片

header('Content-Type: image/jpeg');
imagejpeg($src_image);

示例代码

<?php
$src_image = imagecreatefromjpeg('path/to/image.jpg');
$tag_image = imagecreatetruecolor(100, 50);
$font_file = 'path/to/font.ttf';
$font_size = 14;
$color = imagecolorallocate($tag_image, 255, 255, 255);
$text = 'Example Tag';
$angle = 0;
$x = 10;
$y = 30;
imagettftext($tag_image, $font_size, $angle, $x, $y, $color, $font_file, $text);
imagecopymerge($src_image, $tag_image, 50, 50, 0, 0, 100, 50, 100);
header('Content-Type: image/jpeg');
imagejpeg($src_image);
?>