0

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?

mcan
  • 1,914
  • 3
  • 32
  • 53

3 Answers3

3

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);
Yatharth Agarwal
  • 4,385
  • 2
  • 24
  • 53
Baba
  • 94,024
  • 28
  • 166
  • 217
2

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 :(

Khôi
  • 2,133
  • 11
  • 10
  • 1
    `` is clearly wrong on several levels, my solution at least offers one possibility to solve it. I don't know why my answer gets downvoted and the header one gets upvoted. The `image.php` solution needs to put the GD code in another file entirely, my answer could do everything in one PHP script. Of course it's up to OP to decide which way to solve his problem. – Khôi Sep 21 '12 at 12:35
  • 1
    Indeed you are on the right track. I wouldn't necessarily recommend `data:` URIs as the solution, but the root of the problem is certainly correct and the solution is valid. – deceze Sep 21 '12 at 12:37
  • 1
    I will upvote though I still don't like the sound of placing base64 into a img tag. It sounds like something that wouldn't be good for SEO nor for initial page size, nor for caching nor for speed. – Sammaye Sep 21 '12 at 12:39
  • I don't think src="data: ... is good.It has advantage for small images like icons, but in case of watermarked JPEG photos (I think this is OP's case) it will make HTML much much bigger and loading terrible slower. – rkosegi Sep 21 '12 at 12:42
  • 1
    I don't think either that it is good nor would I ever implement it like that. Both mine and @Baba 's solution generates the image everytime it is displayed, taking a lot of CPU power of the server. A caching/persistence mechanism is to be considered as well - but clearly not scope of this question. – Khôi Sep 21 '12 at 12:48
0

Set this as your header:

header("Content-type:image/jpeg");

instead of:

header("Content-type:image/png");
Yatharth Agarwal
  • 4,385
  • 2
  • 24
  • 53
swapnesh
  • 26,318
  • 22
  • 94
  • 126
  • 2
    This is wrong .. i wonder why you are getting up votes he is using `header('Content-type: image/png');` and `imagepng($im);` why should he change the header to `header("Content-type:image/jpeg");` ??? – Baba Sep 21 '12 at 12:33