How to use php to extend the GD image processing library to create image watermarks

王林
Release: 2023-07-29 13:40:02
Original
747 people have browsed it

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"
Copy after login

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
Copy after login

After the installation is complete, you need to restart the Apache service for the extension to take effect:

sudo service apache2 restart
Copy after login

Step 2: Create an image watermark
Use in PHP The steps for GD library to create an image watermark are as follows:

  1. Create a canvas and load the original image onto the canvas:
$sourceImagePath = 'source.jpg'; $sourceImage = imagecreatefromjpeg($sourceImagePath);
Copy after login
  1. Create a watermark image or text:
$watermarkImagePath = 'watermark.png'; $watermarkImage = imagecreatefrompng($watermarkImagePath);
Copy after login

or

$watermarkText = 'Copyright'; $watermarkFont = 'arial.ttf'; $watermarkSize = 20; $watermarkColor = imagecolorallocate($sourceImage, 255, 255, 255);
Copy after login
  1. Superimpose the watermark image or text onto the original image:
imagecopy($sourceImage, $watermarkImage, $x, $y, $x_offset, $y_offset, $watermark_width, $watermark_height);
Copy after login

or

imagettftext($sourceImage, $watermarkSize, 0, $x, $y, $watermarkColor, $watermarkFont, $watermarkText);
Copy after login
  1. Output or save new pictures:
$outputImagePath = 'output.jpg'; imagejpeg($sourceImage, $outputImagePath, 100);
Copy after login

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);
Copy after login

The function of this code is to convert thesource.jpgpicture 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!

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
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!