Download the PHP package rosell-dk/dom-util-for-webp without Composer
On this page you can find all versions of the php package rosell-dk/dom-util-for-webp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Rated 1.00 based on 1 reviews
Informations about the package dom-util-for-webp
dom-util-for-webp
Replace image URLs found in HTML
This library can do two things:
1) Replace image URLs in HTML 2) Replace <img> tags with <picture> tags, adding webp versions to sources
To setup with composer, run .
1. Replacing image URLs in HTML
The ImageUrlReplacer::replace($html) method accepts a piece of HTML and returns HTML where where all image URLs have been replaced - even those in inline styles.
Usage:
Example replacements:
input:
output:
Default behaviour of ImageUrlReplacer::replace:
- The modified URL is the same as the original, with ".webp" appended (to change, override the
replaceUrl
function) - Only replaces URLs that ends with "png", "jpg" or "jpeg" (no query strings either) (to change, override the
replaceUrl
function) - Attribute search/replace limits to these tags: <img>, <source>, <input> and <iframe> (to change, override the
$searchInTags
property) - Attribute search/replace limits to these attributes: "src", "src-set" and any attribute starting with "data-" (to change, override the
attributeFilter
function) - Urls inside styles are replaced too (background-image and background properties)
The behaviour can be modified by extending ImageUrlReplacer and overriding public methods such as replaceUrl
ImageUrlReplacer uses the Sunra\PhpSimple\HtmlDomParser
library for parsing and modifying HTML. It wraps simplehtmldom. Simplehtmldom supports invalid HTML (it does not touch the invalid parts)
Example: Customized behaviour
2. Replacing <img> tags with <picture> tags
The PictureTags::replace($html) method accepts a piece of HTML and returns HTML where where all <img> tags have been replaced with <picture> tags, adding webp versions to sources
Usage:
Example replacements:
Input:
Output:
Note that with the picture tags, it is still the img tag that shows the selected image. The picture tag is just a wrapper. So it is correct behaviour not to copy the style, width, class or any other attributes to the picture tag. See issue #9.
As with ImageUrlReplacer
, you can override the replaceUrl function. There is however currently no other methods to override.
PictureTags
currently uses regular expressions to do the replacing. There are plans to change implementation to use Sunra\PhpSimple\HtmlDomParser
, like our ImageUrlReplacer
class does.
Platforms
Works on (at least):
- OS: Ubuntu (22.04, 20.04, 18.04), Windows (2022, 2019), Mac OS (13, 12, 11, 10.15)
- PHP: 5.6 - 8.2 (also tested 8.3 and 8.4 development versions in October 2023)
Each new release will be tested on all combinations of OSs and PHP versions that are supported by GitHub-hosted runners. Except that we do not below PHP 5.6.\ Status:
Testing consists of running the unit tests. The code in this library is almost completely covered by tests (~95% coverage).
We also test future versions of PHP monthly, in order to catch problems early.\ Status:
Do you like what I do?
Perhaps you want to support my work, so I can continue doing it :)