0

我有一个 MySQL 表,我在其中将用户头像存储为 blob。我使用名为 avatar.php 的脚本获取这些图像,该脚本输出图像。要请求特定的用户图像,您必须调用 avatar.php?id=(user id)。为了使动态图像名称更漂亮,我在 .htaccess 中添加了 URL 重写,因此您也可以调用 avatar-(userid).jpg,它将转换为 avatar.php?id=(userid)。

现在我想获取用户图像并使用 GD 对其进行操作。我尝试使用 getimagesize() 和 imagecreatefromjpeg() 但我不断收到“无法打开流,没有这样的文件或目录”错误。我尝试使用这些函数的两个脚本,如 avatar.php 脚本,位于根目录中。我试过了:

getimagesize('avatar.php?id=1');
getimagesize('./avatar.php?id=1');
getimagesize('/avatar.php?id=1');
...and the same but using avatar-1.jpg instead.

当我使用我网站的完整网址(包括 http:// 等)时,它实际上可以工作......但我还没有找到原因。

//编辑:使用 file_exists() 函数时会出现类似的问题。如果我尝试 file_exists('./avatar.php') 它返回 true,如果我添加 get-vars 它返回 false。这些功能不适合动态文件吗?

有人可以帮帮我吗?

提前致谢

4

3 回答 3

2

getimagesize expects a file path. getimagesize('avatar.php?id=1') tries to open the file avatar.php?id=1 on the local hard disk, which of course doesn't exist.
If you use getimagesize('http://example.com/avatar.php?id=1'), it is understood that the path is a URL and it will go through the HTTP wrapper. Basically, getimagesize will still think it's opening a local file, but PHP will fetch that file through an HTTP request in the background. Since that HTTP request is actually returning a valid image file, that works.
Of course you do not want to engage your web server every time you're trying to open an image, as that's an unnecessary waste of resources. In fact, even if getimagesize('avatar.php?id=1') would work the way you think it does, that's still a lot of overhead for what you're trying to do.

If your images are stored in the database, figure out the image dimensions once when you put the image into the database and store them in a database column. Then simply query the size from the database.


Similar problem occurs when using the file_exists() function. If I try file_exists('./avatar.php') it returns true, if I add get-vars it returns false. Are these functions unsuitable for dynamic files?

All these functions expect file paths. avatar.php is a valid file name/path. avatar.php?foo=bar is not a valid file name/path. The latter is (part of) a URL. URLs have nothing to do with the local file system. They're only handled through a web server, which may or may not invoke a PHP or some other file on the disk. You need to distinguish the concept of file paths and URLs.

于 2012-10-22T12:36:34.257 回答
2
  1. Load the image string directly from your database into a variable ... not via getimagesize('avatar.php?id=1');
  2. Use imagecreatefromstring to load your image as an image object. Afterwards you can manipulate your image and send it to your output.

If you just want the image size without any manipulation you can use getimagesizefromstring

于 2012-10-22T12:42:23.970 回答
1

It doesn't work because getimagesize tries to open the php file, not the "image" the php file would output when parsed and executed.

This is also the reason why the full URL works: your script requests the output from your webserver, and the webserver executes the script (loading the image from DB and outputting it).

Loading it through the webserver (with full URL) might still be the simplest option.

You could also create an image resource with imagecreatefromstring (link), giving the function your results from the DB and then manipulate that.

于 2012-10-22T12:35:46.097 回答