watermark an image with rotation and opacity using imagick in php - 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

Related

PHP imagecopy function changing stamp size according to the main image height and width

I'm trying to add a watermark (logo) to each image upload to the website.
So, I used imagecopy PHP function to add a watermark (a png image) to the main image (a jpg image) but the problem is the logo size is changing according to the main image size (height and width), That's mean if I upload a 4000x2000 image the logo with be somthing like 100x100 and if the main image size is 1000x500 the stamp will be bigger than the real size (546x537).
Image Samples:
https://crkemlak.com/appimg/199f8486d7d77007771f2f450dffca4d.jpeg
https://crkemlak.com/appimg/d6f9fd02999eced76eac9a6995df904f.jpeg
https://crkemlak.com/img/stamp.png
I used this code to add the watermark to the image:
$im = imagecreatefromjpeg('../appimg/'.$filenamerand);
$originalWidth= imagesx($im);
$originalHeight = imagesy($im);
$stamp = imagecreatefrompng('../img/stamp.png');
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, ($originalWidth-$sx)/2, ($originalHeight-$sy)/2, 0, 0, imagesx($stamp), imagesy($stamp));
I need your help please to fix this problem, I need to make the watermark is in it's real size in any size of the main jpg image
Thanks
I used imagecopyresized so that the watermark could be scaled to look the same on any input image irregardless of its size. Possibly better ways to do it. Im not sure how good the quality is when scaling images with transparent backgrounds. Here it is on git
$watermark = imagecreatefrompng('watermark.png');
$image = imagecreatefromjpeg('main-image.jpg');
$wm_x = imagesx($watermark);
$wm_y = imagesy($watermark);
$img_x = imagesx($image);
$img_y = imagesy($image);
// calculate watermark size
$wm_scale = 19; // set size in relation to image
$wm_w = $img_x/$wm_scale;
$wm_aspect = $wm_y/$wm_x;
$wm_h = (int) ($wm_aspect * $wm_w);
// calculate margin
$margin_scale = 6; // set margin in relation to new watermark size
$margin_right = $wm_w/$margin_scale;
$margin_bottom = $wm_h/$margin_scale;
// calculate watermark destination
$dst_x = $img_x - $wm_w - $margin_right;
$dst_y = $img_y - $wm_h - $margin_bottom;
imagecopyresized ($image, $watermark, $dst_x, $dst_y, 0, 0, $wm_w, $wm_h, $wm_x, $wm_y);
// Output and free memory
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);

adding multiple pngs to image php

I am adding a line of text onto an image. Is it possible to overlay multiple lines (line2.png & line3.png) onto the same image?
$stamp = imagecreatefrompng('img/line1.png');
$im = imagecreatefromjpeg('tom.jpg');
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 40;
$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);
Yes, you can by using imagecopy.
Typical usage:
$copyimage = imagecreatefromjpeg("path/to/image/you/want/to/put/into/an/image.jpg");
//$copyimage just needs to be a GD object, so you can also use imagecreatefrompng etc.
imagecopy($image, $copyimage, $position_x,
$position_y, 0, 0, $image_width, $image_height);
http://php.net/imagecopy

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

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);

Center watermark on image

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");
}

Categories