Code solutions to PHP, JS, CSSCode source PHP, JS, CSS

Spread your code soluces to the world

Love me
Solucior » PHP GD IMAGE » PHP watermark

PHP watermark

01/13/2012 by Dux   -   5/5 (3 ratings)

This script applies a watermark on a JPG, GIF or PNG image. The watermark comes from a 24-bit PNG that preserve empty areas around the logo. I recommend Photoshop to make it.
This is the only script that changes the opacity of an image before pasting to another. For indeed, PHP5 and GD2 library does not own filter opacity.
Operation is simple. It analyzes the color of each pixel of the watermark and redraws it on a blank image. If the pixel is transparent (alpha value 127 or greater than 100), he redesigned a completely transparent pixel. It remains only to stick the tattoo on the image.

You can code positioning and resizing options to meet every desire.

Demo.

Image source (reduced to the display)
watermarking image source
PNG watermark image
watermarking image stamp
Result
watermarking image result

source code



 
<?php
/****************************************************/
/*            solucior.com by Dux                   */
/****************************************************/
// Image source
$image = "ipad.jpg";
// Watermark source
$watermark = "watermark.png";
// Watermark opacity (0 = solid to 127 = invisible)
$opacity = 80;



/****************************************************/
// Create img
list($x,$y,$type)=getimagesize($image);
if (
$type == 1) $img=imagecreatefromgif($image);
if (
$type == 2) $img=imagecreatefromjpeg($image);
if (
$type == 3) $img=imagecreatefromPNG($image);

// Create watermark
$wm = imagecreatefrompng($watermark);
list(
$xwm,$ywm)=getimagesize($watermark);

// Create the new transparent watermark
$new_wm = imagecreatetruecolor($xwm, $ywm);
imagealphablending($new_wm, false);
imagesavealpha($new_wm, true );


// Modify opacity pixel by pixel and set blank area with completely transparent pixels
for( $x=0; $x < $xwm; $x++ ) {
       for(
$y=0; $y < $ywm; $y++ ) {
               
$color_index = imagecolorat($wm, $x, $y);
               
$alpha = ($color_index & 0x7F000000) >> 24;
               
$c = ImageColorsForIndex( $wm, $color_index );
               if (
$alpha < 100)
                       
$px_color = imagecolorallocatealpha($new_wm, $c["red"], $c["green"], $c["blue"], $opacity);
               else
                       
$px_color = imagecolorallocatealpha($new_wm, 0,0,0, 127);
               
imagesetpixel ($new_wm, $x, $y, $px_color);
       }
}

// Paste new watermark on the image
imagecopyresampled( $img , $new_wm , 4 , 4 , 0 , 0 , $xwm/1.5 , $ywm/1.5 , $xwm , $ywm);

// Send as image JPEG good quality
header("Content-type: image/jpeg");
imageJPEG($img, '', 80);
?>
Rate this code     |     Copied : 77x

Comments for PHP watermark

marouane     08/19/2015 13:04
thanks realy thanks
Dux     05/08/2012 02:39
This script is sending to browser as image. You can build a loop to display an entire folder with this script as image.
rish     02/01/2012 16:33
it's realy great
Owang     01/26/2012 19:47
My friend, very good script.
But I have a doubt. but if I want to list a whole directory with watermark?

If I have a directory with images as example 10 podertia I put watermark on all and display on a single file, one below the other photo?
Add a comment Nickname
ZOOM


PHP watermark