How to Create a Watermark on an Image with an Image in PHP

How to Create a Watermark on an Image with an Image in PHP
Hello everyone, back again at porkaone. On this occasion, we will learn how to make or how to watermark images with available images. Intrigued?, let's follow in full below.



How to Create a Watermark on an Image with an Image in PHP

1. Create a new folder with the name watermark_image, then prepare 1 image you want to watermark, 1 image to be the watermark and 1 index.php file

php watermark image
Folder



2. Buka file index.php lalu edit seperti gambar di bawah ini.

<?php // make object from existing file $img = imagecreatefrompng('image.png'); $watermark = imagecreatefrompng('watermark.png'); // get original size $original_width = imagesx($watermark); $original_height = imagesy($watermark); // make new size, we make 10% from the size $new_width = $original_width * 0.1; $new_height = $original_height * 0.1; // chnage watermark size $watermark_resized = imagescale($watermark, $new_width, $new_height); // add watermark to image // explanation: // 10 first is x coordinate, 10 second is y coordinate // imagesx to get new width size, imagesy to get new height size imagecopy($img, $watermark_resized, 10, 10, 0, 0, imagesx($watermark_resized), imagesy($watermark_resized)); // open the image header('Content-Type: image/png'); imagepng($img); // uncomment below if you want to save the watermarked image // imagepng($img, 'new_image.jpg', 100); // remove object from the memory imagedestroy($img); imagedestroy($watermark); imagedestroy($watermark_resized); ?>


3. After editing, save the file then open it in the browser watermark_image. If successful then the display will look like the image below. You can see that there is a sahretech image in the top left corner, you can change the position of the writing by changing the coordinates (10,10) in the script.

Cara Membuat Watermark pada Gambar dengan Gambar di PHP
Final Result





How to Use JPEG Images

To use jpeg images, the method is almost the same. You just need to replace some code. Follow the script as shown below.

<?php // make object from existing file $img = imagecreatefromjpeg('image.jpg'); $watermark = imagecreatefromjpeg('watermark.jpg'); // get original size $original_width = imagesx($watermark); $original_height = imagesy($watermark); // make new size, we make 10% from the size $new_width = $original_width * 0.1; $new_height = $original_height * 0.1; // chnage watermark size $watermark_resized = imagescale($watermark, $new_width, $new_height); // add watermark to image // explanation: // 10 first is x coordinate, 10 second is y coordinate // imagesx to get new width size, imagesy to get new height size imagecopy($img, $watermark_resized, 10, 10, 0, 0, imagesx($watermark_resized), imagesy($watermark_resized)); // open the image header('Content-Type: image/jpeg'); imagejpeg($img); // uncomment below if you want to save the watermarked image // imagepng($img, 'new_image.jpg', 100); // remove object from the memory imagedestroy($img); imagedestroy($watermark); imagedestroy($watermark_resized); ?>


Ok, that's all for our programming tutorial this time about how to make a watermark with an image in PHP. Hopefully this is useful, if you have questions, please ask directly in the comments column below. That is all and thank you.

Post a Comment

0 Comments