watermark in php is not uniform on all images - php

I am trying to create watermark on images programmatically in PHP. Images are uploaded by users and they can be of different dimensions. What I am doing to create a watermark is this:
$stamp = imagecreatefrompng('stamp381x387.png');
$ext = substr(strrchr($_GET['src'], '.'), 1);
if ($ext == 'png') {
$im = imagecreatefrompng($_GET['src']);
} else if ($ext == 'jpg') {
$im = imagecreatefromjpeg($_GET['src']);
}
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 0;
$marge_bottom = 0;
$sx = imagesx($stamp); //width
$sy = imagesy($stamp); //height
// 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 ) / 2, (imagesy($im) - $sy) / 2, 0, 0, imagesx($stamp), imagesy($stamp));
// Output and free memory
if ($ext == 'png') {
header('Content-type: image/png');
imagejpeg($im);
imagedestroy($im);
} else if ($ext == 'jpg') {
header('Content-type: image/jpeg');
imagejpeg($im);
imagedestroy($im);
}
Now, it creates water mark on images but those are not uniform. See attached Images.
How to make it uniform on all images? any help?

imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
Here's a good example for adding watermark on images using PHP -
http://php.net/manual/en/image.examples-watermark.php

Related

Watermark fading with transparent background

Have some script that display images on page with png watermark, but when i want to fade watermark png image to 50% there is white background display (but watermark fading to 50%) and when i want to change the size of watermark image the watermark png just gone and not displayed. Please help me to solve my problemwith fading and size changing. Ертл you to all.
<?php
/* Image source */
$src = strtolower($_GET['src']);
/* If no image src redirect to start page */
if(isset($src) && !empty($src) && $src !=0 && $src !=1) {
header('content-type: image/jpeg');
$path = pathinfo($src);
$watermark = imagecreatefrompng('karantino.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
if ($path["extension"]=="png") {
$image = "no_img.jpg";
}else if ($path["extension"] == "jpg" || $path["extension"] == "jpeg"){
$image = imagecreatefromjpeg($src);
$size = getimagesize($src);
$dest_x = $size[0] - $watermark_width-10;
$dest_y = $size[1] - $watermark_height-10;
$alpha_channel = imagecolorallocatealpha($image, 255, 255, 255, 127);
imagecolortransparent($image, $alpha_channel);
imagefill($image, 0, 0, $alpha_channel);
imagecopy($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
imagesavealpha($image);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
}
} else{
header("HTTP/1.1 301 Moved Permanently");
header ("Location: /");
}
/*
to use this code, just create php file with random name (i use wt.php) and put code like this in your HTML page <img src="wt.php?src=your_jpg_file.jpg">
*/
?>
Try to change image type to png (not the watermark image). Because the default background in jpg is white.
And about the size : change the image original size not in code.
I find a new working suggestion for me:
<?php
header("Content-type:image/jpeg");
$src = $_GET["src"];
/* If no image src redirect to start page, else do watermark */
if(isset($src) && !empty($src) && $src!=0 && preg_match("/.(jpg|jpeg|png)$/", $src)){
$path = pathinfo($src);
if ($path["extension"] == "png") {
$stamp = imagecreatefrompng("../../favicon.png");
$im = imagecreatefrompng($src);
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right,
imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp),
imagesy($stamp));
imagepng($im);
imagedestroy($im);
}
else if ($path["extension"] == "jpg" || $path["extension"] == "jpeg"){
$stamp = imagecreatefrompng("../../favicon.png");
$im = imagecreatefromjpeg($src);
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right,
imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp),
imagesy($stamp));
imagepng($im);
imagedestroy($im);
}
else{
/* If unknow format */
$im = imagecreatefrompng("../important/no-photo.png");
imagepng($im);
imagedestroy($im);
}
}
else{
/* Redirect to start page if no string */
header("HTTP/1.1 301 Moved Permanently");
header ("Location: /");
}
/*
to use this code, just create php file with random name and put code like this in your HTML page <img src="wt.php?src=your_jpg_file.jpg">
*/
?>
But still have a problem. When i use code like this
<img src="wt.php?src=/img/my_img/your_jpg_file.jpg"> instead of this <img src="wt.php?src=your_jpg_file.jpg"> the script didn't display any image.
Any ideas?

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

Watermark image size scaling in php

Here is my code..
$abc1 =$obj->image_1; //comming from database
$stamp = imagecreatefrompng("../image/product/stamp.png");
$im = imagecreatefromjpeg("../image/product/$abc1");
$save_watermark_photo_address ="../image/product/$abc1";
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
imagejpeg($im, $save_watermark_photo_address, 80);
imagedestroy($im);
This code is working fine but the size of watermark image is large . I Want to make it small size and put it to left side down corner on $abc1 image .
Try this code with your own adaption.
<?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);
?>

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

Categories