Adding watermark stamp to image inside of WP - php

I have an AJAX call with wordpress where I need to add watermark to image and send it in the email. I'm following this instructions (and many others here on stackoveflow) to reach desired result, but to no avail, all I'm getting is small black image.
Here is my code
$im = imagecreatefromjpeg("https://s3-eu-west-1.amazonaws.com/pitchprint.io/previews/" . $project_id . "_1.jpg");
$stamp = imagecreatefrompng(get_site_url() . "/wp-content/themes/porto-child/img/watermark.png");
$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));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
images paths are correct, and they load fine in the borwser, but for some reason I cannot get this code to work, any help is much appreciated.

Code runs as well, I've tested it in local machine.
Check your code with static image URL, Make sure it works with static image.
Put your image URLs in tag to make sure your images are work well
I think it has problem with your images.
Edit: Your image in cloud is not JPEG, It's a PNG image format,It just has jpg extension.
So, Just change this code
$im = imagecreatefromjpeg("https://s3-eu-west-1.amazonaws.com/pitchprint.io/previews/" . $project_id . "_1.jpg");
To
$im = imagecreatefrompng("https://s3-eu-west-1.amazonaws.com/pitchprint.io/previews/" . $project_id . "_1.jpg");

$string = "My watermark text";
$color = imagecolorallocate($image, 255, 255, 255);
$fontSize = 3;
$x = 0;
$y = 0;
imagestring($image, $fontSize, $x, $y, $string, $color);
if you what to past image on image use imagecopymerge

Related

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 image inside image using imagecopy function

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

PHP created watermark looks strange

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

Htaccess and watermark

I want to know if it is possible to protect images in my host that are loaded from outside by adding a watermark using .htaccess?
That is, if another site uses my image URL http://example.com/1.jpg in a img tag in their own websites.
The plan is the when a foreign request comes to my host, I add a watermark to it and send it to the user browsing the foreign site.
What you basically want to do, is start with this tutorial:
http://www.alistapart.com/articles/hotlinking/
This shows you how to redirect images that come from external sites to a PHP page. Then, you can use that PHP page to watermark your image, with something like this:
<?php
header('content-type: image/jpeg');
$watermark = imagecreatefrompng('watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($_GET['pic']);
$size = getimagesize($_GET['pic']);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
The .htaccess cannot add watermarks to your images. However, it can restrict access to your images.
However, you can add watermarks using PHP GD Library . The below code shows you how to add a watermark to your image through PHP GD.
<?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);
?>
$stamp is the content what you want to be on watermark (eg: Copyrights Reserved) and $im is your actual image which has to be protected.
you must use HTTP_REFERER to determinate if user came from which link and then compare it with your own domain. if request domain was different then use php gd lib to add watermark on your images.
you should check if HTTP_REFERER is set or not. by default if user not came from another page to your site and came directly it will be empty and not set.
if(isset($_SERVER['HTTP_REFERER'])) {
#check if its from external domin
# do something here
}

Add 'Watermark' to images with php [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I have a website where users may upload images...
I need to add my logo (watermark) to the images once they are uploaded.
How can I do so?
And it is important that the watermark is in a corner where it will be visible, for example I have seen websites which generates a watermark on the fly, and puts the mark wherever the background of the main image is "the same color" so the watermark sticks out if you know what I mean.
Anybody have a good tutorial or article about this?
Or know of any function in php which I would need to find the position of the watermark?
A good example in the PHP manual:
// 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);
use this function
the type of watermark image must be "png"
function watermark_image($target, $wtrmrk_file, $newcopy) {
$watermark = imagecreatefrompng($wtrmrk_file);
imagealphablending($watermark, false);
imagesavealpha($watermark, true);
$img = imagecreatefromjpeg($target);
$img_w = imagesx($img);
$img_h = imagesy($img);
$wtrmrk_w = imagesx($watermark);
$wtrmrk_h = imagesy($watermark);
$dst_x = ($img_w / 2) - ($wtrmrk_w / 2); // For centering the watermark on any image
$dst_y = ($img_h / 2) - ($wtrmrk_h / 2); // For centering the watermark on any image
imagecopy($img, $watermark, $dst_x, $dst_y, 0, 0, $wtrmrk_w, $wtrmrk_h);
imagejpeg($img, $newcopy, 100);
imagedestroy($img);
imagedestroy($watermark);
}
watermark_image('image_name.jpg','watermark.png', 'new_image_name.jpg');
Good Example of watermark image and positioned at the center
<?php
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stampimg.png');
$im = imagecreatefrompng('mainimage.png');
// 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
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
I found a much better solution which add a watermark dinamically through .htaccess, you can find the tutorial here:
Add watermark to images through htaccess
After uploading the custom .htaccess file, the watermark.php scrypt, and your watermark.png image, all the images in the folder and its subfolders will show the watermark, however, you will still keep the original file in the server.
Hope that helps someone the same that it helped to me.
This can be done using an image manipulation library such as GD or ImageMagick. Here is a tutorial that explains a way to do it using GD:
http://articles.sitepoint.com/article/watermark-images-php
ImageMagick works well for that. I've done it before. The whole business is a bit of a pain, though. Especially if you want fancy blending modes and the like.
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('stamp.png');
$im = imagecreatefromjpeg('photo.jpg');
$save_watermark_photo_address = 'watermark_photo.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);
// 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');
imagejpeg($im, $save_watermark_photo_address, 80);
imagedestroy($im);

Categories