在原始图片上增加文字、图片水印,注意需要打开GD。
// 清除所有缓冲区的内容
ob_clean();
// 检查原始图片是否存在
$originalImagePath = './static/bg.png';
if (!file_exists($originalImagePath)) {
die("Original image not found.");
}
// 加载原始图片
$originalImage = imagecreatefrompng($originalImagePath);
// ==========以下为添加文字============
// 0 0 0 文字颜色 0-255
$fontColor = imagecolorallocate($originalImage, 0, 0, 0);
// 设置字体大小和位置
$fontSize = 15;
// 文本的位置
$textX = 100;
$textY = 100;
// 添加文本到图片上,必须保证 “./static/simhei.ttf” 字体文件存在
imagettftext($originalImage, $fontSize, 0, $textX, $textY, $fontColor, './static/simhei.ttf', '测试字体');
// ==========以下为添加水印图片============
$watermarkImagePath = './static/watermark.png';
if (file_exists($watermarkImagePath)) {
// 加载水印图片
$watermarkImage = imagecreatefrompng($watermarkImagePath);
$watermarkWidth = imagesx($watermarkImage);
$watermarkHeight = imagesy($watermarkImage);
// 计算并调整水印图片的比例大小
$newWidth = 200;
$newHeight = ($watermarkHeight / $watermarkWidth) * $newWidth;
$newImage = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($newImage, $watermarkImage, 0, 0, 0, 0, $newWidth, $newHeight, $watermarkWidth, $watermarkHeight);
// 添加到原始图片里
imagecopy($originalImage, $newImage, 110, 90, 0, 0, $newWidth, $newHeight);
// 释放资源
imagedestroy($newImage);
imagedestroy($watermarkImage);
}
// 设置响应头为图像类型
header('Content-Type: image/png');
// 输出图片
imagepng($originalImage);
imagedestroy($originalImage);
exit();
发表回复
要发表评论,您必须先登录。