开发者

Favicon to PNG in PHP

I need a PHP script to convert favicons to PNGs while keeping their original dimensions.

I know Google has it's secret icon converter - http://www.google.com/s2/favicons?domain=http://facebook.com/ but this converts favicons to 16x16 even if they they were originally larger. So basically I need this, minus the shrinking effect.

I've also seen this - http://www.controlstyle.com/articles/programming/text/php-favicon/ but I couldn't get it to work after hours of messing around with it.

Basically I am trying to automatically grab the icon for a link that will be as large as possible - automatically 48x48 png based on a URL would be the perfect 开发者_StackOverflow社区scenario, but I don't know of any humanly possible way to do this given that no websites happen to keep a 48x48 icon in a publicly accessible spot.

Does anybody know of a script/service or have a suggestion? Thanks!


So I ended up using a class called FloIcon that could convert BMPs to ICO. I should note that it's always important to check the file type of an icon and not assume that .ico means bmp because some sites (like Facebook) were actually PNG).

@goker.cebeci Your service looks awesome! The main thing is that I needed my icons to be the maximum size when possible, so I just wrote my own script.


Here is a function to convert from bmp(ico) to png http://us3.php.net/manual/en/function.imagecreate.php#53879

  1. Download the ico to your server (file_get_contents or other methods) usually is favicon.ico at the base url, or scrape the html code for the <link rel="shortcut icon" href="ico_url_here" type="image/x-icon" /> element and extract the href
  2. use the function from the link above to convert to the png
  3. use the GD functions to open and resize

$image = imagecreatefrompng($filename);
$resized_image = imagecreatetruecolor($NewWidth, $NewHeight);
imagecopyresampled($resized_image, $image, 0, 0, 0, 0, $NewWidth, $NewHeight, $OriginalWidth, $OriginalHeight);

4 Save the file (imagepng or similar)


I used Imagemagick on my favicon to PNG convertor web service project.

convert "favicon.ico" -thumbnail 16x16 -alpha on -background none -flatten "favicon.png"

Some websites' favicons have scene and their sizes are bigger than 16x16 pixels eg: http://blogger.com/favicon.ico

http://www.google.com/s2/favicons?domain=http://facebook.com/ does not work properly. So, I developed a web service for this.

If you want to try my web service you can go this way http://geticon.org/of/http://facebook.com/ or this way http://geticon.org/of/facebook.com


Code at http://www.controlstyle.com/articles/programming/text/php-favicon/ has small error:

You need to change $entry['swBitCount'] to $entry['wBitCount']. When I have made that changing all work right


imagecopyresized - the docs contains the example as well

The above require compiled with option --with-gd

I assume you did not aware of imagick extension as well

etc: all possible image processing extensions/functions


Im using here: http://plugins.trac.wordpress.org/browser/wp-favicons/trunk/plugins/filters/convert_to_png.php a lib from here: http://www.tom-reitz.com/2009/02/09/ico-images-in-facebook-profile-boxes/

(I did not want to save the ico's to disk first)

The only problem with the lib is that it sometimes fails on the XOR e.g. on this favicon: http://www.slatch.com/

So that is something I need to fix in it but furthermore it worked great for thousands of icons.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜