3

I have a folder that will consist of hundreds of PNG files and I want to write a script to make them all interlaced. Now, images will be added to that folder over time and processing all the images in the folder (wether their interlaced or progressive) seems kinda silly.

So I was wondering, is there any way to use PHP to detect if an image is interlaced or not that way I can choose wether to process it or not.

Thanks heaps!

leonbloy
  • 73,180
  • 20
  • 142
  • 190
SteppingHat
  • 1,199
  • 4
  • 19
  • 50

2 Answers2

11

You can also take the low-level approach - no need of loading the full image, or to use extra tools or libraries. If we look at the spec, we see that the "interlaced" flag it's just the byte 13 of the iHDR chunk, so we have to skip 8 bytes from the signature, plus 8 bytes of the iHDR Chunk identifier+length, plus 12 bytes of the chunk... That gives 28 bytes to be skipped, and if the next byte is 0 then the image is not interlaced.

The implementation takes just 4 lines of code:

function isInterlaced( $filename ) {
   $handle = fopen($filename, "r");
   $contents = fread($handle, 32);
   fclose($handle);
   return( ord($contents[28]) != 0 );
}

BTW, are you sure you want to use interlaced PNG? (see eg)

Community
  • 1
  • 1
leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • Well the reason why I have chosen to use interlaced is for this page, it is loading thousands of images initially and slowly cycle through them. There are multiple image cycles going at once. Even though images arent displayed, they are loading for when they will be. But the problem is that some images havent loaded by the time theyre called. So interlaced is better than progressive in this case. Also, storage isnt an issue. The web server has unlimited disk space and bandwidth :) – SteppingHat Jan 10 '13 at 08:40
  • It also seems that the function doesn't return anything when called. Any ideas why? – SteppingHat Jan 10 '13 at 08:42
  • As you can see in the code, it always returns TRUE or FALSE. Perhaps you are confused because you are printing the result casting it to a string, and PHP (in its infinite wisdom that we -mere mortals- will never fully comprehend) converts TRUE to string "1" and FALSE to empty string "". http://stackoverflow.com/a/9042041/277304 – leonbloy Jan 10 '13 at 12:18
  • " Even though images arent displayed, they are loading for when they will be. But the problem is that some images havent loaded by the time theyre called" I'm unsure if interlaced image will help here, considering that interlaced images will be larger (if storage is not an issue, download speed might be) and that "thousand images" in a web page are probably not going to be downloaded in thousand parallell connections. Try and see. – leonbloy Jan 10 '13 at 13:47
  • Well then. Could you suggest another way of going about this? Prehaps prioritising the load order of the images? – SteppingHat Jan 13 '13 at 08:59
  • @SteppingHat That would be another question. This is not a discussion forum. – leonbloy Jan 13 '13 at 14:49
  • This worked perfectly for me. Now I can assign the proper MIME type to an image before inserting it into our database. Thank you very much! – Derek Foulk Mar 29 '16 at 16:54
  • To anyone who isn't quite sure how to use this: `if ( isInterlaced( $filename ) ) { echo $filename . " is interlaced/progressive"; }` – Derek Foulk Mar 29 '16 at 16:56
0

I think ImageMagick could solve your problem.

http://php.net/manual/en/imagick.identifyimage.php

Don't know if all the attributes are returned, but if you look at the ImageMagick tool documentation you can find that they can spot if an image is interlaced or not.

http://www.imagemagick.org/script/identify.php

At worst you can run the command for ImageMagick via PHP if the ImageMagick extension is not installed and parse the output for the "Interlace" parameter.

napolux
  • 15,574
  • 9
  • 51
  • 70