How To Read/Write Icons With PHP

I discovered this interesting PHP class today that can be used to process all kinds of .ico files in PHP.

Features

  • Read/write icon files.
  • Extract individual icons from a multi-icon file as GD icon resources.
  • Add new icons to an .ico file.
  • Supports all icon image sizes, bit depths and transparency.

You can find the source code on PHPClasses.org (you’ll need to register to download it). You can also try out some of the features at a live test page on the author’s site.

What to do with it

Good question. One obvious application for this PHP class would be a website favicon generator/manager but let’s face it – there are already dozens of those out there. I haven’t seen any procedural icon generators though – that might be an idea worth exploring. Most of the favicon “generators” simply convert an uploaded image to an icon or make text-based favicons. One could probably create a script that generates abstract icons based on fractals or other mathematical shapes. Or create a bunch of preset shapes and color/style them as the visitor wishes (similar to the AJAX loading .gif generator).  Or something, use your imagination 😛

Related posts :

8 Responses to “How To Read/Write Icons With PHP”

  1. Flobi says:

    Well, after reading your “What to do with it” section, it makes good sense why the class only got 4th place in the innovation awards.

    Unlike most of the classes on phpclasses.org, this class was designed to be a library (the real purpose of a class), not a standalone application or a copy & paste webscript. There is no way it could stand on its own, and it wasn’t designed to. I have written a simple interface (which you can see on my website) demonstrating the extraction and assembly capabilities for which this library was designed. And as you can see, people try to assemble full size photographs or pictures they find on the web that are not even standard sizes.

    There is a lot of misunderstading with the public about icon files, however, the icon format is a very versitile format (even storing those non-standard sizes). In my opinion, none of the generator/managers I’ve seen are really up to snuff (and very few of the icon libraries). My hope is that with easy access to this versitile format, someone will create something that is up to snuff.

    Thank you for analyzing my class. I agree it is interesting and it was a lot of fun to write. I plan to make one for Vista images, but so far, I’ve not got Vista…and haven’t been perticularly impressed with what I’ve seen of it. I also plan to make a DLL assembler & EXE/DLL/etc extracter for icons in that format too, but so far I haven’t had the time.

  2. White Shadow says:

    Thank you for your comments.

    I also thought about extracting icons from EXE files when trying to imagine some uses for this library – this might be useful for software directories and similar sites.

    While the .ICO format may be versatile I’d say it’s use is going to remain confined to a very small number of niches. It’s even possible that eventually most applications would use some other – more general purpose – image format in place of standard icons (PNG comes to mind).

  3. Flobi says:

    I totally agree. The .ico format, though versatile, has a very specific niche in icons because it can contain different images suited to different environments, that is, different sizes and bit depths. As long as there is a customizable GUI (e.g. Windows, GNOME, etc), there will be a call for an icon type image format. This is useful if you want to display the same idea in multiple locations with different specs. E.g. you want the desktop icon to be 48×48, but you need a quicklaunch icon at 16×16. Often times, resizing an image from 48×48 to 16×16 looses too much detail to be useful.

    Take into account this icon: http://www.flobi.com/test/floIcon/show_all.php?sid=d65f1f5ca84e7a8d478b1cc4805ee94e_3 . Resizing the donkey to 16×16 makes the image practically indistinguishable (bad icon file). On the other hand, this icon: http://www.flobi.com/test/floIcon/show_all.php?sid=898b0f4ffbd8fb442d3597b48495b192 . Resizing that design also gets hard to view at 16×16, but an alternative has been provided representing the same (or similar) idea for the smaller lists. You can’t do this with any other popular image format (as far as I’m aware).

    This is not perticularly useful in a web environment as far as I’ve seen, save perhaps favicon files, because in web design, usually there is a very specific environment. Images are sized specifically for the space to which they are assigned.

    It is interesting that you mention PNG’s because the new Vista .ico file format utilizes PNGs internally. That is, the images saved inside the icon file are stored as PNGs (whereas the old format stores the image as a modified uncompressed BMP). This allows the icon file to utilize PNG’s compression capabilities. That is important because icons are getting bigger. Though even the old format technically supports icons 256×256 and bigger, without the compression, the file size can get pretty big.

    Oddly enough, using PNG’s effectively eliminates one feature that I think is unique to the ico format and that is what I’ve dubbed “Inversion Method” (I’m not sure what the real term for this is because I’ve never seen it talked about before) you can see what this is in section III.g. on http://www.flobi.com/test/floIcon/more_on_icons.php . But no one uses this feature anyways.

  4. Flobi says:

    Hey, I thought of a cool use for this. For link sites, they could get the favicon from the other site and use this class to pull the image as a gd and save it to a png or something for use on the link list.

  5. White Shadow says:

    Yes, that’s a good idea. Evidently there are uses for the library, some just might not be obvious.

  6. Flobi says:

    I added a field to the example on my website where you can enter a url and it will try to determine the favicon and import it. I always like seeing what image people have in their icons.

  7. Garcia says:

    Just to let you know, not many people have enough time to make favicons for their sites. Personally, I don’t use favicons.

  8. Alison Roe says:

    Very nicely explained. I usually do it like this: http://www.coreldraw.com/en/pages/ico-file/ and edit them, but for PHP I expected it to be more complicated, but it’s actually pretty simple. Nice work!

Leave a Reply