How to use PHP to extend the GD image processing library to create a picture watermark
Introduction:
Picture watermark is a common picture processing technology, which is protected by superimposing a layer of transparent text or pictures on the picture. Copyright of the image and can increase the beauty of the image. In PHP, you can use the GD image processing library to implement the image watermark function. This article will introduce how to use PHP to extend the GD image processing library to create image watermarks, and provide relevant code examples.
Introduction to GD Image Processing Library:
GD Image Processing Library is an open source library for creating and modifying bitmap images. It provides a series of functions and methods that can perform various operations on images, including scaling, cropping, rotating, watermarking, etc. The GD library supports a variety of image formats, including GIF, JPEG, and PNG, etc.
Step 1: Install the GD extension
First, make sure your PHP environment has the GD extension installed. You can check whether the GD extension is installed by running the following command:
php -i | grep "GD Library"
If the GD Library related information is returned, it means that the GD extension has been installed. If no information is returned, the GD extension has not been installed. You can install the GD extension through the following command:
sudo apt-get install php7.0-gd
After the installation is complete, you need to restart the Apache service for the extension to take effect:
sudo service apache2 restart
Step 2: Create an image watermark
Use in PHP The steps for GD library to create an image watermark are as follows:
$sourceImagePath = 'source.jpg'; $sourceImage = imagecreatefromjpeg($sourceImagePath);
$watermarkImagePath = 'watermark.png'; $watermarkImage = imagecreatefrompng($watermarkImagePath);
or
$watermarkText = 'Copyright'; $watermarkFont = 'arial.ttf'; $watermarkSize = 20; $watermarkColor = imagecolorallocate($sourceImage, 255, 255, 255);
imagecopy($sourceImage, $watermarkImage, $x, $y, $x_offset, $y_offset, $watermark_width, $watermark_height);
or
imagettftext($sourceImage, $watermarkSize, 0, $x, $y, $watermarkColor, $watermarkFont, $watermarkText);
$outputImagePath = 'output.jpg'; imagejpeg($sourceImage, $outputImagePath, 100);
The complete code example is as follows:
$sourceImagePath = 'source.jpg'; $sourceImage = imagecreatefromjpeg($sourceImagePath); $watermarkImagePath = 'watermark.png'; $watermarkImage = imagecreatefrompng($watermarkImagePath); $watermarkText = 'Copyright'; $watermarkFont = 'arial.ttf'; $watermarkSize = 20; $watermarkColor = imagecolorallocate($sourceImage, 255, 255, 255); $x = 10; $y = 10; $x_offset = 0; $y_offset = 0; $watermark_width = imagesx($watermarkImage); $watermark_height = imagesy($watermarkImage); imagecopy($sourceImage, $watermarkImage, $x, $y, $x_offset, $y_offset, $watermark_width, $watermark_height); // 或者使用文字水印 //imagettftext($sourceImage, $watermarkSize, 0, $x, $y, $watermarkColor, $watermarkFont, $watermarkText); $outputImagePath = 'output.jpg'; imagejpeg($sourceImage, $outputImagePath, 100);
The function of this code is to convert thesource.jpg
picture Add a watermark and output asoutput.jpg
.
Summary:
Through the introduction of this article, we have learned how to use PHP to extend the GD image processing library to create image watermarks. Using the GD library, you can flexibly control the location, size, color and other attributes of image watermarks, and support multiple image formats. I hope this article is helpful to you, and I wish you success in using image watermarks!
The above is the detailed content of How to use php to extend the GD image processing library to create image watermarks. For more information, please follow other related articles on the PHP Chinese website!