Link to home
Start Free TrialLog in
Avatar of Sam Cohen
Sam CohenFlag for United States of America

asked on

image merge error

I have a image located on file (watermark.png) and an image located in the database which renders when i run the url: mydomain.com/show_pic.php?table=homes_images&field=home_img_id&id=11

But when trying to add a watermark to jpeg, I keep getting the following error(s):

*************************************
Warning: imagecreatefromjpeg(show_pic.php?table=homes_images&field=home_img_id&id=11) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 5

Warning: getimagesize(show_pic.php?table=homes_images&field=home_img_id&id=11) [function.getimagesize]: failed to open stream: No such file or directory in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 6

Warning: imagecopymerge(): supplied argument is not a valid Image resource in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 9

Warning: imagejpeg(): supplied argument is not a valid Image resource in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 10

Warning: imagedestroy(): supplied argument is not a valid Image resource in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 11

***************************************************

My code is below:
<?php
	$watermark = imagecreatefrompng('common/images/watermark.png');
	$watermark_width = imagesx($watermark);
	$watermark_height = imagesy($watermark);
	$image = imagecreatefromjpeg('show_pic.php?table=' . $_GET["table"] . '&field=' . $_GET["field"] . '&id=' . $_GET["id"]);  
	$size = getimagesize('show_pic.php?table=' . $_GET["table"] . '&field=' . $_GET["field"] . '&id=' . $_GET["id"]);
	$dest_x = ($size[0] - $watermark_width)/2;
	$dest_y = ($size[1] - $watermark_height)/2;
	imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
	imagejpeg($image);
	imagedestroy($image);
	imagedestroy($watermark);
?>

Open in new window

Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

2 comments on your code:
- general comment, probably not directly related to your problem: at lines 5 and 6 you are building the address for url/file directly from $_GET arguments; this is generally unsafe because some 'friendly' hacker or script-kiddie might try to easily attack your script and your page with fake values for the arguments; best practice is isually to first collect the $_GET values, check/ sanitize them, then build whichever string value you need.
- the problem in you code mainly arises at line 5 where you try to open as a jpg image the file
'show_pic.php?table=homes_images&field=home_img_id&id=11'. Line 6 has the same problem, but you could probably replace it with   $size = getimagesize($image);
So your problem is line 5: all the other messages arise because, as far as php is concerned, $image which should contain an image after line 5 does not.

So in fact you need to check if it is possible at line 5 to get your image.
I would first suspect that 'show_pic.php' is an uncomplete url, and that you should prefix it with a complete url path, eg http://mydomain.com/show_pic.php' (precise path will differ, of course. You can probably get a "portable" value for this by using
'http://' . $_SERVER['HTTP_HOST'] . '/show_pic.php' Test that value directly. Once you are sure of the right url, then you can re-test your program.

You might also consider reworking the code in show_pic.php with a function: you would then include_once the file and call the function.
Instead of returning an http image the function would need a tmpfile... which you would use directly instead of the $image you are creating.

So that we can progress:
1 - First clarify the url path question until you can directly type within your browser
  http://mydomain.com/...the path you have found.../'show_pic.php?table=homes_images&field=home_img_id&id=11'
and get an correct answer, ie the program is launched correctly (but in that case might return wrong results if table, field and id are not correct)
2 - Then replace the path in your line 5 accordingly and test
3 - Then make the sanitization of $_GET and pre-building of the url with the checked avlues and $_SERVER['HTTP_HOST']

Tell us of any trouble you find on this path...
Avatar of Sam Cohen

ASKER

OK after doing what you've said, i get the follow errors:

*************
Warning: imagecreatefromjpeg() [function.imagecreatefromjpeg]: URL file-access is disabled in the server configuration in directory in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 5

Warning: imagecreatefromjpeg(http://www.mydomain.com/show_pic.php?table=available_homes_images&field=home_img_id&id=11) [function.imagecreatefromjpeg]: failed to open stream: no suitable wrapper could be found in directory in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 5

Warning: imagecopymerge(): supplied argument is not a valid Image resource in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 9

Warning: imagejpeg(): supplied argument is not a valid Image resource in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 10

Warning: imagedestroy(): supplied argument is not a valid Image resource in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 11

**************

I've also verified that "http://www.mydomain.com/show_pic.php?table=homes_images&field=home_img_id&id=11" is a working image. :)

But I didn't change the GET as of yet
Any thoughts???
ASKER CERTIFIED SOLUTION
Avatar of Bernard Savonet
Bernard Savonet
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK.ok that clear up another error. So the errors that are left showing are:

*****************************

Warning: imagecopymerge(): supplied argument is not a valid Image resource in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 25

Warning: imagejpeg(): supplied argument is not a valid Image resource in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 26

Warning: imagedestroy(): supplied argument is not a valid Image resource in /nfs/c04/h03/mnt/11111/domains/mydomain.com/html/show_pic_sold.php on line 27

***************************************************
BTW fibo,
I really appreciate what you are doing and your depth of knowledge and how you explain things. :)
What interesting is that if i change to this to the code:

$image = imagecreatefromjpeg('common/images/sold.jpg');  
$size = getimagesize('common/images/sold.jpg');  

everything seems to work fine...


 but if i do this:
$image = imagecreatefromjpeg($file_contents);  
$size = getimagesize($file_contents);  

or this:

$image = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT'] .'show_pic.php?table=' . $_GET["table"] . '&field=' . $_GET["field"] . '&id=' . $_GET["id"]);  
$size = getimagesize($_SERVER['DOCUMENT_ROOT'] .'show_pic.php?table=' . $_GET["table"] . '&field=' . $_GET["field"] . '&id=' . $_GET["id"]);

it doesn't :(
Seems you're almost done, since the main possible obstacle has been jumped over.

I suggested in my first post that you change

$size = getimagesize($file_contents);  

to

$size = getimagesize($image);  

It should do the trick, 'cause the image is there and I believe this is what getimagesize needs.

B-)  Now that it will work, just spend one mùinute to make your code safer, easier to read and maintain by handling the $_GET issue... you can even provide default value if one value is missing or out of range...


B-) and thx for the nice comment
For some reason, it isn't working still throwing the last 3 errors, i will have at it again later today
B-( My error.. getimagesize needs to address a file, not an image...


Seems that he error is not at getimagesize but later. Could you paste your current code so that we are sure of what is happening?

It seems that one of the argument to imagecopymerge is wrong, but not easy to finf which one(s).
Could you get the vales of dest_x and size['0'] ?