在 GD (PHP) 中找不到图像
我试图在图像上添加水印,但脚本找不到我的水印图像。我是动态执行此操作的,因此对于每个用户,水印文件夹中都有一个水印。尝试使用此代码时:
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");
什么也没有发生。我尝试打印用户名变量,效果很好。我什至尝试只打印图像,它也有效。但是当使用imagecreatefrompng时,永远找不到水印图像。查看我的日志,我看到以下错误:
警告: imagecreatefrompng(../watermarks/.png) [函数.imagecreatefrompng]: 无法打开流:没有这样的文件或 目录
我没明白。我做错了什么?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您需要使用:(
在使用此方法之前,您可以打印
dirname(__FILE__)
来检查您的目录结构)。You would need to use:
(You can print
dirname(__FILE__)
to check your directory structure before using this method).线索在警告中:
它正在寻找一个名为
.png
的文件,因此显然您的$username = $_SESSION['_user_login'];
实际上并未返回用户名,或者字符串没有正确创建。您是否尝试过在字符串中使用大括号,如下所示:
"../watermarks/{$username}.png"
?如果这不起作用,那么您的用户名有问题。The clue's in the warning:
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.$用户名似乎为空。因此,“imagecreatefrompng(../watermarks/.png)”
我建议从确保您请求的文件路径有效开始进行调试。
$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.
..
是从 PHP 的当前工作目录解析的。据推测,这是 PHP 文件当前所在的目录。通常,最好从
$_SERVER['DOCUMENT_ROOT']
解析目录,以便它们是绝对*目录,而不是依赖任何东西在打开文件之前执行chdir
。*对于给定的绝对定义。
..
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 achdir
before your file is opened.*For a given definition of absolute.
根据该错误, $username 变量为空。在脚本中执行 var_dump($_SESSION) 来仔细检查您是否正确存储了 _user_login 变量。
另外,我建议在将变量放入字符串时使用花括号。
这将使您轻松查看变量并防止 php 使用错误的变量名称。
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.
This will make it easy to see your variables and prevent php from using the wrong variable name.
尝试了一切,但由于某种原因,SESSION 变量在 imagecreatefrompng 函数中不起作用。
为了解决这个问题,我只需将用户名作为隐藏变量发布。目前看来这工作正常。
感谢大家的帮助。值得赞赏。
兆瓦
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