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.
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
How can i apply a watermark onto an image with rotation and opacity using imagick in php
this is my code
<?php
$image = new Imagick();
$image->readImage(realpath('C:\xampp\htdocs\imagick\3.jpg'));
$watermark = new Imagick();
$watermark->readImage(realpath('C:\xampp\htdocs\imagick\1.png'));
$image->compositeImage($watermark, imagick::COMPOSITE_OVER, 0, 0);
header("Content-Type: image/" . $image->getImageFormat());
echo $image;
?>
Get your watermark image and the image you want to add it to .
Them merge them using PHP ImageCopy function
<?php
// Load the stamp and the photo to apply the watermark to
$watermark= 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($watermark);
$sy = imagesy($watermark);
// Copy the stamp image onto our photo using the margin offsets and the photo
// width to calculate positioning of the stamp.
imagecopy($im, $watermark, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($watermark), imagesy($watermark));
// Output and free memory
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>
Should do the trick..
In your example you are only missing the rotate image.
$imagick->rotateImage(new ImagickPixel('transparent'), -13.55);
Imagick.rotateImage
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");
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);
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);