In PHP, image processing functions can be implemented through multiple functions provided by the GD library. imagecreatefromgd2() is a function in the GD library to load images in the .gd2 format. In this article, we will introduce how to use the imagecreatefromgd2() function to add a watermark effect to an image. We will also show how to use PHP's GD function to perform image manipulation and create a simple watermark effect.
Before you begin, make sure your PHP environment has GD library enabled. If you are using Linux, you can install it through the following command:
sudo apt-get install php-gd
After the installation is complete, just restart your PHP server.
First, we need to use the imagecreatefromgd2() function to load the original .gd2 image file. This function returns an image resource, which can be operated on next.
<?php
// Load the original image
$original_image = imagecreatefromgd2('original_image.gd2');
// Check whether the image is loading successfully
if (!$original_image) {
die("Image loading failed!");
}
?>
Next, we load an image as a watermark. This can be an image in any format, such as PNG, JPEG, etc. Suppose we have a watermark image in PNG format.
<?php
// Loading watermark image
$watermark = imagecreatefrompng('watermark.png');
// Check whether the watermark image is loading successfully
if (!$watermark) {
die("Watermark loading failed!");
}
?>
In order to prevent the watermark from being too abrupt, we can set the transparency of the watermark. You can use imagecolorallocatealpha() to create a color with transparency. Image processing in GD libraries usually uses the "transparency" value of the color to control transparency.
<?php
// Set transparency
imagealphablending($watermark, true);
imagesavealpha($watermark, true);
?>
In order to properly add the watermark to the original image, we need to calculate the location of the watermark image. Generally speaking, we can place the watermark in the lower right corner of the image, but you can adjust the position according to your needs.
<?php
// Get the size of the original image and watermark image
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// Calculate the coordinates for the placement of the watermark(Lower right corner)
$x = $original_width - $watermark_width - 10;
$y = $original_height - $watermark_height - 10;
?>
Through the imagecopy() function, we can synthesize the watermark image onto the original image. The function imagecopy() can copy the source image to the specified location of the target image.
<?php
// Synthesize watermarks to original image
imagecopy($original_image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height);
?>
Finally, we need to output the composite image. You can select an image output to any format (such as PNG or JPEG). The following is the code output in PNG format:
<?php
// Output the final image
header('Content-Type: image/png');
imagepng($original_image);
// Destroy image resources,Free memory
imagedestroy($original_image);
imagedestroy($watermark);
?>
<?php
// Load the original image
$original_image = imagecreatefromgd2('original_image.gd2');
if (!$original_image) {
die("Image loading failed!");
}
// Loading watermark image
$watermark = imagecreatefrompng('watermark.png');
if (!$watermark) {
die("Watermark loading failed!");
}
// Set transparency
imagealphablending($watermark, true);
imagesavealpha($watermark, true);
// Get the size of the original image and watermark image
$original_width = imagesx($original_image);
$original_height = imagesy($original_image);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
// Calculate the coordinates for the placement of the watermark(Lower right corner)
$x = $original_width - $watermark_width - 10;
$y = $original_height - $watermark_height - 10;
// Synthesize watermarks to original image
imagecopy($original_image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height);
// Output the final image
header('Content-Type: image/png');
imagepng($original_image);
// Destroy image resources,Free memory
imagedestroy($original_image);
imagedestroy($watermark);
?>
By using the imagecreatefromgd2() function, we can easily load images in .gd2 format and use PHP's GD library to add watermarks to the images. You can adjust the transparency, position and image output format of the watermark according to your needs. If you need to work with images in other formats, just replace the corresponding image loading function (such as imagecreatefrommpng() or imagecreatefromjpeg() ).
In this way, you can easily implement the watermarking function of your image to protect your image copyright or add personalized elements.