// 设置图片路径和字体路径
$imgPath = 'image.jpg';
$fontPath = 'arial.ttf';
// 创建图像对象
$img = imagecreatefromjpeg($imgPath);
// 设置颜色
$white = imagecolorallocate($img, 255, 255, 255);
// 添加文字
$text = 'Hello World!';
$font_size = 20;
$x = 10;
$y = 50;
imagettftext($img, $font_size, 0, $x, $y, $white, $fontPath, $text);
// 添加水印
$watermark = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = imagesx($img) - $watermark_width - 10;
$dest_y = imagesy($img) - $watermark_height - 10;
imagecopy($img, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
// 输出图像
header('Content-Type: image/jpeg');
imagejpeg($img);
// 释放内存
imagedestroy($img);
imagedestroy($watermark);
代码解释:
首先,我们需要设置要操作的图像路径和字体路径。
使用 imagecreatefromjpeg() 函数创建一个图像对象。
使用 imagecolorallocate() 函数设置颜色。
使用 imagettftext() 函数在指定位置添加文本。
使用 imagecreatefrompng() 函数创建水印图像对象。
使用 imagesx() 和 imagesy() 函数获取水印图像的宽度和高度。
使用 imagecopy() 函数将水印添加到图像的指定位置。
使用 header() 函数设置图像的 MIME 类型,并使用 imagejpeg() 函数输出图像。
最后,使用 imagedestroy() 函数释放内存。
请注意,此示例中的水印图像是一个 PNG 图像,但可以使用任何支持的图像格式,如 GIF 或 JPEG。
实际项目中使用:
项目使用:
<?php
namespace App\Service;
class CertificationService
{
public static function generateCertification(): void
{
// 设置图片路径和字体路径
$imgPath = './statics/img/certificationTemplate.png';
$fontPath = './stsong.ttf';
// 创建图像对象
$img = imagecreatefrompng($imgPath);
// 设置颜色
$white = imagecolorallocate($img, 255, 255, 255);
// 添加文字
$name = 'Ryan Gislong';
$font_size = 20;
$x = 100;
$y = 496-19; // 496 - (10 + ( 38 - 20 ) / 2)
imagettftext($img, $font_size, 0, $x, $y, $white, $fontPath, $name);
// 添加文字
$course = 'Information for Editors';
$font_size = 20;
$x = 100;
$y = 587 - 14; // 587 - (10 + (28 - 20) / 2)
imagettftext($img, $font_size, 0, $x, $y, $white, $fontPath, $course);
// 添加文字
$number = 'JTT 108';
$font_size = 20;
$x = 292;
$y = 692 + 24; // 692 + 20 + (28 - 20) / 2
imagettftext($img, $font_size, 0, $x, $y, $white, $fontPath, $number);
// 添加文字
$createdAt = '08.08.2023';
$font_size = 20;
$right_margin = 54; // 右侧边距
$bbox = imagettfbbox($font_size, 0, $fontPath, $createdAt);
$text_width = $bbox[2] - $bbox[0];
$x = imagesx($img) - $text_width - $right_margin;
$y = 443 + 24; // 692 + 20 + (28 - 20) / 2
imagettftext($img, $font_size, 0, $x, $y, $white, $fontPath, $createdAt);
// 添加文字
$signatureName = 'John Doe';
$font_size = 20;
$right_margin = 54; // 右侧边距
$bbox = imagettfbbox($font_size, 0, $fontPath, $signatureName);
$text_width = $bbox[2] - $bbox[0];
$x = imagesx($img) - $text_width - $right_margin;
$y = 471 + 24; // 692 + 20 + (28 - 20) / 2
imagettftext($img, $font_size, 0, $x, $y, $white, $fontPath, $signatureName);
// 添加水印
$watermark = imagecreatefrompng('./statics/img/signature.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = imagesx($img) - $watermark_width - 56;
$dest_y = 507;
imagecopy($img, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
// 输出图像
// header('Content-Type: image/jpeg');
// imagepng($img);
imagewebp($img, './statics/img/new.png');
// 释放内存
imagedestroy($img);
imagedestroy($watermark);
}
}
