How does logic work to figure out the size of the image?

8

In PHP and other programming languages, it is possible, through some special functions, to find out the size of the image.

For example:

list($width, $height) = getimagesize('images/icon.png');

I was curious to know how languages do this. Where does the image come from?

I wanted to understand how this information is read.

My question came up because when I asked this question #, I realized that in my tests, the large files took a long time to process.

But when I did the test with large and small images, to know their size, the response times were the same. So apparently the function responsible for capturing this information did not read the whole file, but returned that information from somewhere.

And what I wanted to know is where does this information come from?

How can you know, for example, the mime of an image, with such agility? I say "agility", because even if an image had 20MB, the speed of reading this information is always the same.

Where is this "information" stored? How is image size processing done?

    
asked by anonymous 22.06.2016 / 17:11

1 answer

12

Obviously the file has metadata with the relevant information. It is usually a header with the data, its format varies in each type of image according to some normally public specification so that everyone can develop their algorithms to pick up / manipulate the information they want.

In addition to the basic data that forms the image itself, it is common for the file to begin with a "signature" so that it can be easily recognized as being of that format. It is common to have a versioning on this signature.

Several existing libraries already do what you want and are available for various languages, such as PHP. The language does nothing, but rather this code that may even be part of the standard library. Whoever did it certainly studied the specification and created the necessary code. In the background is just to read a few bytes at specific positions of the file.

The algorithm probably looks at whether the data is well formed. Some formats can help with this type of verification (with CRC , for example), others are more susceptible to corruption. And confirming the comment below: yes, it's annoying to do right, that's why you see people using things ready.

In fact the technique is valid for any type of file, not just image. Whenever you need to retrieve information in complexity (1) , and it can be obtained previously, save it in some place, preferably at the beginning and in a fixed position to guarantee the O (1) "firm".

Trying to figure out the information on its own will likely complicate O (N) which is much worse, though not tragic.

In the case of PNG there is a chunk called IHDR starting with the% byte sequence of% and then the following structure with the data that refers to the question:

Width               4 bytes //tamanho aqui
Height              4 bytes //tamanho aqui
Bit depth           1 byte
Colour type         1 byte
Compression method  1 byte
Filter method       1 byte
Interlace method    1 byte

There is a website with file formats . I do not know if it's good.

    
22.06.2016 / 17:19