carlo
carlo

Reputation: 700

getimagesize on dynamic image

I have a MySQL table in which I store user avatars as a blob. I fetch those images with a script called avatar.php which outputs the image. To request a specific user image, you have to call avatar.php?id=(user id). To make the dynamic image name pretty, I added an URL rewrite in my .htaccess so you can also call avatar-(userid).jpg, which will translate in avatar.php?id=(userid).

Now I want to fetch the user image and manipulate it using GD. I tried using getimagesize() and imagecreatefromjpeg() but I'm continuously getting an "failed to open stream no such file or directory" error.. Both the script in which I try to use these functions, as the avatar.php script, are in the root directory. I tried:

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

When I use the full url of my site (including http:// etc.) it actually works... But I haven't found out why.

//Edit: 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?

Could someone help me out please?

Thanks in advance

Upvotes: 0

Views: 1504

Answers (3)

deceze
deceze

Reputation: 522081

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.

Upvotes: 2

hazelnut
hazelnut

Reputation: 157

  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

Upvotes: 2

Roman
Roman

Reputation: 6428

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.

Upvotes: 1

Related Questions