Center watermark on image - php

I need to add watermark on image.
I have solved using this code, work well, but image is positioned on left/bottom corner.
How to set to center watermark on image center?
$img = 'test.jpg';
// Load the image where the logo will be embeded into
$image = imagecreatefromjpeg($img);
// Load the logo image
$logoImage = imagecreatefrompng("watermark.png");
imagealphablending($logoImage, true);
// Get dimensions
$imageWidth=imagesx($image);
$imageHeight=imagesy($image);
$logoWidth=imagesx($logoImage);
$logoHeight=imagesy($logoImage);
// Paste the logo
imagecopy(
// destination
$image,
// source
$logoImage,
// destination x and y
$imageWidth-$logoWidth, $imageHeight-$logoHeight,
// source x and y
0, 0,
// width and height of the area of the source to copy
$logoWidth, $logoHeight);
// Set type of image and send the output
header("Content-type: image/png");
imagePng($image);
// Release memory
imageDestroy($image);
imageDestroy($imageLogo);

replace
// destination x and y
$imageWidth-$logoWidth, $imageHeight-$logoHeight,
with
// destination x and y
($imageWidth-$logoWidth)/2, ($imageHeight-$logoHeight)/2
,

http://gloryplus.com/index.php?route=product/product&path=81&product_id=285
for($i=0; $i < $count; $i++)
{
("'".$im[$i]."'");
$imVar = imagecreatefromjpeg($im[$i]);
// First we create our stamp image manually from GD
$stamp = imagecreatefromgif('a.gif'); //imagecreatetruecolor(100, 54);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Merge the stamp onto our photo with an opacity (transparency) of 50%
imagecopymerge($imVar, $stamp, imagesx($imVar) - $sx - $marge_right, imagesy($imVar) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 50);
// Save the image to file and free memory
imagepng($imVar, "./image/image[$i].png");
}

Related

How to fix watermark position php?

I want the watermark to be applied to a selected image, in 5 different positions, I have 3 function for these positions center, bottom_right, top_left which work with all images but i dont know how to do bottom_left, top_right. I tried but with different images the watermark disappears or appear somewhere wrong, here are the three functions that work. I need the watermark to be placed in every image the same position like responsive.
Center:
$imgs = imagecreatefromjpeg("/var/www/$f$p/$n");
// Load the logo image
$logoImage = imagecreatefrompng("$waterpath/$watername");
imagealphablending($logoImage, true);
// Get dimensions
$imageWidth = imagesx($imgs);
$imageHeight = imagesy($imgs);
$logoWidth = imagesx($logoImage);
$logoHeight = imagesy($logoImage);
// Paste the logo
imagecopy(
// destination
$imgs,
// source
$logoImage,
// destination x and y
($imageWidth - $logoWidth) / 2, ($imageHeight - $logoHeight) / 2,
// source x and y
0, 0,
// width and height of the area of the source to copy
$logoWidth, $logoHeight);
$watermark->update(array('status' => 'done'));
imagejpeg($imgs, "/var/www/$f$p/$n");
top_left:
$imgs = imagecreatefromjpeg("/var/www/$f$p/$n");
// Load the logo image
$logoImage = imagecreatefrompng("$waterpath/$watername");
imagealphablending($logoImage, true);
// Get dimensions
$imageWidth = imagesx($imgs);
$imageHeight = imagesy($imgs);
$logoWidth = imagesx($logoImage);
$logoHeight = imagesy($logoImage);
// Paste the logo
imagecopy(
// destination
$imgs,
// source
$logoImage,
// destination x and y
($imageWidth - $logoWidth) / 100, ($imageHeight - $logoHeight) / 100,
// source x and y
0, 0,
// width and height of the area of the source to copy
$logoWidth, $logoHeight);
imagejpeg($imgs, "/var/www/$f$p/$n");
bottom_right:
$imgs = imagecreatefromjpeg("/var/www/$f$p/$n");
// Load the logo image
$stamps = imagecreatefrompng("$waterpath/$watername");
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamps);
$sy = imagesy($stamps);
$imageWidth = imagesx($imgs);
$imageHeight = imagesy($imgs);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($imgs, $stamps, $imageWidth - $sx - $marge_right, $imageHeight - $sy - $marge_bottom, 0, 0, imagesx($stamps), imagesy($stamps));
imagejpeg($imgs, "/var/www/$f$p/$n");
These functions i tried but they dont work with all images:
bottom_left:
$imgs = imagecreatefromjpeg("/var/www/$f$p/$n");
// Load the logo image
$stamps = imagecreatefrompng("$waterpath/$watername");
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 670;
$marge_bottom = 10;
$sx = imagesx($stamps);
$sy = imagesy($stamps);
$imageWidth = imagesx($imgs);
$imageHeight = imagesy($imgs);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($imgs, $stamps, $imageWidth - $sx - $marge_right, $imageHeight - $sy - $marge_bottom, 0, 0, imagesx($stamps), imagesy($stamps));
imagejpeg($imgs, "/var/www/$f$p/$n");
top_right:
$imgs = imagecreatefromjpeg("/var/www/$f$p/$n");
// Load the logo image
$stamps = imagecreatefrompng("$waterpath/$watername");
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 430;
$sx = imagesx($stamps);
$sy = imagesy($stamps);
$imageWidth = imagesx($imgs);
$imageHeight = imagesy($imgs);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($imgs, $stamps, $imageWidth - $sx - $marge_right, $imageHeight - $sy - $marge_bottom, 0, 0, imagesx($stamps), imagesy($stamps));
imagejpeg($imgs, "/var/www/$f$p/$n");

Add a watermark to a external image and then store it

I need a way to add a watermark to the center of the external image ( from another domain ) and then store it in my filesystem.
This is my code:
$stamp = imagecreatefrompng('centerbutton.png');
$im = imagecreatefromjpeg( 'https://www.cleverfiles.com/howto/wp-content/uploads/2016/08/mini.jpg' );
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$imgx = imagesx($im);
$imgy = imagesy($im);
$centerX=round($imgx/2);
$centerY=round($imgy/2);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, $centerX, $centerY, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
imagejpeg($im,"img/newimage.jpg");
imagedestroy($im);
The problem is that it store nothing.
Why ?

Resize and watermark for image in PHP

I am wanting to resize and add watermark to the uploaded image.
I am able to resize the image successfully but once I apply the watermark, the watermark has a black background rather than transparent.
$watermark_l = "source/watermark_l.png";
$size_wm_l = getimagesize($watermark_l);
$watermark_l = imagecreatefrompng($watermark_l);
$filename = "input/$gallery/$file";
header('Content-Type: image/jpeg');
list($width, $height) = getimagesize($filename);
$x_large = 2000;
$y_large = 1333;
$image_p = imagecreatetruecolor($x_large, $y_large);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $x_large, $y_large, $width, $height);
imagecopy($image_p, $watermark_l, 0, 0, 0, 0, $size_wm_l[0], $size_wm_l[1]);
imagejpeg($image_p, "output/$gallery/2000x1333_$file", 100);
Try to enable alpha blending on images after ImageCreate's:
ImageAlphaBlending($watermark_l,true);
ImageAlphaBlending($image_p,true);
Here's a good example for adding watermark on images using PHP -
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Source - http://php.net/manual/en/image.examples-watermark.php

watermark an image with rotation and opacity using imagick in php

How can i apply a watermark onto an image with rotation and opacity using imagick in php
this is my code
<?php
$image = new Imagick();
$image->readImage(realpath('C:\xampp\htdocs\imagick\3.jpg'));
$watermark = new Imagick();
$watermark->readImage(realpath('C:\xampp\htdocs\imagick\1.png'));
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 0, 0);
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
?>
Get your watermark image and the image you want to add it to .
Them merge them using PHP ImageCopy function
<?php
// Load the stamp and the photo to apply the watermark to
$watermark= imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpeg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($watermark);
$sy = imagesy($watermark);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $watermark, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermark), imagesy($watermark));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Should do the trick..
In your example you are only missing the rotate image.
$imagick->rotateImage(new ImagickPixel('transparent'), -13.55);
Imagick.rotateImage

WaterMarking on gif image

So I am watermarking the png image on Gif image.here is my code :
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('add_item.png');// watermark image
$im = imagecreatefromjpeg('gif_image.gif');// source image
$image_path = "/opt/lampp/htdocs/my/Harshal/watermarking/".time().".png";
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
//header('Content-type: image/png');
imagepng($im,$image_path);
imagedestroy($im);
?>
My code is working fine and its watermarking the png image on gif image but The problem is :
There is white background coming just behind of the watermark png image and not the transparent as comes for other images.
Can you guys tell me why only gif image have issues ?
See the generated image.
I know Gif is the combinations of many images but is there any solution that we can put the image with normal behavior.?
I saw some Online watermarking tools also but they have also same issue.
You can convert the gif to a true color image. Try this:
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('add_item.png');// watermark image
$im = imagecreatefromgif('gif_image.gif');// source image
$image_path = "/opt/lampp/htdocs/my/Harshal/watermarking/".time().".png";
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
// Convert gif to a true color image
$tmp = imagecreatetruecolor(imagesx($im), imagesy($im));
$bg = imagecolorallocate($tmp, 255, 255, 255);
imagefill($tmp, 0, 0, $bg);
imagecopy($tmp, $im, 0, 0, 0, 0, imagesx($im), imagesy($im));
$im = $tmp;
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
//header('Content-type: image/png');
imagepng($im,$image_path);
imagedestroy($im);

Categories