mathsgod/html-image-extractor
Composer 安装命令:
composer require mathsgod/html-image-extractor
包简介
Extract embedded data: URI images from HTML and replace with placeholders
README 文档
README
A PHP library to extract embedded data: URI images from HTML, replace them with temporary placeholders, and restore them with real URLs once the images have been saved or uploaded.
Requirements
- PHP 8.1+
Installation
composer require mathsgod/html-image-extractor
How it works
HTML (with data: URIs)
↓ extract()
HTML with __IMG_xxx__ placeholders + image data map
↓ save / upload images, get URLs
↓ restore()
Final HTML (with real URLs)
Usage
Basic — save to local directory
use HtmlImageExtractor\HtmlImageExtractor; $extractor = new HtmlImageExtractor(); // Step 1: extract embedded images $extractor->extract($html); $modifiedHtml = $extractor->getHtml(); // HTML with __IMG_xxx__ placeholders $images = $extractor->getImages(); // image data map echo $extractor->count() . ' image(s) found'; // Step 2: save to disk and get URL map $urlMap = $extractor->saveToDir( saveDir: __DIR__ . '/uploads', baseUrl: 'https://example.com/uploads' ); // Step 3: restore placeholders with real URLs $finalHtml = $extractor->restore($urlMap);
Advanced — custom upload (e.g. cloud storage)
$extractor->extract($html); // Build the URL map yourself after uploading $urlMap = []; foreach ($extractor->getImages() as $id => $info) { // $info['mimeType'] — e.g. "image/png" // $info['data'] — base64 encoded image data // $info['extension'] — e.g. "png" $url = myCloudUpload(base64_decode($info['data']), $info['mimeType']); $urlMap[$id] = $url; } $finalHtml = $extractor->restore($urlMap);
API
| Method | Description |
|---|---|
extract(string $html): static |
Extract all data: URI images and replace with placeholders. Returns $this for chaining. |
getHtml(): string |
Get the HTML with placeholders (after extract()). |
getImages(): array |
Get extracted image data keyed by placeholder ID. Each entry has mimeType, data (base64), extension. |
count(): int |
Number of images found in the last extract() call. |
saveToDir(string $saveDir, string $baseUrl): array |
Save images to a local directory. Returns a urlMap ready for restore(). |
restore(array $urlMap): string |
Replace placeholders with real URLs. Returns final HTML. |
Supported image formats
jpeg, png, gif, webp, svg, bmp, tiff, avif
License
MIT
mathsgod/html-image-extractor 适用场景与选型建议
mathsgod/html-image-extractor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mathsgod/html-image-extractor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mathsgod/html-image-extractor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 34
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2026-04-16