Saturday 8 October 2011

The Starting of Journey

Well, i am starting this blog from the first entry i created at IVAN's My friend blog related to Image WaterMarking using PHP to protect your images from others.

So here is my first code

example below to make image watermarking using php:


<?php
//// By aksshay sharma - to create watermark images (runtime) using php
// Telling browser that the content is of image type
header('content-type: image/jpeg');
// Setting our watermark image (company logo)
$watermark_image = imagecreatefrompng('watermark_logo.png');
// Calculating Dimension of our watermark
$watermark_width = imagesx($watermark_image);
$watermark_height = imagesy($watermark_image);
$your_image = imagecreatetruecolor($watermark_width, $watermark_height);
//Set the image on which watermark is to be made
$your_image = imagecreatefromjpeg("my_image.jpg");
$size = getimagesize("my_image.jpg");
$final_x = $size[0] - $watermark_width - 5;
$final_y = $size[1] - $watermark_height - 5;
imagecopymerge($your_image, $watermark_image, $final_x, $final_y, 0, 0, $watermark_width, $watermark_height, 100);
// Generating image having watermark
imagejpeg($your_image);
// Destroying temporary images
imagedestroy($your_image);
imagedestroy($watermark_image);
?>

Example for watermark image with text using PHP:

<?php
//Using imagecopymerge() to create a translucent watermark
// Load the image on which watermark is to be applied
$original_image = imagecreatefromjpeg('pic.jpeg');


// First we create our watermark image manually from GD
$watermark = imagecreatetruecolor(100, 70);
$original_image = imagecreatefromjpeg('pic.jpeg');
//Set the hex color code for your watermark and dimension
imagestring($watermark, 5, 20, 20, 'Aksshay', 0xFFFFF);
imagestring($watermark, 3, 20, 40, 'Sharma', 0xFFFFF);


// Set the margins for the watermark and get the height/width of the watermark image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($watermark);
$sy = imagesy($watermark);


// Merge the watermark onto our photo with an opacity (transparency) of 50%
imagecopymerge($original_image, $watermark, imagesx($original_image) - $sx - $marge_right, imagesy($original_image) - $sy - $marge_bottom, 0, 0, imagesx($watermark), imagesy($watermark), 50);


// Save the image to file and free memory
imagepng($original_image, 'watermark_image.png');
imagedestroy($original_image);
?>


The first code will watermark an image with another image, so it’s like to stamp the other image with your digital sign. And the second example, it will stamp your image with your text. Basically the text will converted to image then stamp to the original image.

To use the code don’t forget to enable php gd library in your php.ini:

extension=php_gd2.dll

Note: Your watermark image is of 8-bit PNG. To make your image 8-bit PNG you can use imagemagik.

Copy the complete code from here -

Complete Code

No comments:

Post a Comment