How to generate dynamic text and image watermarks using PHP arrays

WBOY
Release: 2023-07-15 19:44:01
Original
669 people have browsed it

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

  1. Create a text watermark array:
    Suppose we want to generate a text watermark whose content is the user's username and generation time. First, we can create a PHP array containing the username and generation time:
$watermark = array(
    'username' => 'John Smith',
    'timestamp' => date('Y-m-d H:i:s')
);
Copy after login
  1. Use the array to generate the text watermark:
    Next, we can use the values ​​in the PHP array to generate Text Watermark:
$textWatermark = sprintf('Username: %s, Generated at: %s', $watermark['username'], $watermark['timestamp']);
Copy after login

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

  1. Create an image watermark array:
    If we want to generate an image watermark, including the user's avatar and generation time. We can create a PHP array containing the avatar image path and generation time:
$watermark = array(
    'avatar' => '/path/to/avatar.jpg',
    'timestamp' => date('Y-m-d H:i:s')
);
Copy after login
  1. Use the array to generate the image watermark:
    Next, we can use the PHP GD library to combine the avatar and Generation time is synthesized into an image watermark:
// 创建图像资源
$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);
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!