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.