To avoid negative Cumulative Layout Shift (CLS) ranking I need to set default width and height attributes to all images that an author writes in markdown content field.
Is there a possibilty to set them according to objects original dimensions? I need help to find out how to modify the output of the image.
In the same way I would like to set default classes to use own javascript like lazyload.
Yes, I would have to generate this for all pages by default, including the approx. 1000 pages that already exist. Without the editors having to do it.
I thought of a possibility to generally change the output of the image markup by changing the translation of the markdown into html. Maybe via a hook in a plugin. I just haven't found out which one it is and whether there is an event for it.
This event is fired after the page's content() method has processed the page content. This is particularly useful if you want to perform actions on the post-processed content but ensure the results are cached. Performance is not a problem because this event will not run on a cached page, only when the cache is cleared or a cache-clearing event occurs.
@christiana83, Did some playing with code. The following onPageContentProcessed event handler seems to work.
Note: Used images are in page's folder.
PHP
publicfunctiononPageContentProcessed($event){$page=$event['page'];$media=$page->getMedia();$content=$page->getRawContent();// Get all <img> elements$matches=[];preg_match_all('#<img.*src="([^"]+)".*?/>#',$content,$matches);$contentChanged=false;for($i=0;$i<count($matches[0]);$i++){// Skip <img> elements that already contains width and/or heightif(preg_match('#(width|height)#',$matches[0][$i])){continue;}$src=$matches[1][$i];$filename=substr($src,strrpos($src,'/')+1);$img=$media->get($filename);$oldElement=substr($matches[0][$i],0,-2);$newElement=$oldElement.'width="'.$img['width'].'" height="'.$img['height'].'"';$content=str_replace($oldElement,$newElement,$content);$contentChanged=true;}if($contentChanged){$page->setRawContent($content);}}
<code>1.</code> Update Markdown by one-time run of plugin.
You could also loop through all pages in onPagesInitialized and update the Markdown. This should be a one-time action updating all pages once and for all.
So I choose the solution to use event hook onMarkdownInitialized($event) for manipulating the image output. This way I can also use to add the dimensions of the image later.
@pamtbaau Yes, but native lazyloading by loading="lazy" is still not working in all browsers, e.g. safari.
Additionally the website ist that big and connected to other applications like attlassian jira and additionally with several static exports so that we havent't yet updated to the latets grav version. that issue is in progress.
And we have own image markups from twig files page modules with <picture> tags that need an lazyloading library.
Above PR has been merged (albeit, without much of my initial code :-) ), which will allow height and width to be automatically added to images.
system.yaml has received a new option:
YAML
images:cls:# Cumulative Layout Shift: See https://web.dev/optimize-cls/auto_sizes:false# Automatically add height/width to imageaspect_ratio:false# Reserve space with aspect ratio styleretina_scale:1# scale to adjust auto-sizes for better handling of HiDPI resolutions