I using codeigniter upload.php v1.0 but i want to add watermark.
if ( ! #copy($this->file_temp, $this->upload_path.$this->file_name))
{
if ( ! #move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
{
$this->set_error('upload_destination_error', 'error');
return FALSE;
}
}
I wrote the following codes after this line
//**Watermark
$stamp = imagecreatefrompng($this->upload_path.'/indir.png');
$im = imagecreatefromjpeg($this->upload_path.$this->file_name);
$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));
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
//**Watermark
but not working
Related
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
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);
?>
I have a piece of code which adds a watermark to the bottom right corner of an uploaded photo. However, the watermark doesen't change size according to the uploaded photo as I want it to do. I'd like to scale it calculated on percentage, so the watermark is always 10% of the uploaded photo and placed in the bottom right corner. How can this be done?
This is my code:
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefromgif('../images/watermark.gif');
$marge_right = 5;
$marge_bottom = 5;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
$im = imagecreatefromjpeg($file_tmp)
imagecopymerge($im, $stamp, imagesx($im) - $sx - $marge_right,
imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp), 30);
If you have PHP 5.5+, do it like this:
// Load the stamp and the photo to apply the watermark to
$stamp = imagecreatefromgif('../images/watermark.gif');
$im = imagecreatefromjpeg($file_tmp);
$marge_right = 5;
$marge_bottom = 5;
$percent = 10;
$factor = 1 - ($percent/100);
$stampscaled = imagescale ($stamp, $factor * $imagesx($im));
$sx = imagesx($stampscaled);
$sy = imagesy($stampscaled);
imagecopymerge($im, $stamp, imagesx($im) - $marge_right - $sx,
imagesy($im) - $marge_bottom - $sy, 0, 0, $factor * imagesx($im), $factor * imagesy($im), 30);
Note: This will work well with source images roughly rectangular in size. For extreme aspect ratios, you might need to use more sophisticated scaling.
For pre-PHP5.5, but at least PHP 4, you can scale an image like this:
function scale($image, $percentage)
{
$w = imagesx($image) * $percentage;
$h = imagesy($image) * $percentage;
$newimage = imagecreatetruecolor($w, $h);
imagecopyresized($newimage, $image, 0, 0, 0, 0, $w, $h,
imagesx($image), imagesy($image));
return $newimage;
}
$scaledImage = scale($originalImage, 0.5); // scale by 50%
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);