🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How to Upload and Add Watermark to Image using PHP

Watermark is the best option to protect the image from being stolen or re-used by another person. You can display the ownership by adding a watermark to the image. The watermark helps to identify the creator of the image. Generally, the company logo or creator name is added to images as a watermark.

So, in this tutorial, you can see how to upload image and add watermark to an image using PHP.

index.php:

<?php
$connect = new PDO("mysql:host=localhost;dbname=testing", "root", "");
$message = '';
if(isset($_POST["upload"]))
{
if(!empty($_FILES["select_image"]["name"]))
{
$extension = pathinfo($_FILES["select_image"]["name"],PATHINFO_EXTENSION);
$allow_extension = array('jpg','png','jpeg');
$file_name = uniqid() . '.' . $extension;
$upload_location = 'upload/' . $file_name;
if(in_array($extension, $allow_extension))
{
$image_size = $_FILES["select_image"]["size"];
if($image_size < 2 * 1024 * 1024)
{
if(move_uploaded_file($_FILES["select_image"]["tmp_name"], $upload_location))
{
$watermark_image = imagecreatefrompng('logo.png');
if($extension == 'jpg' || $extension == 'jpeg')
{
$image = imagecreatefromjpeg($upload_location);
}
if($extension == 'png')
{
$image = imagecreatefrompng($upload_location);
}
$margin_right = 10;
$margin_bottom = 10;
$watermark_image_width = imagesx($watermark_image);
$watermark_image_height = imagesy($watermark_image);
imagecopy($image, $watermark_image, imagesx($image) - $watermark_image_width - $margin_right, imagesy($image) - $watermark_image_height - $margin_bottom, 0, 0, $watermark_image_width, $watermark_image_height);
imagepng($image, $upload_location);
imagedestroy($image);
if(file_exists($upload_location))
{
$message = "Image Uploaded with Watermark";
$data = array(
':image_name' => $file_name
);
$query = "
INSERT INTO images_table
(image_name, upload_datetime)
VALUES (:image_name, now())
";
$statement = $connect->prepare($query);
$statement->execute($data);
}
else
{
$message = "There is some error, try again";
}
}
else
{
$message = "There is some error, try again";
}
}
else
{
$message = "Selected Image Size is very big";
}
}
else
{
$message = 'Only .jpg, .png and .jpeg image file allowed to upload';
}
}
else
{
$message = 'Please select Image';
}
}
$query = "
SELECT * FROM images_table
ORDER BY image_id DESC
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
<title>How to Upload and Add Watermark to Image using PHP</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>
<body>
<br />
<div class="container">
<h3 align="center">How to Upload and Add Watermark to Image using PHP</h3>
<br />
<?php
if($message != '')
{
echo '
<div class="alert alert-info">
'.$message.'
</div>
';
}
?>
<div class="panel panel-primary">
<div class="panel-heading">Select Image for Watermark</div>
<div class="panel-body">
<form method="post" enctype="multipart/form-data">
<div class="row">
<div class="form-group col-md-6">
<label>Select Image:</label>
<input type="file" class="form-control" name="select_image" />
</div>
<input type="submit" name="upload" class="btn btn-success" style="margin-top:25px;" value="Upload Image">
</div>
</form>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Uploaded Image with Watermark</div>
<div class="panel-body" style="height: 200px;overflow-y: auto;">
<div class="row">
<?php
foreach($result as $row)
{
echo '
<div class="col-md-2" style="margin-bottom:16px;">
<img src="upload/'.$row["image_name"].'" class="img-responsive img-thumbnail" />
</div>
';
}
?>
</div>
</div>
</div>
</div>
</body>
</html>
view raw index.php hosted with ❤ by GitHub

Database:

--
-- Database: `watermark`
--

-- --------------------------------------------------------

--
-- Table structure for table `images_table`
--

CREATE TABLE `images_table` (
  `image_id` int(11) NOT NULL,
  `image_name` varchar(250) NOT NULL,
  `upload_datetime` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `images_table`
--

INSERT INTO `images_table` (`image_id`, `image_name`, `upload_datetime`) VALUES
(12, '5f33d2821f781.png', '2020-08-12 16:59:06');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `images_table`
--
ALTER TABLE `images_table`
  ADD PRIMARY KEY (`image_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `images_table`
--
ALTER TABLE `images_table`
  MODIFY `image_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=13;
COMMIT;

Don’t forget to create Upload directory.

Then, see the result:

After upload the image.

And also save on Database.

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.