]>
git.wh0rd.org - tt-rss.git/blob - lib/floIcon.php
2 /***************************************************************************
3 * Original floIcon copyright (C) 2007 by Joshua Hatfield. *
5 * In order to use any part of this floIcon Class, you must comply with *
6 * the license in 'license.doc'. In particular, you may not remove this *
9 * Much time and thought has gone into this software and you are *
10 * benefitting. We hope that you share your changes too. What goes *
11 * around, comes around. *
12 ***************************************************************************
18 I was a little hasty on that last update. A couple new bugs from 1.1.0 have
26 Fixed a number of minor bugs. Many thanks to Dvir Berebi for pointing
33 Fixed bug when operating on low bit count images (1 or 4) with odd dimensions.
39 Made function not break quite so bad when reading a PNG file on a Vista icon.
40 Now, you shouldn't be loading Vista icons anyways, but since I'm trying to
41 upgrade to Vista compatible and I need a comparison, I've got to.
47 Okay, this one was just stupid. When adding large image support, I messed up
48 and when reading, it doubled the image size. Now, it's fixed.
49 I took the opportunity to also add a dummy AND map for 32 images on those
50 readers who might be looking for it (though it's not supposed to be used.)
55 Sorry about two versions so quickly back to back, but something needed to be
56 done with speed...things were getting too slow. I'm sure you'll be okay.
59 Told palette determination to stop at 257 colors or is 32 bit because the
60 palette is not used at that point and gets really slow when it starts
61 getting into the high numbers, for instance, in photographs or gradient
62 truecolor images with lots of unique colors.
63 After experimenting, it appears that Windows XP does in fact support 256x256
64 images and larger by setting the entry value to 0 in the 1 byte that holds
65 that value and storing the true dimentions in the image header later. Of
66 course, it still doesn't use these images in normal operation. XP will
67 resize these and use them if no other images are available.
68 Wrapped main documentation (this) to the 80th column for easier reading.
73 Thank you everyone for actively using the implementation on my site and
74 illuminating me very quickly to a number of glaring bugs in this class.
77 Added version history.
78 Fixed bug with non-standard sizes in AND map reading AND writing.
79 Fixed bug with palette images using non-black color in backgrounds.
80 Fixed bug with height/width reversal reading files.
85 Original release date.
89 If you are simply in the effort of making an ICO file, may I recommend visiting
90 my site, http://www.flobi.com/ , and clicking on floIcon. I have a fully
91 functional implementation (on which the sample.php is based) where you can also
92 see recent icons submitted by other visitors. No registration required, no
93 intrusive ads. (As of this writing, there aren't actually any ads at all, I
94 might add google ads at some point.)
96 If you are trying to get an idea of how ICO files, work, may I recommend the
97 page I used, http://www.daubnet.com/formats/ICO.html . It does not fully cover
98 icon files, but it does a very good job of what it does. Any additional
99 information, I will try to post at
100 http://www.flobi.com/test/floIcon/more_on_icons.php for your convenience.
102 If you are trying to get an idea of how image resource files work, I recommend
103 ANY other class that deals with images. This class essentially plots points on
104 the image, and that's not perticularly advanced.
106 For any purpose, I wish you luck and feel free to contact me with any bugs,
107 comments, questions, etc. - Flobi
110 This class parses ICO files. It reads directly from the ICO file, headers
111 first, so that if you are only retrieving 1 image, the entire ICO file need not
112 be parsed. It supports merging ICO files. It supports adding PHP image
113 resources as new images to the file. It has an export capability that can
114 easily be written to a new (or the same) ICO file for saving ICO files. All
115 sizes from 1x1 to 255x255 pixels and 1, 4, 8, 24 (plus transparency) and 32 bit
116 images are supported. Image retrieval by size is supported.
118 Included is a fully functional sample that allows users to upload ICO, GIF,
119 JPEG and PNG files into a session icon file as well as remove images from the
120 file and download the completed file (sample.php).
122 Known Limitations: Does not support Vista icons. Does not support inversion
123 palette method (because of the limitations of the PHP image resource).
125 Addendum on Limitations:
126 Windows Vista has added support for 256x256 size icons and now stores files as
127 PNG's. This class is for older ICO files. A new class is currently under
128 development that supports the new Windows Vista format.
130 Palette inversion (my name for this technique) is the technique of using a black
131 pixel (0, 0, 0) with a 1 "AND" pixel. White pixels with a 1 "AND" show
132 transparent (or "don't" show). Black pixels with a 1 "AND" show inverted
133 (sometimes). Because this method isn't uniformly supported or documented and
134 the PHP image resource doesn't support it, I have decided to not as well. This
135 does not apply to 32 bit images which include alpha transparency and no AND map.
137 Though other functions exist, these are the only ones I believe offer usefulness
139 floIcon public functions:
140 readICO($file, $offset = 0)
141 Loads the icon from the specified filepath ($file) starting at the
142 specified file offset ($offset). This function MERGES the loaded icon
143 images into the floIcon object.
145 formatICO($offset = 0)
146 Returns the current floIcon object formatted as an .ICO file with the
147 file offset altered by $offset. If there are too many or too large
148 images, causing any images saved past the 4,294,967,296th byte, this
149 will return false. (This is well outside PHP's default memory
152 addImage($imageResource, $desiredBitCount = 1, $pngIfWidthExceeds = 48)
153 Adds an icon image to the icon file based on the passed image resource
154 ($imageResource). It will automatically determine the bit count, but
155 can be persuaded to increase that to $desiredBitCount if that value is
156 greater than the determined bit count.
158 NOTE: The image resource is saved by REFERRENCE. So, if you edit it
159 then call getImage, the resource returned will be the same, editions and
160 all. Destruction of the resource will cause a new resource to be
161 created in getImage().
164 Returns the php image resource either assigned by addImage or created
165 dynamically at calltime by the data loaded with readICO(). The offset
166 specified here ($offset) is the array offset starting at 0 and ending
169 getBestImage($height = 32, $width = 32)
170 Returns the php images resource of the highest quality image closest to
171 the size specified. This could be useful when you only want to display
172 the icon in an icon list with a single representative icon. A resized
173 copy of the highest quality available image will be returned if there is
174 no 32 or 24 bit icon present at the speficied dimentions.
177 Sorts the $this->images array by order of size, smallest to largest.
178 This is the optimal sorting for icon files.
181 Returns a count of how many images are present in the current floIcon
184 floIcon public variables:
186 Contains a numerically indexed array of floIconImage objects.
188 True if an image has been added since load or last formatICO, otherwise
191 floIconImage public functions:
193 Returns an associative array containing the information from the ICO
197 Returns an associative array containing the information from the ICO
200 NOTE: If this icon image was created by php image resource, this may not
201 have accurate information until saving from floIcon with the formatICO()
202 function. Specifically, offset information will be inaccurate.
205 Returns a php image resource. Same as floIcon's getImage() function.
207 setImageResource($imageResource, $desiredBitCount = 1, $pngIfWidthExceeds = 48)
208 Changes this icon image based on the passed image resource
209 ($imageResource). It will automatically determine the bit count, but can
210 be persuaded to increase that to $desiredBitCount if that value is
211 greater than the determined bit count.
213 NOTE: The image resource is saved by REFERRENCE. So, if you edit it
214 then call getImageResource, the resource returned will be the same,
215 editions and all. Destruction of the resource will cause a new resource
216 to be created in getImageResource().
219 This destroys the image resource variable, freeing up memory. The image
220 will automatically be recreated when getImageResource is executed.
224 * $images is an associative array of offset integer => floIconImage object
226 var $images; // Array of floIconImage objects.
227 var $updated = false;
229 $this->images
= array();
231 function countImages() {
232 return count($this->images
);
234 function getBestImage($height = 32, $width = 32) {
236 $bestEntry = array();
238 $secondBestEntry = array();
239 foreach ($this->images
as $key => $image) {
240 $entry = $image->getEntry();
241 $header = $image->getHeader();
242 if (!@$entry["BitCount"]) {
243 $entry["BitCount"] = $header["BitCount"];
245 if ($entry["Height"] == $height && $entry["Width"] == $width && $entry["BitCount"] == 32) {
246 return $image->getImageResource();
247 } elseif ($entry["Height"] == $height && $entry["Width"] == $width && $entry["BitCount"] > min(4, @$bestEntry["BitCount"])) {
252 $entry["Height"] >= $secondBestEntry["Height"] &&
253 $entry["Width"] >= $secondBestEntry["Width"] &&
254 $secondBestEntry["BitCount"] >= $secondBestEntry["BitCount"] and
256 $entry["Height"] <= 64 && $entry["Height"] > $secondBestEntry["Height"] and
257 $entry["Height"] > 64 && $entry["Height"] < $secondBestEntry["Height"]
260 $entry["Width"] <= 64 && $entry["Width"] > $secondBestEntry["Width"] and
261 $entry["Width"] > 64 && $entry["Width"] < $secondBestEntry["Width"]
263 $secondBestEntry["BitCount"] > $secondBestEntry["BitCount"]
265 $secondBest = $image;
266 $secondBestEntry = $entry;
270 return $best->getImageResource();
271 } elseif ($secondBest) {
272 if ($secondBestEntry["Width"] != $width ||
$secondBestEntry["Height"] != $height) {
273 $imageResource = $secondBest->getImageResource();
274 $newImageResource = imagecreatetruecolor($width, $height);
275 imagesavealpha($newImageResource, true);
276 imagealphablending($newImageResource, false);
277 imagecopyresampled($newImageResource, $imageResource, 0, 0, 0, 0, $width, $height, $secondBestEntry["Width"], $secondBestEntry["Height"]);
278 $this->addImage($newImageResource, 32);
279 return $newImageResource;
281 return $secondBest->getImageResource();
286 * readICO merges the icon images from the file to the current list
288 function readICO($file, $offset = 0) {
289 if (file_exists($file) && filesize($file) > 0 && $filePointer = fopen($file, "r")) {
290 fseek($filePointer, $offset);
291 $header = unpack("SReserved/SType/SCount", fread($filePointer, 6));
292 for ($t = 0; $t < $header["Count"]; $t++
) {
293 $newImage = new floIconImage();
294 if ($newImage->readImageFromICO($filePointer, 6 +
($t * 16))) {
295 $this->images
[] = $newImage;
298 fclose($filePointer);
301 function sortImagesBySize() {
302 usort($this->images
, array("floIcon", "_cmpObj"));
304 function formatICO($offset = 0) {
305 $this->updated
= false;
307 $output .= pack("SSS", 0, 1, count($this->images
));
309 foreach ($this->images
as $image) {
310 $newImageOffset = $offset +
// Whatever offset we've been given.
312 +
(count($this->images
) * 16) // Entries.
313 +
strlen($output_images);
314 if ($newImageOffset > pow(256, 4) /* 4 bytes available for position */ ) {
317 $output .= $image->formatEntryForIco($newImageOffset); // The images already in there.
318 $output_images .= $image->formatImageForIco();
320 return $output.$output_images;
322 function _cmpObj($a, $b) {
323 $aSize = $a->getSize();
324 $bSize = $b->getSize();
325 if ($aSize == $bSize) {
328 return ($aSize > $bSize)?
1:-1;
331 function addImage($imageResource, $desiredBitCount = 1, $pngIfWidthExceeds = 48) {
332 $this->updated
= true;
333 $newImage = new floIconImage();
334 $newImage->setImageResource($imageResource, $desiredBitCount, $pngIfWidthExceeds);
335 $this->images
[] = $newImage;
337 function getImage($offset) {
338 if (isset($this->images
[$offset])) {
339 return $this->images
[$offset]->getImageResource();
345 * getSize computes the
349 $computedSize = 6; // Always 6 bytes.
350 // Add image entry headers
351 $computedSize +
= count($this->images
) * 16; // Entry headers are always 16 bytes.
352 foreach ($this->images
as $image) {
353 $computedSize +
= $image->getSize() +
$image->getHeaderSize(); // getSize does not include the header.
358 var $_imageResource = null;
360 var $_entryIconFormat = "";
362 var $_headerIconFormat = "";
363 var $_imageIconFormat = ""; // Includes palette and mask.
364 function formatEntryForIco($offset) {
365 // Format the entry, this has to be done here because we need the offset to get the full information.
366 $this->_entry
["FileOffset"] = $offset;
367 $this->_entryIconFormat
= pack("CCCCSSLL",
368 $this->_entry
["Width"]>=256?
0:$this->_entry
["Width"],
369 $this->_entry
["Height"]>=256?
0:$this->_entry
["Height"],
370 $this->_entry
["ColorCount"],
371 $this->_entry
["Reserved"],
372 $this->_entry
["Planes"],
373 $this->_entry
["BitCount"],
374 $this->_entry
["SizeInBytes"],
375 $this->_entry
["FileOffset"]
377 return $this->_entryIconFormat
;
379 function formatImageForIco() {
380 // Format the entry, this has to be done here because we need the offset to get the full information.
381 return ($this->_headerIconFormat
.$this->_imageIconFormat
);
384 // Will move $bitCount UP to $desiredBitCount if $bitCount is found to be less than it.
385 function setImageResource($imageResource, $desiredBitCount = 1, $pngIfWidthExceeds = 48) {
386 imagesavealpha($imageResource, true);
387 imagealphablending($imageResource, false);
388 $height = imagesy($imageResource);
389 $width = imagesx($imageResource);
391 // Parse resource to determine header and icon format
393 // Find Palette information
394 $is_32bit = false; // Start with an assumption and get proven wrong.
395 $hasTransparency = 0;
398 $realPalette = array();
399 $realIndexPalette = array();
400 for ($x = 0; $x < $width && !$is_32bit; $x++
) {
401 for ($y = 0; $y < $height && !$is_32bit; $y++
) {
402 $colorIndex = imagecolorat($imageResource, $x, $y);
403 $color = imagecolorsforindex($imageResource, $colorIndex);
404 if ($color["alpha"] == 0) {
405 // No point continuing if there's more than 256 colors or it's 32bit.
406 if (count($realPalette) < 257 && !$is_32bit) {
407 $inRealPalette = false;
408 foreach($realPalette as $realPaletteKey => $realPaletteColor) {
410 $color["red"] == $realPaletteColor["red"] and
411 $color["green"] == $realPaletteColor["green"] and
412 $color["blue"] == $realPaletteColor["blue"]
414 $inRealPalette = $realPaletteKey;
418 if ($inRealPalette === false) {
419 $realIndexPalette[$colorIndex] = count($realPalette);
421 $blackColor === false and
422 $color["red"] == 0 and
423 $color["green"] == 0 and
426 $blackColor = count($realPalette);
428 $realPalette[] = $color;
430 $realIndexPalette[$colorIndex] = $inRealPalette;
434 $hasTransparency = 1;
436 if ($color["alpha"] != 0 && $color["alpha"] != 127) {
445 if ($hasTransparency && $blackColor === false) {
446 // We need a black color to facilitate transparency. Unfortunately, this can
447 // increase the palette size by 1 if there's no other black color.
448 $blackColor = count($realPalette);
455 $realPalette[] = $color;
457 $colorCount = count($realPalette);
458 if ($colorCount > 256 ||
$colorCount == 0) {
460 } elseif ($colorCount > 16) {
463 } elseif ($colorCount > 2) {
470 if ($desiredBitCount > $bitCount) {
471 $bitCount = $desiredBitCount;
488 // Create $this->_imageIconFormat...
489 $this->_imageIconFormat
= "";
490 if ($bitCount < 24) {
491 $iconPalette = array();
493 foreach ($realIndexPalette as $colorIndex => $paletteIndex) {
494 $color = $realPalette[$paletteIndex];
495 $this->_imageIconFormat
.= pack("CCCC", $color["blue"], $color["green"], $color["red"], 0);
497 while (strlen($this->_imageIconFormat
) < $colorCount * 4) {
498 $this->_imageIconFormat
.= pack("CCCC", 0, 0, 0, 0);
500 // Save Each Pixel as Palette Entry
501 $byte = 0; // For $bitCount < 8 math
502 $bitPosition = 0; // For $bitCount < 8 math
503 for ($y = 0; $y < $height; $y++
) {
504 for ($x = 0; $x < $width; $x++
) {
505 $color = imagecolorat($imageResource, $x, $height-$y-1);
506 if (isset($realIndexPalette[$color])) {
507 $color = $realIndexPalette[$color];
509 $color = $blackColor;
513 $bitPosition +
= $bitCount;
514 $colorAdjusted = $color * pow(2, 8 - $bitPosition);
515 $byte +
= $colorAdjusted;
516 if ($bitPosition == 8) {
517 $this->_imageIconFormat
.= chr($byte);
522 $this->_imageIconFormat
.= chr($color);
525 // Each row ends with dumping the remaining bits and filling up to the 32bit line with 0's.
527 $this->_imageIconFormat
.= chr($byte);
531 if (strlen($this->_imageIconFormat
)%4
) $this->_imageIconFormat
.= str_repeat(chr(0), 4-(strlen($this->_imageIconFormat
)%4
));
535 for ($y = 0; $y < $height; $y++
) {
536 for ($x = 0; $x < $width; $x++
) {
537 $color = imagecolorat($imageResource, $x, $height-$y-1);
538 $color = imagecolorsforindex($imageResource, $color);
539 if ($bitCount == 24) {
540 if ($color["alpha"]) {
541 $this->_imageIconFormat
.= pack("CCC", 0, 0, 0);
543 $this->_imageIconFormat
.= pack("CCC", $color["blue"], $color["green"], $color["red"]);
546 $color["alpha"] = round((127-$color["alpha"]) / 127 * 255);
547 $this->_imageIconFormat
.= pack("CCCC", $color["blue"], $color["green"], $color["red"], $color["alpha"]);
550 if (strlen($this->_imageIconFormat
)%4
) $this->_imageIconFormat
.= str_repeat(chr(0), 4-(strlen($this->_imageIconFormat
)%4
));
553 // save AND map (transparency)
554 $byte = 0; // For $bitCount < 8 math
555 $bitPosition = 0; // For $bitCount < 8 math
556 for ($y = 0; $y < $height; $y++
) {
557 for ($x = 0; $x < $width; $x++
) {
558 if ($bitCount < 32) {
559 $color = imagecolorat($imageResource, $x, $height-$y-1);
560 $color = imagecolorsforindex($imageResource, $color);
561 $color = $color["alpha"] == 127?
1:0;
567 $colorAdjusted = $color * pow(2, 8 - $bitPosition);
568 $byte +
= $colorAdjusted;
569 if ($bitPosition == 8) {
570 $this->_imageIconFormat
.= chr($byte);
575 // Each row ends with dumping the remaining bits and filling up to the 32bit line with 0's.
577 $this->_imageIconFormat
.= chr($byte);
578 $bitPosition = 0; // For $bitCount < 8 math
581 while (strlen($this->_imageIconFormat
)%4
) {
582 $this->_imageIconFormat
.= chr(0);
585 if ($colorCount >= 256) {
588 // Create header information...
589 $this->_header
= array(
592 "Height" => $height*2,
594 "BitCount" => $bitCount,
596 "ImageSize" => strlen($this->_imageIconFormat
),
599 "ColorsUsed" => $colorCount,
600 "ColorsImportant" => 0,
602 $this->_headerIconFormat
= pack("LLLSSLLLLLL",
603 $this->_header
["Size"],
604 $this->_header
["Width"],
605 $this->_header
["Height"],
607 $this->_header
["Planes"],
608 $this->_header
["BitCount"],
610 $this->_header
["Compression"],
611 $this->_header
["ImageSize"],
612 $this->_header
["XpixelsPerM"],
613 $this->_header
["YpixelsPerM"],
614 $this->_header
["ColorsUsed"],
615 $this->_header
["ColorsImportant"]
617 $this->_entry
= array(
620 "ColorCount" => $colorCount,
623 "BitCount" => $bitCount,
624 "SizeInBytes" => $this->_header
["Size"] +
$this->_header
["ImageSize"],
627 $this->_entryIconFormat
= ""; // This won't get set until it's needed with the offset.
628 $this->_imageResource
= $imageResource;
630 // Make png if width exceeds limit for old ico style
631 if ($width > $pngIfWidthExceeds) {
632 // I wish there were a better way to get the info than this. If anyone needs a version that doesn't use OB, I can have one that creates a TMP file.
634 imagepng($imageResource);
635 $imageAsPng = ob_get_contents();
637 $this->_headerIconFormat
= "";
638 $this->_imageIconFormat
= $imageAsPng;
643 function _createImageResource() {
644 if ($newImage = @imagecreatefromstring
($this->_headerIconFormat
.$this->_imageIconFormat
)) {
645 // Vista supports PNG.
646 $this->_headerIconFormat
= "";
647 $this->_imageIconFormat
= $this->_headerIconFormat
.$this->_imageIconFormat
;
648 imagesavealpha($newImage, true);
649 imagealphablending($newImage, false);
650 $this->_imageResource
= $newImage;
651 } elseif ($this->_entry
["Height"] <= 1024 && $this->_entry
["Width"] <= 1024) {
652 $newImage = imagecreatetruecolor($this->_entry
["Width"], $this->_entry
["Height"]);
653 imagesavealpha($newImage, true);
654 imagealphablending($newImage, false);
657 if ($this->_header
["BitCount"] < 24) {
658 // Read Palette for low bitcounts
659 $colorsInPalette = $this->_header
["ColorsUsed"]?
$this->_header
["ColorsUsed"]:$this->_entry
["ColorCount"];
660 for ($t = 0; $t < pow(2, $this->_header
["BitCount"]); $t++
) {
661 $blue = ord($this->_imageIconFormat
[$readPosition++
]);
662 $green = ord($this->_imageIconFormat
[$readPosition++
]);
663 $red = ord($this->_imageIconFormat
[$readPosition++
]);
664 $readPosition++
; // Unused "Reserved" value.
665 $existingPaletteEntry = imagecolorexactalpha($newImage, $red, $green, $blue, 0);
666 if ($existingPaletteEntry >= 0) {
667 $palette[] = $existingPaletteEntry;
669 $palette[] = imagecolorallocatealpha($newImage, $red, $green, $blue, 0);
673 for ($y = 0; $y < $this->_entry
["Height"]; $y++
) {
675 for ($x = 0; $x < $this->_entry
["Width"]; $x++
) {
676 if ($this->_header
["BitCount"] < 8) {
677 $color = array_shift($colors);
678 if (is_null($color)) {
679 $byte = ord($this->_imageIconFormat
[$readPosition++
]);
681 for ($t = 7; $t >= 0; $t--) {
682 $bit_value = pow(2, $t);
683 $bit = floor($byte / $bit_value);
684 $byte = $byte - ($bit * $bit_value);
685 $tmp_color +
= $bit * pow(2, $t%
$this->_header
["BitCount"]);
686 if ($t%
$this->_header
["BitCount"] == 0) {
687 array_push($colors, $tmp_color);
691 $color = array_shift($colors);
694 $color = ord($this->_imageIconFormat
[$readPosition++
]);
696 imagesetpixel($newImage, $x, $this->_entry
["Height"]-$y-1, $palette[$color]) or die("can't set pixel");
698 // All rows end on the 32 bit
699 if ($readPosition%4
) $readPosition +
= 4-($readPosition%4
);
702 // BitCount >= 24, No Palette.
703 // marking position because some icons mark all pixels transparent when using an AND map.
704 $markPosition = $readPosition;
706 $ignoreAlpha = false;
710 for ($y = 0; $y < $this->_entry
["Height"] and !$retry; $y++
) {
711 for ($x = 0; $x < $this->_entry
["Width"] and !$retry; $x++
) {
712 $blue = ord($this->_imageIconFormat
[$readPosition++
]);
713 $green = ord($this->_imageIconFormat
[$readPosition++
]);
714 $red = ord($this->_imageIconFormat
[$readPosition++
]);
715 if ($this->_header
["BitCount"] < 32) {
717 } elseif($ignoreAlpha) {
721 $alpha = ord($this->_imageIconFormat
[$readPosition++
]);
722 $alphas[$alpha] = $alpha;
723 $alpha = 127-round($alpha/255*127);
725 $paletteEntry = imagecolorexactalpha($newImage, $red, $green, $blue, $alpha);
726 if ($paletteEntry < 0) {
727 $paletteEntry = imagecolorallocatealpha($newImage, $red, $green, $blue, $alpha);
729 imagesetpixel($newImage, $x, $this->_entry
["Height"]-$y-1, $paletteEntry) or die("can't set pixel");
731 if ($readPosition%4
) $readPosition +
= 4-($readPosition%4
);
733 if ($this->_header
["BitCount"] == 32 && isset($alphas[0]) && count($alphas) == 1) {
735 $readPosition = $markPosition;
742 if ($this->_header
["BitCount"] < 32 ||
$ignoreAlpha) {
743 // Bitcount == 32, No AND (if using alpha).
744 $palette[-1] = imagecolorallocatealpha($newImage, 0, 0, 0, 127);
745 imagecolortransparent($newImage, $palette[-1]);
746 for ($y = 0; $y < $this->_entry
["Height"]; $y++
) {
748 for ($x = 0; $x < $this->_entry
["Width"]; $x++
) {
749 $color = array_shift($colors);
750 if (is_null($color)) {
751 $byte = ord($this->_imageIconFormat
[$readPosition++
]);
753 for ($t = 7; $t >= 0; $t--) {
754 $bit_value = pow(2, $t);
755 $bit = floor($byte / $bit_value);
756 $byte = $byte - ($bit * $bit_value);
757 array_push($colors, $bit);
759 $color = array_shift($colors);
762 imagesetpixel($newImage, $x, $this->_entry
["Height"]-$y-1, $palette[-1]) or die("can't set pixel");
765 // All rows end on the 32 bit.
766 if ($readPosition%4
) $readPosition +
= 4-($readPosition%4
);
769 if ($this->_header
["BitCount"] < 24) {
770 imagetruecolortopalette($newImage, true, pow(2, $this->_header
["BitCount"]));
773 $this->_imageResource
= $newImage;
775 // this function expects that $_entry, $_header and $_imageIconFormat have already been read, specifically from readImageFromICO.
776 // Don't call this function except from there.
777 function readImageFromICO($filePointer, $entryOffset) {
778 $tmpPosition = ftell($filePointer); // So any other applications won't loose their position.
780 fseek($filePointer, $entryOffset);
781 $this->_entryIconFormat
= fread($filePointer, 16);
782 $this->_entry
= unpack("CWidth/CHeight/CColorCount/CReserved/SPlanes/SBitCount/LSizeInBytes/LFileOffset", $this->_entryIconFormat
);
784 if ($this->_entry
["SizeInBytes"] > 16384)
787 // Position the file pointer.
788 fseek($filePointer, $this->_entry
["FileOffset"]);
791 $this->_headerIconFormat
= fread($filePointer, 40);
792 $this->_header
= unpack("LSize/LWidth/LHeight/SPlanes/SBitCount/LCompression/LImageSize/LXpixelsPerM/LYpixelsPerM/LColorsUsed/LColorsImportant", $this->_headerIconFormat
);
795 $this->_imageIconFormat
= @fread
($filePointer, $this->_entry
["SizeInBytes"] - strlen($this->_headerIconFormat
));
796 fseek($filePointer, $tmpPosition); // So any other applications won't loose their position.
798 if ($newImage = @imagecreatefromstring
($this->_headerIconFormat
.$this->_imageIconFormat
)) {
799 // This is a PNG, the supposed header information is useless.
800 $this->_header
= array (
802 "Width" => imagesx($newImage),
803 "Height" => imagesy($newImage) * 2,
807 "ImageSize" => strlen($this->_imageIconFormat
),
811 "ColorsImportant" => 0,
813 imagedestroy($newImage);
816 // Support for larger images requires entry marked as 0.
817 if ($this->_entry
["Width"] == 0) {
818 $this->_entry
["Width"] = $this->_header
["Width"];
820 if ($this->_entry
["Height"] == 0) {
821 $this->_entry
["Height"] = $this->_header
["Height"]/2;
826 function getHeader() {
827 return $this->_header
;
829 function getEntry() {
830 return $this->_entry
;
832 function floIconImage() {
834 function getHeaderSize() {
835 return strlen($this->_headerIconFormat
);
838 return strlen($this->_imageIconFormat
);
840 function getImageResource() {
841 if (!$this->_imageResource
) $this->_createImageResource();
842 return $this->_imageResource
;
844 function dealocateResource() {
845 @imagedestroy
($this->_imageResource
);
846 $this->_imageResource
= null;