protonlabs/icofileloader
Composer 安装命令:
composer require protonlabs/icofileloader
包简介
High quality PHP package for reading and converting any .ico icon file, particularly website favicons
README 文档
README
Fork from https://github.com/lordelph/icofileloader to make it compatible with newer PHP versions
This package provides a means to load and convert .ico files in a PHP application. It has no dependencies apart from gd for rendering.
The package has unit tests which verify support for 1bit, 4bit, 8bit, 24bit and 32bit .ico files, and the newer form of .ico files which can included embedded PNG files.
Installation
IcoFileLoader is available via Composer:
composer require lordelph/icofileloader
Usage
The IcoFileService class provides a one-shot method
extractIcon. This should suit most use-cases where you simply want to get one
image out of a .ico file.
It returns an image resource, which you can further manipulate with GD functions, e.g. save it to a file with imagepng
For example, here's how you extract a 32x32 transparent image from an ico file:
$loader = new Elphin\IcoFileLoader\IcoFileService; $im = $loader->extractIcon('/path/to/icon.ico', 32, 32); //$im is a GD image resource, so we could, for example, save this as a PNG imagepng($im, '/path/to/output.png');
Render with background color
Instead of retaining the alpha channel from the icon, you can render with a background color instead - pass the required color as a renderer option as follows:
$im = $loader->extractIcon('/path/to/icon.ico', 32, 32, ['background'=>'#FFFFFF']);
Extract icon at any size
The extractIcon method will try find an image in the icon which is the exact
size you request at highest color depth it can find. If it can't, it will resize the
best quality image in the icon. So, you can request any size you require...
$im = $loader->extractIcon('/path/to/icon.ico', 100, 100);
Extract icon from a URL
As long you have the PHP fopen wrappers
installed, you can pass a URL to extractIcon
$im = $loader->extractIcon('https://assets-cdn.github.com/favicon.ico', 16, 16);
Extract icon from binary data
If you already have an ico file held as a binary string, extractIcon will cope
with that just fine too:
$data = file_get_contents('/path/to/icon.ico'); $im = $loader->extractIcon($data, 16, 16);
Lower level methods
If you want to do more than just extract a single image from an icon, you can use lower level methods of IcoFileService to inspect an .ico file and perform multiple renderings.
The fromFile, fromString and from methods will parse an ico file and return
an Icon instance representing an icon and the images
it contains.
You can iterate the images in icon, examine them, and render them with renderImage
For example, here's how you could extract all the images in an icon and save them as individual files.
$icon = $loader->fromFile('/path/to/icon.ico'); foreach ($icon as $idx => $image) { $im=$loader->renderImage($image); $filename=sprintf('img%d-%dx%d.png', $idx, $image->width, $image->height); imagepng($im, $filename); printf("rendered %s as %s\n", $image->getDescription(), $filename); }
Internals
The service is composed of a parser and a renderer, which can be injected into the service at runtime if you wanted to override them.
The current GdRenderer works by drawing individual pixels for BMP based icon images. This isn't going to be terribly fast. PHP 7.2 will have support for BMP images, and I'll add a renderer which takes advantage of that when it is released.
Testing
$ composer test
Contributing
Please see CONTRIBUTING for details.
Credits
- Paul Dixon - 2017 modernization / update
- Diogo Resende. Original author of 2005 library this was derived from.
Thanks also to the PHP League's skeleton project from which this project's structure was derived.
License
The MIT License (MIT). Please see License File for more information.
Note: this was based on some classes originally written in 2005 by Diogo Resende. While these were originally provided on the PHPClasses site under a GPL license, Diogo kindly agreed to allow them to be licensed under an MIT license.
protonlabs/icofileloader 适用场景与选型建议
protonlabs/icofileloader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27.17k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「convert」 「extract」 「icon」 「load」 「favicon」 「ico」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 protonlabs/icofileloader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 protonlabs/icofileloader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 protonlabs/icofileloader 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a simple decimal/roman number converter
A library that provides a simple infrastructure to create your own converters and to perform any conversion you want
PHP Library to get the apple-touch-icons and favicon from a website.
Icon support for Silverstripe taxonomy terms
Convert xml to array
Convert doctrine dbal query result to dto
统计信息
- 总下载量: 27.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-08-15