Reputation: 485
I'm trying to put a watermark on my images, but the script can't find my watermark image. I'm doing this dynamically, so for each user there is a watermark in the watermarks folder. When trying to use this code:
session_start();
$username = $_SESSION['_user_login'];
// Set the path to the image to watermark
$input_image = $targetPath.$newName;
// Read in the text watermark image
$watermark = imagecreatefrompng("../watermarks/$username.png");
Nothing happens. I tried printing the user name variable and it works fine. I even tried to just print the image and it works also. But when using imagecreatefrompng, the watermark image is never found. Looking in my log I see the following error:
Warning: imagecreatefrompng(../watermarks/.png) [function.imagecreatefrompng]: failed to open stream: No such file or directory
I don't get it. What am I doing wrong?
Thanks.
Upvotes: 0
Views: 839
Reputation: 485
Tried everything but for some reason the SESSION variable just doesn't work in the imagecreatefrompng function.
To solve this I just post the username as a hidden variable. This seems to work fine for the moment.
Thanks everyone for the help. It's appreciated.
mw
Upvotes: 0
Reputation: 731
Well the $username variable is blank according to that error. Do a var_dump($_SESSION) in your script to double check your are properly storing the _user_login variable.
Also I would suggest using curly braces when putting variables in strings.
$watermark = imagecratefrompng("../watermarks/{$username}.png");
This will make it easy to see your variables and prevent php from using the wrong variable name.
Upvotes: 0
Reputation: 88836
..
is resolved from PHP's current working directory. Presumably, this is the directory that the PHP file is currently in.
Generally, it's a better idea to resolve directories from $_SERVER['DOCUMENT_ROOT']
so that they are absolute* directories rather than relying on nothing doing a chdir
before your file is opened.
$watermark = imagecreatefrompng($_SERVER['DOCUMENT_ROOT'] . "/watermarks/$username.png");
*For a given definition of absolute.
Upvotes: 0
Reputation: 21175
$username appears to be blank. Hence, "imagecreatefrompng(../watermarks/.png)"
I'd suggest debugging starting with making sure the file path you're requesting is valid.
session_start();
$username = $_SESSION['_user_login'];
// Set the path to the image to watermark
$input_image = $targetPath.$newName;
// Read in the text watermark image
$filestring = "../watermarks/$username.png";
echo "LOOKING FOR $filestring";
$watermark = imagecreatefrompng($filestring);
Upvotes: 0
Reputation: 70879
The clue's in the warning:
imagecreatefrompng(../watermarks/.png)
It's looking for a file called .png
, so obviously your $username = $_SESSION['_user_login'];
isn't actually returning the username, or the string isn't getting created properly.
Have you tried using braces in the string like this: "../watermarks/{$username}.png"
? If that doesn't work then you've got a problem with your username.
Upvotes: 1
Reputation: 55354
You would need to use:
$watermark = imagecreatefrompng(dirname(dirname(__FILE__)) . "/watermarks/" . $username . ".png");
(You can print dirname(__FILE__)
to check your directory structure before using this method).
Upvotes: 1