Home > Article > Backend Development > How to generate dynamic text and image watermarks using PHP arrays
How to use PHP arrays to generate dynamic text and image watermarks
Introduction:
In Web development, dynamically generating text and image watermarks is a very common requirement. PHP array is a very powerful data structure. When generating dynamic text and image watermarks, we can use PHP arrays to achieve fast, flexible and efficient operations. This article will introduce how to use PHP arrays to generate dynamic text and image watermarks, and provide relevant code examples.
1. Generate dynamic text watermark
$watermark = array( 'username' => 'John Smith', 'timestamp' => date('Y-m-d H:i:s') );
$textWatermark = sprintf('Username: %s, Generated at: %s', $watermark['username'], $watermark['timestamp']);
In the above example, we used the sprintf function to fill the username and generation time into the text watermark template.
2. Generate dynamic image watermark
$watermark = array( 'avatar' => '/path/to/avatar.jpg', 'timestamp' => date('Y-m-d H:i:s') );
// 创建图像资源 $originalImage = imagecreatefromjpeg($watermark['avatar']); // 获取原始图像宽度和高度 $originalWidth = imagesx($originalImage); $originalHeight = imagesy($originalImage); // 创建包含生成时间的文本水印 $textWatermark = $watermark['timestamp']; $textWidth = imagefontwidth(5) * strlen($textWatermark); $textHeight = imagefontheight(5); // 创建水印图像资源 $watermarkImage = imagecreatetruecolor($originalWidth, $originalHeight); // 设置透明背景 imagefill($watermarkImage, 0, 0, imagecolorallocatealpha($watermarkImage, 255, 255, 255, 127)); imagesavealpha($watermarkImage, true); // 复制原始图像到水印图像 imagecopy($watermarkImage, $originalImage, 0, 0, 0, 0, $originalWidth, $originalHeight); // 在水印图片上添加文本水印 imagettftext($watermarkImage, 12, 0, 10, $originalHeight - 10, imagecolorallocate($watermarkImage, 255, 255, 255), '/path/to/font.ttf', $textWatermark); // 输出合成后的图像 header('Content-Type: image/jpeg'); imagejpeg($watermarkImage); // 释放资源 imagedestroy($originalImage); imagedestroy($watermarkImage);
In the above example, we first create the original image resource using the imagecreatefromjpeg function. Then, create an image resource equal to the size of the original image through the imagecreatetruecolor function to store the synthesized watermark image. Next, we use the imagecopy function to copy the original image to the watermark image. Finally, use the imagettftext function to add text watermarks to the watermark image, and output the synthesized image to the browser through the imagejpeg function.
Conclusion:
Using PHP arrays to generate dynamic text and image watermarks is a very flexible and efficient method. We can use PHP arrays combined with built-in functions and the functions of the GD library to quickly implement various needs. Through the introduction of this article, I believe that readers have a deeper understanding of how to use PHP arrays to generate dynamic text and image watermarks, and can flexibly apply them according to specific needs.
The above is the detailed content of How to generate dynamic text and image watermarks using PHP arrays. For more information, please follow other related articles on the PHP Chinese website!