How to fix watermark position php? - 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");

Related

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

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

Calculate image size in percentage in php

I have a piece of code which adds a watermark to the bottom right corner of an uploaded photo. However, the watermark doesen't change size according to the uploaded photo as I want it to do. I'd like to scale it calculated on percentage, so the watermark is always 10% of the uploaded photo and placed in the bottom right corner. How can this be done?
This is my code:
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefromgif('../images/watermark.gif');
$marge_right = 5;
$marge_bottom = 5;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$im = imagecreatefromjpeg($file_tmp)
imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right,
imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 30);
If you have PHP 5.5+, do it like this:
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefromgif('../images/watermark.gif');
$im = imagecreatefromjpeg($file_tmp);
$marge_right = 5;
$marge_bottom = 5;
$percent = 10;
$factor = 1 - ($percent/100);
$stampscaled = imagescale ($stamp, $factor * $imagesx($im));
$sx = imagesx($stampscaled);
$sy = imagesy($stampscaled);
imagecopymerge($im, $stamp, imagesx($im) - $marge_right - $sx,
imagesy($im) - $marge_bottom - $sy, 0, 0, $factor * imagesx($im), $factor * imagesy($im), 30);
Note: This will work well with source images roughly rectangular in size. For extreme aspect ratios, you might need to use more sophisticated scaling.
For pre-PHP5.5, but at least PHP 4, you can scale an image like this:
function scale($image, $percentage)
{
$w = imagesx($image) * $percentage;
$h = imagesy($image) * $percentage;
$newimage = imagecreatetruecolor($w, $h);
imagecopyresized($newimage, $image, 0, 0, 0, 0, $w, $h,
imagesx($image), imagesy($image));
return $newimage;
}
$scaledImage = scale($originalImage, 0.5); // scale by 50%

Categories