简体   繁体   中英

how many bytes do I need to read an image so that I can get its dimensions metadata in PHP

In reference to this question: Super fast getimagesize in php

I'm interested to use that accepted answer's code in obtaining the image size from its metadata by reading in just a few bytes in without downloading the entire image

What is the absolute minimum of bytes that I should download just enough so that I can get the image's metadata?

I can imagine that it will vary between images and file-types, but is there some sort of formula or guideline to determine this? Perhaps a percentage guideline relative to the image file-size?

Images can have arbitrary lenghts of metadata embedded inside them - especially with JPEG and PNG where you can even store full color profiles, preview pictures and whatnot inside the picture metadata.

Images can even have more metadata in it than actual image data.

As such, there's no way to tell an arbitrary number that will always work.

Now, if you are just interested in the image dimensions, you can usually get away with reading less than 50 bytes of the file.

  • PNG stores the width and height in bytes 16-19 (width) and 20-23 (height).
  • Other formats do similar things.

As no generic image library will probably be willing to deal with just 50-100 bytes of image data, you'll have to write your own parsers and thus check the references anyways.

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