kodie/img-items
Composer 安装命令:
composer require kodie/img-items
包简介
Finds individual items inside an image and gets their size/coordinates.
README 文档
README
Finds individual items inside an image and gets their size/coordinates. A PHP port of the img-items Node module.
Example
| Source | Result |
|---|---|
![]() |
![]() |
<?php require_once('vendor/autoload.php'); $contents = file_get_contents('assets/feed-example.png'); $img = imagecreatefromstring($contents); $items = img_items($img); foreach($items as $item) { for ($y = $item['top']; $y < $item['bottom'] + 1; $y++) { for ($x = $item['left']; $x < $item['right'] + 1; $x++) { imagesetpixel($img, $x, $y, imagecolorallocate($img, 255, 0, 0)); } } } imagepng($img, 'assets/feed-example-filled.png', 0); imagedestroy($img); ?>
Caveat
The larger the image and/or the more background colors defined to compare against, the slower the module will run. There are more than likely some things that could be approved upon to make it more efficient but it does work as is.
Installation
composer require kodie/img-items
Usage
img_items(image, options)
<?php require_once('vendor/autoload.php'); $items = img_items('my-image.jpg', array( 'background' => 0, 'background_threshold' => 5, 'gap_threshold' => 5, 'size_threshold' => 5 )); ?>
Parameters
$image
The image to scan for items. Accepts a file path to an image, a gd Image Resource, or a GdImage Instance.
$options
An array with options you can set to tweak how items are found:
-
background(Default:0) - The color(s) that should be considered the background of the image. Accepts any string with a color value that the color component factory can convert to a color (examples:#FFFFFF,rgb(255, 0, 0),hsl(240, 100%, 50%)), an array of those color strings, an integer set to0to use the top left pixel color, an integer set to-1to set the background color to either black or white depending on the average luminance of the image, or an integer from1to10to use that number of primary colors from the image. (Keep in mind that the more colors that are defined here, the slower the module will run.) -
background_threshold(Default:5) - An integer from0to100defining how close a color can be to the background color (using the CIE76 Color Difference Formula) for it to be considered part of the background.0being an exact match. -
gap_threshold(Default:5) - An integer defining how many pixels of background color before an item is considered it's own item. -
gap_y_threshold(Default:null) - The same asgap_thresholdbut only on theYaxis. (Setting tonullwill fallback togap_threshold) -
gap_x_threshold(Default:null) - The same asgap_thresholdbut only on theXaxis. (Setting tonullwill fallback togap_threshold) -
size_threshold(Default:5) - An integer defining how many pixels wide and high an item should be to make the cut. If an item has a width or height lower than this value it will be filtered out of the results. -
height_threshold(Default:null) - The same assize_thresholdbut only for the item's height. (Setting tonullwill fallback tosize_threshold) -
width_threshold(Default:null) - The same assize_thresholdbut only for the item's width. (Setting tonullwill fallback tosize_threshold)
Example Response
Array
(
[0] => Array
(
[left] => 728
[top] => 42
[right] => 782
[bottom] => 62
[width] => 55
[height] => 21
)
[1] => Array
(
[left] => 47
[top] => 44
[right] => 58
[bottom] => 61
[width] => 12
[height] => 18
)
[2] => Array
(
[left] => 66
[top] => 44
[right] => 93
[bottom] => 61
[width] => 28
[height] => 18
)
)
Related
- img-items - The Node module this module was based on.
License
MIT. See the license.md file for more info.
kodie/img-items 适用场景与选型建议
kodie/img-items 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 157 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 08 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「item」 「detect」 「gd」 「extract」 「img」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kodie/img-items 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kodie/img-items 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kodie/img-items 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel 5 Package Provider to Identify/detect a user's browser, device, operating system and Language
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
The Yii2 extension uses jQuery PrettyPhoto and OwlCarousel js and makes image galary from php array of structure defined.
MobileDetect library for YiiFramework 1.x
Yii2 extension for Mobile-Detect library
统计信息
- 总下载量: 157
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-28

