Htaccess and watermark - php

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
}

Related

Adding watermark stamp to image inside of WP

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

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

php add watermark on a s3 imported image

I'm trying to add a watermark on an image.
Both , the image and the watermark image are imported from the aws s3.
I tried to do this
$watermark = tempnam(sys_get_temp_dir(), 'watermark.png');
//save the file from s3 storage to the temporary file
$content=$this->s3Manager->getFile('watermark.png',$this->verb,$watermark);
// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
list($watermark_width,$watermark_height) = getimagesize($watermark);
//Get the width and height of your image - we will use this to calculate where the watermark goes
$size = getimagesize($tempFile);
//Calculate where the watermark is positioned
//In this example, it is positioned in the lower right corner, 15px away from the bottom & right edges
$dest_x = $size[0] - $watermark_width - 15;
$dest_y = $size[1] - $watermark_height - 15;
imagecopy($tempFile, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
//Finalize the image:
imagejpeg($tempFile);
return $tempFile;
It's failing on the imagecopy method -
"imagecopy() expects parameter 1 to be resource, string given in.
I checked if the images a successfully imported by the dest_y and dest_x and they seems ok.
As the error says, imagecopy wants a resource. I assume tempfile is a string. You can do this
$res = imagecreatefrompng($tempfile)
and pass this to imagecopy

image watermark doesn't work

I am trying to add a watermark to an uploaded image.
Unfortunately, I am getting this error: '
Warning: imagecreatefromjpeg(upload/##.jpg): failed to open stream: No
such file or directory in
I am using the code from php.net with some changes:
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefrompng('logo.png');
$im = imagecreatefromjpeg($image);
// 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);
?>
You could maybe make use of the textPainter class I created to print text over images:
https://github.com/alvarotrigo/PHP-Backend/tree/master/textPainter
Living demo
I feel you are passing wrong path of image $im = imagecreatefromjpeg($image); . check the value of $image and for test purpose copy both image in same directory and run the script.

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