tags:

views:

277

answers:

3

Say I have the URL of an image, and URL file-access is disabled in the server configuration, and that's not something I can (or want) to change, is there an easy way to get it's height/width using PHP? I'd prefer not to use javascript if possible.

A: 

umm, so you're going to pull the image over the network into your PHP application "find" the size, and then presumably emit an HTML img take with the size? seems kind of funky to me.

mlathe
Yes. Basically the idea is to create avatars. I suppose I could just let people manually enter in the size and then resize them accordingly, or hope that people will only pick avatars that are within the size limits I want to keep them at... but I'd rather be able to resize the images to be (say 50x50) while still allowing people to select non square images (so might have to be 40x50)
aslum
Have you considered simply using the Gravatar service? (www.gravatar.com) Stack overflow uses it. rather than going through all the trouble of building the system yourself. The added benefit is that the users who already have gravatars will automatically be displayed. Those who don't can have auto-generated identicons (the patterns).
Soviut
http://en.gravatar.com/site/implement/php and just for posterity, here's how simple it is to implement gravatars in PHP.
Soviut
A: 

In order to examine a remote image, you're going to have to request it somehow. You can't determine properties of something you can't see. If remote file access is disabled for your PHP installation, you might have to farm this out to a system level process, such as wget, cURL, or ftp.

Odds are if your host has disabled remote file access, they've probably disabled shell access too, but as an example, you could do something like:

exec("wget ".$url);

Another option might be to use the built-in cURL package, if by some odd configuration it's installed and active despite not having remote file access.

If you can figure out a way to download the image from the remote server, you can use getimagesize() on it.

zombat
A: 

This doesn't really make much sense since if you can view the image on a web page, the image should be directly accessible via the URL. Why, in other words, would you have a url to an image that can't be accessed via url?

Regardless, you can use any number of http/url libraries to set a user-agent in the header of the request. Essentially, you trick the server into believing that a browser is accessing the file, rather than an anonymous service.

Here is a list of browser user agents you can use for this purpose.

Soviut