简体   繁体   中英

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM