I have this:
<img src="large/image.png" />
I want this:
<picture>
<source media="(min-width:800px)" srcset="medium/image.png">
<source media="(min-width:300px)" srcset="small/image.png">
<img src="large/image.png" />
</picture>
I want to do it with PHP's DOMDocument and have tried this code: (there may be more than one image in the html)
$domContent = $domDocument->loadHTML($html);
$images = $domDocument->getElementsByTagName('img');
foreach ($images as $image) {
$picture = $domDocument->createElement('picture');
// $pic_clone = $picture->cloneNode();
$source = $domDocument->createElement('source');
$source->setAttribute("media", "(min-width:800px)");
$source->setAttribute("srcset", "path_large");
$source->setAttribute("media", "(min-width:300px)");
$source->setAttribute("srcset", "path_small");
$src_clone = $source->cloneNode();
$src_clone->appendChild($picture);
$image->parentNode->replaceChild($src_clone, $image);
$src_clone->appendChild($image);
}
This produces:
<source media="(min-width:300px)" srcset="path_small">
<picture></picture>
<img src="large/image.png"></source>
- How to place the picture tag correctly?
- How do I get both source tags ?
- How can I avoid the closing source tag ?
I just cant wrap my head around this...
PS: Dont bother about the image paths, those I will fix later.