Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to: Watermark your images with PHP in 20 minutes

LAdding watermarks to your images is an effective way to protect them from unauthorized use and also promote your brand or website. In this tutorial, we will learn how to Watermark images with PHP in just 20 minutes.

Prerequisites
Before we get started, you need to have the following installed on your system:

PHP (version 5 or higher)
GD Library (for image manipulation)

Step 1: Create a Watermark Image
The first step is to create a watermark image that you want to overlay on your original image. You can create a transparent PNG image with your logo or text using any image editing software like Photoshop or GIMP.

Step 2: Upload the Original Image
Upload the original image that you want to watermark to your server. You can use PHP’s $_FILES superglobal to handle file uploads. Here’s an example:


if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

$extensions= array("jpeg","jpg","png");

if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}

if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}

if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}

This Code checks for file extensions and size, and then moves the uploaded file to a directory named “images”.

Step 3: Load the Original Image
Next, we need to load the original image using the GD library. Here’s an example:


$original_image = imagecreatefromjpeg("images/original.jpg");

This code loads the original image “original.jpg” from the “images” directory.

Step 4: Load the Watermark Image
Now, we need to load the watermark image that we created earlier. Here’s an example:


$watermark_image = imagecreatefrompng("images/watermark.png");

This code loads the watermark image “watermark.png” from the “images” directory.

Step 5: Get the Dimensions of the Original Image
To overlay the watermark image on the original image, we need to know the dimensions of the original image. Here’s an example:


$original_width = imagesx($original_image);
$original_height = imagesy($original_image);

This code gets the width and height of the original image.

Step 6: Get the Dimensions of the Watermark Image
Similarly, we need to know the dimensions of the watermark image. Here’s an example:


$watermark_width = imagesx($watermark_image);
$watermark_height = imagesy($watermark_image);

This code gets the width and height of the watermark image.

Step 7: Overlay the Watermark Image on the Original Image
Now, we can overlay the watermark image on the original image using the imagecopy() function. Here’s an example:


imagecopy($original_image, $watermark_image, $original_width - $watermark_width - 10, $original_height - $watermark_height - 10, 0, 0, $watermark_width, $watermark_height);

This code overlays the watermark image on the bottom right corner of the original image with a margin of 10 pixels.

Step 8: Save the Watermarked Image
Finally, we can save the watermarked image using the imagejpeg() function. Here’s an example:


imagejpeg($original_image, "images/watermarked.jpg");

This code saves the watermarked image as “watermarked.jpg” in the “images” directory.

Conclusion
That’s it! You have successfully watermarked your image with PHP. You can use this code to watermark all your images and protect them from unauthorized use.

The post How to: Watermark your images with PHP in 20 minutes first appeared on CodeSnippetsandTutorials™ - Code Tutorials, Graphic Design and More.



This post first appeared on Code Snippets And Tutorials, please read the originial post: here

Share the post

How to: Watermark your images with PHP in 20 minutes

×

Subscribe to Code Snippets And Tutorials

Get updates delivered right to your inbox!

Thank you for your subscription

×