|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-01-20 19:05 UTC] guozheng dot ge at gmail dot com
Description:
------------
I am using imagick to overlay a favicon (favicon.ico) on top of another image. It seems that Imagick class cannot handle ico file content as binary string. However, if I create the Imagick object by providing a filename in the constructor, it works fine.
For example, this always gives exception on the line of readImageBlob:
<?php
try
{
$imgStr = file_get_conents(?favicon.ico?);
$im = new Imagick();
$im->readImageBlob($imgStr);
$im->setImageFormat(?png?);
header(?Content-type: image/x-icon?);
echo $im;
}
catch (Exception $ex)
{
echo ?exception happened: \n?;
print_r($ex);
}
?>
However, if I create an Imagick object directly using favicon.ico filename, it works fine:
<?php
try
{
$im = new Imagick(?favicon.ico?);
$im->setImageFormat(?png?);
header(?Content-type: image/x-icon?);
echo $im;
}
catch (Exception $ex)
{
echo ?exception happened: \n?;
print_r($ex);
}
?>
Reproduce code:
---------------
try
{
$imgStr = file_get_conents(?favicon.ico?);
$im = new Imagick();
$im->readImageBlob($imgStr);
$im->setImageFormat(?png?);
header(?Content-type: image/x-icon?);
echo $im;
}
catch (Exception $ex)
{
echo ?exception happened: \n?;
print_r($ex);
}
Expected result:
----------------
favicon.ico image will be returned
Actual result:
--------------
exception was thrown saying error at readImageBlob function
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 05 08:00:02 2025 UTC |
Try calling $im->setFormat("ico"); before readImageBlob(). Does this help?tried to setImageFormat('ico') before readImageBlob, it returns an exception saying cannot perform it on an empty Imagick object. exception happened ImagickException Object ( [message:protected] => Can not process empty Imagick object [string:private] => [code:protected] => 1 [file:protected] => /home/y/share/htdocs/testimagick.php [line:protected] => 24 [trace:private] => Array ( [0] => Array ( [file] => /home/y/share/htdocs/testimagick.php [line] => 24 [function] => setimageformat [class] => Imagick [type] => -> [args] => Array ( [0] => ico ) ) ) )Hi, Mikko Thanks a lot for the quick replies and great work on imagick ;-) I tried setFormat('ico'), now, I don't get "cannot process empty Imagick object" exception anymore. But still I cannot get the ico image output correctly. The output is an ugly looking image (maybe this is because ico has multiple images embedded inside?). Here is the code I tried: $imageStr = file_get_contents('favicon.ico'); $im = new Imagick(); $im->setFormat('ico'); $im->readImageBlob($imageStr); $im->setImageFormat('png'); header('Content-type: image/x-icon'); echo $im; Previously, when I create an Imagick object from file using constructor and then setImageFormat('png'), it worked fine and I can get a 24x24 image. Here is the code that works for comparison: $im = new Imagick('favicon.ico'); $im->setImageFormat('png'); header('Content-type: image/x-icon'); echo $im; thanks!Found a solution to this problem and does not need to write to a temp file and read from the file. The problem is indeed with the ICO format. It contains multiple icons with different sizes. So you should treat it in a similar way to a GIF animation and grab one individual icon and output it. I am not sure why reading from the file works though. So, I guess it is still better to be fixed inside Imagick library? Here is the sample code that should fix the problem: try { $im = new Imagick(); $im->setFormat(?ico?); $im->readImageBlob($content); /* content contains the ico image string */ $idx = 0; foreach($im as $frame) { $idx += 1; if($idx == 1) /* use the 1st frame or other frames, Imagick::getNumberImages() returns the num of frames */ { /* process the frame, such as resize */ $frame->setFormat(?png?); $imgStr = $frame->getImageBlob(); header(?Content-Length: ? . strlen($imgStr)); header(?Content-Type: image/png?); echo $imgStr; return; } } }catch (Exception $ex) { /* handle exception */ }