Mark
Mark

Reputation: 5098

get remote image dimensions before accepting it as an upload? getimagesize() too slow

I've been using getimagesize() to get the dimensions of an image a user is uploading, then checking if there is enough memory to process this image. The problem is getimagesize() downloads the file to the server before reading dimensions. So its catch 22. this must be a very common problem but i havent come across a universal solution. Is there a solid way to get a remote images dimensions without downloading it?

Upvotes: 0

Views: 243

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

Send a HEAD HTTP request for the image. This will get you the response headers, but the actual body (the image) won't be sent.

Then, read those headers and look for Content-Length. This is a number that shows the size of the image in bytes. Using that you can determine if there's enough memory to process the image, in which case you can repeat the request as GET (which is what getimagesize() basically does)

Upvotes: 3

Related Questions