Home > Backend Development > PHP Tutorial > PHP image processing skills: how to add watermarks and text

PHP image processing skills: how to add watermarks and text

王林
Release: 2023-08-25 16:32:01
Original
1972 people have browsed it

PHP image processing skills: how to add watermarks and text

PHP image processing skills: how to add watermarks and text

In website development, image processing is a common requirement. Many times we need to add watermarks or text to images to protect the copyright of the image or add additional information. As a powerful server-side scripting language, PHP provides rich image processing functions. This article will introduce how to use PHP to add watermarks and text to images.

Add watermark
Adding watermark is a common requirement. It can effectively protect the copyright of pictures and increase the beauty of pictures. Below is a PHP sample code that demonstrates how to add a watermark on an image using the GD library:

<?php
// 创建画布,打开图片
$image = imagecreatefromjpeg('image.jpg');
$watermark = imagecreatefrompng('watermark.png');

// 获取图片和水印的宽高
$imageWidth = imagesx($image);
$imageHeight = imagesy($image);
$watermarkWidth = imagesx($watermark);
$watermarkHeight = imagesy($watermark);

// 计算水印的位置
$positionX = $imageWidth - $watermarkWidth - 10; // 水印距离右边10像素
$positionY = $imageHeight - $watermarkHeight - 10; // 水印距离底部10像素

// 将水印复制到图片上
imagecopy($image, $watermark, $positionX, $positionY, 0, 0, $watermarkWidth, $watermarkHeight);

// 输出图片
header('Content-Type: image/jpeg');
imagejpeg($image);

// 释放内存
imagedestroy($image);
imagedestroy($watermark);
?>
Copy after login

In this example, we first create a canvas and open the image to be watermarked and the watermark image. Then get the width and height of the image and watermark through the imagesx() and imagesy() functions, which will be used to calculate the position of the watermark. Then use the imagecopy() function to copy the watermark to the image, and use the imagejpeg() function to output the final image. Finally, use the imagedestroy() function to release the memory.

Add text
In addition to watermarks, adding text to pictures is also a common requirement. PHP provides the imagettftext() function to easily add custom text to images. Here is a PHP sample code that demonstrates how to add text to an image:

<?php
// 创建画布,打开图片
$image = imagecreatefromjpeg('image.jpg');

// 设置文字颜色
$textColor = imagecolorallocate($image, 255, 255, 255);

// 设置字体
$font = 'arial.ttf';

// 添加文字
$text = 'Hello, World!';
imagettftext($image, 30, 0, 10, 50, $textColor, $font, $text);

// 输出图片
header('Content-Type: image/jpeg');
imagejpeg($image);

// 释放内存
imagedestroy($image);
?>
Copy after login

In this example, we first create a canvas and open the image to which we want to add text. Then use the imagecolorallocate() function to set the text color. Here we use pure white. Then set the font. The arial.ttf font file is used here. You can choose other fonts according to your needs. Finally, the text is added to the image through the imagettftext() function, and the final image is output through the imagejpeg() function. Similarly, we use the imagedestroy() function to release memory.

Conclusion
By using PHP's GD library, we can easily add watermarks and text to images. Whether it's to protect the copyright of an image or add additional information, these tips can help us work better with images. Hope this article can be helpful to you.

The above is the detailed content of PHP image processing skills: how to add watermarks and text. 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