2

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?

Community
  • 1
  • 1
kamikaze_pilot
  • 14,304
  • 35
  • 111
  • 171

1 Answers1

1

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
pilif
  • 12,548
  • 5
  • 34
  • 31
  • 1
    It would be nice if there would not be only PNG outlined but also JPEG and GIF as those are common. Also for which formats is the meta-data actually before the dimensions information so that it would increase the byte-range? And finally what is your experience and have you run any tests about the fractions of binary data `getimagesize` accepts? – hakre Oct 21 '12 at 14:11