在 GD (PHP) 中找不到图像

发布于 2024-10-02 12:22:46 字数 599 浏览 3 评论 0原文

我试图在图像上添加水印,但脚本找不到我的水印图像。我是动态执行此操作的,因此对于每个用户,水印文件夹中都有一个水印。尝试使用此代码时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

归属感 2024-10-09 12:22:46

您需要使用:(

$watermark = imagecreatefrompng(dirname(dirname(__FILE__)) . "/watermarks/" . $username . ".png");

在使用此方法之前,您可以打印 dirname(__FILE__) 来检查您的目录结构)。

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).

流心雨 2024-10-09 12:22:46

线索在警告中:

imagecreatefrompng(../watermarks/.png)

它正在寻找一个名为 .png 的文件,因此显然您的 $username = $_SESSION['_user_login']; 实际上并未返回用户名,或者字符串没有正确创建。

您是否尝试过在字符串中使用大括号,如下所示:"../watermarks/{$username}.png"?如果这不起作用,那么您的用户名有问题。

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.

卖梦商人 2024-10-09 12:22:46

$用户名似乎为空。因此,“imagecreatefrompng(../watermarks/.png)”

我建议从确保您请求的文件路径有效开始进行调试。

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);

$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);
小ぇ时光︴ 2024-10-09 12:22:46

.. 是从 PHP 的当前工作目录解析的。据推测,这是 PHP 文件当前所在的目录。

通常,最好从 $_SERVER['DOCUMENT_ROOT'] 解析目录,以便它们是绝对*目录,而不是依赖任何东西在打开文件之前执行chdir

$watermark = imagecreatefrompng($_SERVER['DOCUMENT_ROOT'] . "/watermarks/$username.png");

*对于给定的绝对定义。

.. 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.

高冷爸爸 2024-10-09 12:22:46

根据该错误, $username 变量为空。在脚本中执行 var_dump($_SESSION) 来仔细检查您是否正确存储了 _user_login 变量。

另外,我建议在将变量放入字符串时使用花括号。

$watermark = imagecratefrompng("../watermarks/{$username}.png"); 

这将使您轻松查看变量并防止 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.

$watermark = imagecratefrompng("../watermarks/{$username}.png"); 

This will make it easy to see your variables and prevent php from using the wrong variable name.

秋意浓 2024-10-09 12:22:46

尝试了一切,但由于某种原因,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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文