I have script that add water mark (PNG, transparent) to image(JPG). Works fine with a catch - in some way water mark changes colors and makes it NOT transparent.
This is code i use for adding water mark:
$im = imagecreatefromjpeg('../../pics/'.$ran.'_large.jpg');
$stamp = imagecreatefrompng('a.png');
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 70);
// Save the image to file and free memory
imagejpeg($im, '../../pics/'.$ran.'_large.jpg');
imagedestroy($im);
Image with watermark after PHP generates it (wrong way)
Your output image format is jpeg. Jpeg does not support transaprency. Change your output format to png.
Also suggest you to use image magic. Gd is very primitive.
Don't forget these functions when it comes to PNG images with alpha maps after creating it from PNG:
imagealphablending($stamp,false);
imagesavealpha($stamp,true);
See if it makes any difference?
thank you for help guys - I found answer in this site
$im = imagecreatefromjpeg('../../pics/'.$ran.'_large.jpg');
$stamp = imagecreatefrompng('a.png');
imagealphablending($im, true);
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$offset = 10;
imagecopy($im, $stamp, imagesx($im) - imagesx($stamp) - $offset, imagesy($im) - imagesy($stamp) - $offset, 0, 0, imagesx($stamp), imagesy($stamp));
// Save the image to file and free memory
imagejpeg($im, '../../pics/'.$ran.'_large.jpg');
imagedestroy($im);
Related
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
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);
?>
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);
I have a simple script that adds an image inside an image. Here is the code:
<?php
$im = imagecreatetruecolor(650, 400);
$stamp = imagecreatefrompng('test.png');
$red = imagecolorallocate($im, 209, 231, 244);
imagefill($im, 0, 0, $red);
$marge_right = 10;
$marge_bottom = 133;
$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));
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Now this will place the test image 10x from the right of the big image and 133px from bottom of image. How to place the image centered vertically depending on its height??
Thanks
$marge_right and $marge_bottom should correspond to the difference of horizontal and vertical sizes (respectively) between the source and destination images.
So you need to calculate the difference this way:
$marge_right = abs($dest_sx-$src_sx);
$marge_bottom = abs($dest_sy-$src_sy);
The abs is optional if you are sure the source is bigger that the source is smaller than the destination.
There is that powerful tool called 5th grade math...
Since you have your height given in
imagecreatetruecolor(650, 400);
and the height and width of your placed image in $sx and $sy this boils down to simple offset calculation to pass to $marge_right and $marge_bottom
This is my PHP water-marking function:
function img_watermark($image) {
$stamp = imagecreatefrompng('images/wm.png');
$im = imagecreatefromjpeg($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);
}
And this is my HTML code:
<img src="<?php img_watermark('images/ornek.jpeg');?>" />
But it just gives me something like this:
.......<,� u��/,R����M�����7����a4�3eY&k� �Tƃ#Ki��'�S�^� A��2_^�����L*���\�LMSOݺ7���"#���\k*)�4I����5�<�C�LP��/W�2w� qopwnnnw3e��҂g�����QB\�_ȋc��#� F$��`4;;���[T�b�-��XגsΩ(�J����"G���;�O=it�*��˗>���Nw��o�#¨�8����J����wz�V�W���>�{���#�����z�^����/?��7VWo?�������CRVS3ӷ�������?������ڝ�}��Ϳ���������O=q��~�?���IY� ?MvN�Y�����k�7[�hwg�������<��/��O���s7o�u���?����3F 8�|�~ᗟ}��v'����#g���6�/|���~ᫍ(����?p(�B����_?�sY���G>|�ŗ�V)%�\Z��� J���7/...........
I want it to show me watermarked image. How can I fix this?
You have the wrong set. Your HTML should look like:
<img src="image.php?src=images/ornek.jpeg" />
And image.php should be like this:
$stamp = imagecreatefromjpeg('images/wm.png');
$im = imagecreatefromjpeg($_GET['src']);
$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);
You can't put binary data in the src attribute of an image tag.
The src attribute expects usually an URL to an image.
You could do it with base64 encoding though:
$file = base64_encode(img_watermark('images/ornek.jpeg'));
echo "<img src='data:image/jpeg;base64,".$file."' alt='watermarked image'>";
and remove the header line in your function, unless your PHP file is supposed to send image data instead of HTML. Better not mix those things up :(
Set this as your header:
header("Content-type:image/jpeg");
instead of:
header("Content-type:image/png");