Gustavo Piris
Gustavo Piris

Reputation: 68

Put text watermark to jpg image with php

I try to put some text to an image with php, i have tried with this code

$jjj = "drag_drop/server/php/files/QBicmbmf78c4qi2kedvhlvlnn8cjn0/UWBR3105.jpg";

                        $im = imagecreatefromjpeg($jjj);
                        $estampa = imagecreatetruecolor(210, 70);
                        imagefilledrectangle($estampa, 0, 0, 210, 69, 0x0000FF);
                        imagefilledrectangle($estampa, 9, 9, 200, 60, 0xFFFFFF);                            
                        imagestring($estampa, 5, 20, 20, 'TheTexr', 0xff0000);

                        $margen_dcho = 10;
                        $margen_inf = 10;
                        $sx = imagesx($estampa);
                        $sy = imagesy($estampa);

                        imagecopymerge($im, $estampa, imagesx($im) - $sx - $margen_dcho, imagesy($im) - $sy - $margen_inf, 0, 0, imagesx($estampa), imagesy($estampa), 40);

                        imagepng($im, "drag_drop/server/php/files/QBicmbmf78c4qi2kedvhlvlnn8cjn0/");
                        imagedestroy($im);

But in any time i have this error:

Warning: imagecreatefromjpeg(): gd-jpeg: JPEG library reports unrecoverable error

And

Warning: imagecreatefromjpeg(): 'drag_drop/server/php/files/QBicmbmf78c4qi2kedvhlvlnn8cjn0/UWBR3105.jpg' is not a valid JPEG file

and the images are not generated, any help?

Upvotes: 0

Views: 108

Answers (1)

DinoCoderSaurus
DinoCoderSaurus

Reputation: 6520

That error message is unambiguous. php doesn't think it's a jpeg. Perhaps interrogate the value of "mime" in the array returned from getimagesize()? A jpeg has the value "image/jpeg". Also, the imagepng call does not give a file name (only a directory). When I run this code, with a jpeg and a file name in imagepng, I get a good result.

I tried it with a jpeg that I had created in paint.net and (to my surprise) it failed. getimagesize["mime"] returned png. It was probably a user error that the mime and extension didn't match, but nevertheless it provoked the error.

Bottom line: if php doesn't think it's a jpeg, it will return an error.

Upvotes: 1

Related Questions