node-canvas ico support?

162 views
Skip to first unread message

nop

unread,
Sep 26, 2012, 11:44:55 AM9/26/12
to nodejs
Hi. I'd like to use node-canvas to manipulate ico-format images. I
couldn't find any mention of supported formats in the readme, but from
my tests it seems either ico are not among them or there's an issue
with my environment. Would someone knowledgeable in the matter please
let me know if ico support should be there? If it isn't, it'd also be
nice to know if it might find its way into node-canvas sometime soon.
Thanks!

Ryan Schmidt

unread,
Sep 26, 2012, 12:39:32 PM9/26/12
to nod...@googlegroups.com
I haven't ever heard of canvas supporting loading ico format images, so it's not surprising that node-canvas doesn't support it either.

So your goal should be to look for or write an ico image reader for the canvas. Then it should work with node-canvas as well.



nop

unread,
Sep 26, 2012, 1:40:58 PM9/26/12
to nodejs
Hi Ryan. Thanks for the tip. I'd been playing with canvas in Firefox
and Chrome to manipulate ico images so it hadn't crossed my mind that
the format wasn't universally supported. I'm sure you're right,
though; a quick test reveals Internet Explorer doesn't like the
format, either, and I can't find mention of the formats that should be
supported in the W3C spec. It may be above my skill level but I'll
take a look at an ico reader, as you suggested.

On Sep 26, 12:39 pm, Ryan Schmidt <google-2...@ryandesign.com> wrote:

nop

unread,
Sep 26, 2012, 5:55:28 PM9/26/12
to nodejs
My bad- IE's canvas handles ico perfectly well. I am quick to judge IE
and careless when it comes to configuring my own server's MIME
types. ;)

For the record, Safari's canvas (desktop and iOS) supports ico, too.
toDataURL() is entirely broken on Android 2.2 (the only Android
version I can test at the moment), but I suspect later versions are in
line with Chrome/WebKit. Opera's canvas works with ico on the versions/
platforms I tested. So if it's not part of the spec, whatever that is,
ico seems to be a de facto standard.

vicapow

unread,
Oct 1, 2012, 12:10:40 AM10/1/12
to nod...@googlegroups.com

So, i ironically,  I recently needed a way to do this myself. node-canvas doesn't support it but there's this package called jParser which has a great example of reading in an ico file: https://github.com/vjeux/jParser/blob/master/sample/ico/ico.node.js

from there, i put together a little script that does basically what you want to do. you can find it here. 


I just found out this myself but ico files can actually contain more then one image icon. I haven't figured out how to deal with yet using the example from jParser.

nop

unread,
Oct 1, 2012, 12:34:59 PM10/1/12
to nod...@googlegroups.com
Hi Victor. Over the weekend I began poking around with node-imagemagick. I still prefer the idea of using node-canvas to manipulate bitmaps, so I'll take a close look at jParser and your code. Thank you very much for pointing me in this direction and providing your example.

One question I had was if you intended to increment "this.current.header.imageCount" in your "images" structure? I'm a fan of explicit statement delimiters and not familiar with underscore so I'm probably confused, but it seemed an operator might be AWOL there.

vicapow

unread,
Oct 2, 2012, 1:05:56 AM10/2/12
to nod...@googlegroups.com
sorry, that was a typo in the code. imageCount should be properly set. If you want to get the number of images, use ico.images.length.

to make it even easier, i whipped together a module, since this might be useful to others down the road: https://github.com/vicapow/fav

and here's the example of converting an ico to a png: https://github.com/vicapow/fav/blob/master/examples/to-png-stream.js

- Victor
Reply all
Reply to author
Forward
0 new messages