Convert png file to ico with PHP

2020-02-05 04:34发布

问题:

I would like to create a PHP script that convert a png file to an ico file. Is it possible to do it just with PHP ? How ?

Thanks !!!

回答1:

The recently uploaded https://github.com/chrisbliss18/php-ico creates valid ICO files including multiple embedded resolutions from PNG files and other file formats using only PHP and the GD library.



回答2:

After some googling and light experimentation, it appears that an .ico file is basically a BMP with another file extension. I believe the ICO format supports more resolutions in one file, but a BMP seems to constitute a minimal ICO file. You should be able to generate BMP files easily with gd or ImageMagick.



回答3:

This seems to suggest Imagick can do this.

Otherwise, you could do the conversion on the server using an external tool and server the .ico file. Tools to achieve this have been analyzed in Stack Overflow before (well, doing the opposite transformation, but I bet most of that software can do what you need).



回答4:

There is no easy way to do this directly though php built in libraries. To write an ico file, you will need to write individual bits and bytes by hand to suit the file format. The easiest way would be to use a third party library.

phpthumb is a good tool for this.

http://phpthumb.sourceforge.net/



回答5:

I would give floIcon a try: http://tech.flobi.com/test/floIcon/



回答6:

For my WP plugin: http://wordpress.org/extend/plugins/wp-favicons/ I am using this ico lib: http://www.tom-reitz.com/2009/02/17/php-ico-to-png-conversion/comment-page-1/#comment-14883 here is the code : http://plugins.trac.wordpress.org/browser/wp-favicons/trunk/plugins/filters/convert_to_png.php

It works with almost all icons but some like an icon found here: http://www.slatch.com/ give me : Notice: Uninitialized string offset: 64 etc... on line 296.

So... if you have found a better .ico php lib than I can replace that one with another one :)



标签: php png gd icons