Home >Backend Development >PHP Tutorial >PHP and GD library tutorial: How to add watermark to images
PHP and GD library tutorial: How to add watermarks to images
Introduction:
In website development, we often encounter the need to add watermarks to images. Watermarks can play a role in image copyright protection, brand promotion, etc. This article will introduce how to add watermarks to images using PHP and the GD library.
GD library introduction:
GD library is a library for processing images in PHP. It provides a set of functions to create, edit and output images. To use the GD library, you need to ensure that PHP has the GD extension library installed.
Add text watermark:
To add text watermark to an image, you first need to use the function in the GD library to create a canvas and load the original image onto the canvas.
<?php // 创建画布 $image = imagecreatetruecolor(500, 300); // 载入原始图片 $originalImage = imagecreatefromjpeg('path/to/your/image.jpg'); // 在画布上绘制原始图片 imagecopy($image, $originalImage, 0, 0, 0, 0, 500, 300); // 添加文字水印 $font = 'path/to/your/font.ttf'; $color = imagecolorallocate($image, 255, 255, 255); // 水印文字颜色 $text = 'Your Watermark Text'; imagettftext($image, 20, 0, 50, 50, $color, $font, $text); // 输出图片 header('Content-Type: image/jpeg'); imagejpeg($image); // 清理资源 imagedestroy($image); imagedestroy($originalImage); ?>
In the above code, first use the imagecreatetruecolor()
function to create a canvas with a size of 500x300. Then use the imagecreatefromjpeg()
function to load the original image onto the canvas. Next, use the imagettftext()
function to add a text watermark to the canvas. Finally, use the imagejpeg()
function to output the modified image to the browser.
Add image watermark:
In addition to text watermarks, we can also add image watermarks. The process of adding a picture watermark is similar to adding a text watermark, except that the content drawn on the canvas is changed to another picture.
<?php // 创建画布 $image = imagecreatetruecolor(500, 300); // 载入原始图片 $originalImage = imagecreatefromjpeg('path/to/your/image.jpg'); // 在画布上绘制原始图片 imagecopy($image, $originalImage, 0, 0, 0, 0, 500, 300); // 添加图片水印 $watermark = imagecreatefrompng('path/to/your/watermark.png'); imagecopy($image, $watermark, 0, 0, 0, 0, 100, 100); // 输出图片 header('Content-Type: image/jpeg'); imagejpeg($image); // 清理资源 imagedestroy($image); imagedestroy($originalImage); imagedestroy($watermark); ?>
In the above code, we first use the imagecreatetruecolor()
function to create a canvas with a size of 500x300, and then use the imagecreatefromjpeg()
function to load the original image. Next, use the imagecreatefrompng()
function to load the watermark image, and use the imagecopy()
function to draw the watermark onto the canvas. Finally, use the imagejpeg()
function to output the image to the browser.
Summary:
Through the introduction of this article, we have learned how to use PHP and GD libraries to add watermarks to images. Whether it is text watermark or image watermark, it can be realized through the functions of the GD library. This provides convenience for us to add watermarks during website development. By learning and practicing the GD library, we can better perform image processing and optimization.
The above is the detailed content of PHP and GD library tutorial: How to add watermark to images. For more information, please follow other related articles on the PHP Chinese website!