PHPackages                             waughj/html-image - 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. [Image &amp; Media](/categories/media)
4. /
5. waughj/html-image

ActiveLibrary[Image &amp; Media](/categories/media)

waughj/html-image
=================

Simple class for automatically generating image HTML code.

v0.9.3(6y ago)02105AGPL-3.0-or-laterPHPPHP &gt;=7.0

Since May 13Pushed 6y agoCompare

[ Source](https://github.com/waughjai/html-image)[ Packagist](https://packagist.org/packages/waughj/html-image)[ RSS](/packages/waughj-html-image/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (5)Versions (17)Used By (5)

HTML Image
==========

[](#html-image)

Simple class for encapsulating image info and autogenerating the HTML for it.

The 1st mandatory argument to pass into the constructor is the src.

2nd optional argument is a FileLoader instance that will make loading images from certain URLs &amp; adding a cache-corruption-fixing version parameter to it. For no FileLoader, pass in null. For mo' info on FileLoader, visit .

3rd is an optional hash map o' attributes.

If no alt attribute is passed in, an empty alt attribute will automatically be added to the image's HTML, as per proper HTML protocol.

Example
-------

[](#example)

```
use WaughJ\HTMLImage\HTMLImage;
use WaughJ\FileLoader\FileLoader;

$image = new HTMLImage
(
	'image.png',
	new FileLoader([ 'directory-url' => 'https://www.example.com' ]),
	[
		'class' => 'center-img ornate',
		'width' => '600',
		'height' => '400'
	]
);
$image->print();

```

Will output ``

```
echo $image->getSource();

```

Will output `https://www.example.com/image.png`

### Changing Attributes o' Already-Existing Instances

[](#changing-attributes-o-already-existing-instances)

You can also set attributes or add classes to an already-created image using the "setAttribute" &amp; "addToClass" methods:

```
$image = new HTMLImage
(
	new FileLoader([ 'directory-url' => 'https://www.example.com' ]),
	'image.png',
	[
		'class' => 'center-img ornate',
		'width' => '600',
		'height' => '400'
	]
);
$image = $image->addToClass( 'new-image' );
$image = $image->setAttribute( 'id', 'first-image' )
$image->print();

```

Will output ``

Note that "setAttribute" &amp; "addToClass" do not directly change object, which is immutable, but return a clone o' the image with the changes. Thus, if you want to change an object, you must set the object equal to the output o' the method call: `$image = $image->setAttribute( 'id', 'first-image' )`. Just `$image->setAttribute( 'id', 'first-image' )` won't do anything.

### Responsive Images

[](#responsive-images)

HTMLImage makes setting srcset &amp; sizes for responsive images easier &amp; mo’ convenient.

For instance, if srcset is set, but not sizes, the constructor will automatically generate a sizes attribute based on how srcset is set, which will regenerate if srcset is changed later, but will ne’er o’erride manually-set sizes.

Example:

```
use WaughJ\HTMLImage\HTMLImage;

$image = new HTMLImage
(
	"demo.png",
	null,
	[ 'srcset' => 'demo-300x300.png 300w, demo-800x500.png 800w, demo.png 1280w' ]
);

// Will output “”
$image->print();

```

HTMLImage can also recognize a shorthand version o’ srcset using : as delimiter following the following pattern:

```
[$path].[$extension]:[$width]x[$height],[$width]x[$height][…]

```

For each comma-delimited size, height is optional. Not providing a height will simply use the base filename as the full filename &amp; will just use the width as the width tag that goes after the filename.

For example, to print out the same content as the previous example, you can type out ’stead:

```
use WaughJ\HTMLImage\HTMLImage;

$image = new HTMLImage
(
	"demo.png",
	null,
	[ 'srcset' => 'demo.png:300x300,800x500,1280' ]
);

// Will output “”
$image->print();

```

Malformed srcset values will throw a MalformedSrcSetStringException. These are srcset values that don’t follow the standard HTML format or the shorthand format.

If for some reason you want to, you can pass in an array o’ SrcSetItem instances ’stead o’ a string:

```
use WaughJ\HTMLImage\HTMLImage;
use WaughJ\HTMLImage\SrcSetItem;

$image = new HTMLImage
(
	"demo.png",
	null,
	[ 'srcset' => [ new SrcSetItem( 'demo', 300, 300, 'png' ), new SrcSetItem( 'demo', 800, 500, 'png' ), new SrcSetItem( 'demo', 1280, -1, 'png' ) ] ]
);

// Will output “”
$image->print();

```

As shown, to make a srcset item keep the base filename &amp; only use the width as a width tag, pass -1 for height.

You can also pass in a SrcSet instance instead o’ an array, which can take in an array o’ SrcSetItem instances, an array o’ strings, or a string. ( All o’ the options shown here are simply passed into a SrcSet constructor, including a SrcSet instance, in the backend, anyway ).

### Error Handling

[](#error-handling)

The HTMLImage constructor may throw a WaughJ\\FileLoader\\MissingFileException exception if it is set to show a version tag ( the default ) &amp; it can't access the file to get its modified date ( usually caused by the file not being where it's expected to be ). This exception includes in its getFallbackContent method with an HTMLImage object with the versionless source for easy recovery like so ( while the getFilename method can be used to find where it's trying to find the file on the server ):

```
use WaughJ\HTMLImage\HTMLImage;
use WaughJ\FileLoader\FileLoader;
use WaughJ\FileLoader\MissingFileException;

try
{
	$image = new HTMLImage
	(
		'image.png',
		new FileLoader([ 'directory-url' => 'https://www.example.com' ]),
		[
			'class' => 'center-img ornate',
			'width' => '600',
			'height' => '400'
		]
	);
}
catch ( MissingFileException $e )
{
	// Maybe log somewhere that it couldn't find the file located @ $e->getFilename().
	$image = $e->getFallbackContent(); // This will be the equivalent o' the image with its 'show_version' property false.
}

$image->print(); // Will still work, e'en if an exception is thrown.

```

As mentioned in the Responsive Images section, malformed srcset values will throw a MalformedSrcSetStringException.

Changelog
---------

[](#changelog)

### 0.9.0

[](#090)

- Make Autogenerate Sizes Attribute When Srcset is Set But Not Sizes &amp; Implement Shorthand Format for Srcset

### 0.8.0

[](#080)

- Revamp SrcSet Handling &amp; Error Handling

### 0.7.0

[](#070)

- Optimize By Adding HTML Cache

### 0.6.0

[](#060)

- Revamp Error Handling for Missing Files

### 0.5.0

[](#050)

- Add "setAttribute" &amp; "addToClass" Methods

### 0.4.0

[](#040)

- Add Ability to Just Get Source

### 0.3.0

[](#030)

- Add Ability to Work with FileLoaders

### 0.2.0

[](#020)

- Add Auto String Conversion &amp; Print Method

### 0.1.0

[](#010)

- Initial Version

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 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.

###  Release Activity

Cadence

Every ~2 days

Recently: every ~8 days

Total

16

Last Release

2521d ago

### Community

Maintainers

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

---

Top Contributors

[![jjwmezun](https://avatars.githubusercontent.com/u/24711797?v=4)](https://github.com/jjwmezun "jjwmezun (5 commits)")

---

Tags

imagehtmlautogenerate

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[intervention/image

PHP Image Processing

14.3k194.3M2.2k](/packages/intervention-image)[league/glide

Wonderfully easy on-demand image manipulation library with an HTTP based API.

2.6k51.2M116](/packages/league-glide)[liip/imagine-bundle

This bundle provides an image manipulation abstraction toolkit for Symfony-based projects.

1.7k38.3M217](/packages/liip-imagine-bundle)[spatie/image

Manipulate images with an expressive API

1.4k54.4M138](/packages/spatie-image)[picqer/php-barcode-generator

An easy to use, non-bloated, barcode generator in PHP. Creates SVG, PNG, JPG and HTML images from the most used 1D barcode standards.

1.8k25.5M88](/packages/picqer-php-barcode-generator)[intervention/image-laravel

Laravel Integration of Intervention Image

1496.5M102](/packages/intervention-image-laravel)

PHPackages © 2026

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