PHPackages                             mathsgod/html-image-extractor - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. mathsgod/html-image-extractor

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mathsgod/html-image-extractor
=============================

Extract embedded data: URI images from HTML and replace with placeholders

00PHP

Since Apr 16Pushed 1mo agoCompare

[ Source](https://github.com/mathsgod/html-image-extractor)[ Packagist](https://packagist.org/packages/mathsgod/html-image-extractor)[ RSS](/packages/mathsgod-html-image-extractor/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

html-image-extractor
====================

[](#html-image-extractor)

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
------------

[](#requirements)

- PHP 8.1+

Installation
------------

[](#installation)

```
composer require mathsgod/html-image-extractor
```

How it works
------------

[](#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
-----

[](#usage)

### Basic — save to local directory

[](#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)

[](#advanced--custom-upload-eg-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
---

[](#api)

MethodDescription`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
-----------------------

[](#supported-image-formats)

`jpeg`, `png`, `gif`, `webp`, `svg`, `bmp`, `tiff`, `avif`

License
-------

[](#license)

MIT

###  Health Score

19

—

LowBetter than 10% of packages

Maintenance59

Moderate activity, may be stable

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity11

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/18732337?v=4)[Raymond](/maintainers/mathsgod)[@mathsgod](https://github.com/mathsgod)

---

Top Contributors

[![mathsgod](https://avatars.githubusercontent.com/u/18732337?v=4)](https://github.com/mathsgod "mathsgod (2 commits)")

### Embed Badge

![Health badge](/badges/mathsgod-html-image-extractor/health.svg)

```
[![Health](https://phpackages.com/badges/mathsgod-html-image-extractor/health.svg)](https://phpackages.com/packages/mathsgod-html-image-extractor)
```

###  Alternatives

[calotype/seo

A package containing SEO helpers.

732.6k](/packages/calotype-seo)[silverstripe/multiuser-editing-alert

A module that indicates when people are editing the same page in the CMS

1531.2k1](/packages/silverstripe-multiuser-editing-alert)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
