Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue converting ICO to PNG using PHP

Tags:

php

favicon

png

ico

Context: Favicons

I am writing a WordPress plugin to download favicons and also convert them to png:

http://plugins.trac.wordpress.org/browser/wp-favicons/trunk ( GPL2)

Icon Lib

  • I needed to have an ICO to PNG conversion Library that does not read files from disk but from strings.
  • I also need broad support for users so not a compiled php with a parameter that not most of the general WordPress users have
  • I am currently using this one: http://www.phpclasses.org/package/2369-PHP-Extract-graphics-from-ico-files-into-PNG-images.html

Problem

In 1 out of 5.000 icons (the others work ok...) a problem occurs with the XOR functionality. See line 296 here: http://plugins.trac.wordpress.org/browser/wp-favicons/trunk/plugins/filters/inc/class.ico.php (Notice: Uninitialized string offset: 64) So the string expected is too small.

Example

An example is this icon: click here to see (navigates to slatch.com)

Question

Does anybody know how to fix this? OR knows another good PHP Icon Class that lets me read .ico (all sorts of) via get_as_string instead of get_from_file which is better?

PS

I already read:

  • How to convert .ICO to .PNG? (tools)
  • Convert png file to ico with PHP
  • Favicon to PNG in PHP
like image 221
edelwater Avatar asked Mar 21 '11 02:03

edelwater


People also ask

How to output a PNG image in PHP?

(PHP 4, PHP 5, PHP 7, PHP 8) imagepng— Output a PNG image to either the browser or a file Description imagepng( GdImage$image, resource|string|null$file= null, int$quality= -1, int$filters= -1 ): bool

How do I write an ICO file in PHP?

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.

How to convert image to Base64 encoding in PHP?

The base64_encode () function is an inbuilt function in PHP which is used to convert any data to base64 encoding. In order to convert an image into base64 encoding firstly need to get the contents of file. This can be done with the help of file_get_contents () function of PHP.

Is imagepng () more restrictive in PHP5 than PHP4?

When changeing the PHP version from 4 to 5 I found out, that PHP5 handles imagepng() more restrictive than in PHP4. I'd used imagepng($image,'',90); to reduce the image quality whithout saving the image as a file.


3 Answers

The example icon you posted is monochrome (1 BPP). Its possible the library does not support such a low color depth.

like image 139
Unsigned Avatar answered Sep 22 '22 11:09

Unsigned


I've written a fully unit tested package for reading .ico files which supports 1bpp images (as well as embedded PNGs) - it's available here https://github.com/lordelph/icofileloader

The link to the problem .ico in the question is dead, but I'm happy to fix the package if anyone comes across a similar problem.

like image 28
Paul Dixon Avatar answered Sep 24 '22 11:09

Paul Dixon


@edelwater

I might be mistaken, but isn't the new generation of ICO files really PNG's with a different extension? That's how I have created them in the past. I could understand converting other formats like jpeg to png and then changing the extension.

http://en.wikipedia.org/wiki/Favicon#Standardization

http://en.wikipedia.org/wiki/ICO_(file_format)#PNG_format

This isn't meant to be a answer but a reference.

like image 22
RichardW11 Avatar answered Sep 21 '22 11:09

RichardW11