定制 protonlabs/icofileloader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

protonlabs/icofileloader

Composer 安装命令:

composer require protonlabs/icofileloader

包简介

High quality PHP package for reading and converting any .ico icon file, particularly website favicons

README 文档

README

Software License Build Status Coverage Status Quality Score

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

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 protonlabs/icofileloader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 27.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-15