In the case of Fancy Images, we show our reader first a tiny image. And on her request — wants to say: click — also a larger version. For implementing this feature, I initially put the URL to the uploaded image in the href
-attribute of the fancy link and in the src
attribute of the img
tag. That slows down our site. We should fasten our fancy images:
Until now, the browser that is supposed to display the ‘Fancy Box Image’ always fetches the large image — even if the user only wants to see the reduced version. And then browser reduces it:
<a href="ORIGINAL_IMAGE_URL" data-fancybox=""><img src="ORIGINAL_IMAGE_URL" width="SMALLER_PLEASE"></a>
Thus, the browser that is supposed to display the image always downloads the large image — even if our user only wants to see the reduced version. And then it renders the large version down. In other words: the bigger the original image, the longer the download, the longer the rendering, and the later the display. But WordPress has already precalculated different versions of the images. So why not take the one first and get the big one only on demand?
Solution
- Check that your
functions.php
contains the following code for displaying block images1:
/*
* Simple short code for inserting a fancy boxed image
* (C) 2023 Karsten Reincke
* SPDX-License-Identifier: MIT
*/
function fcbPicCode($atts){
$atts = (array) $atts;
$imid = $atts[0]; // obligatoric parameter
$width = $atts[1]; // obligatoric parameter
$wpsize = ""; // thumbnail, medium, large, or full
$alt=""; // optional parameter
$style="is-style-default"; // optional parameter
$align="alignleft"; // optional parameter
/* IDEA: try to download the best fitting image size be default.
* Download the full sized image if and when the user had said
* she wants to see it by a click (fancy)
*/
if ($width <= 150)
$wpsize = "thumbnail";
elseif ($width <=300)
$wpsize = "medium";
elseif ($width <= 1024)
$wpsize = "large";
else
$wpsize = "full";
/* get the path to the original image */
$fimgp=wp_get_attachment_image_url($imid,"full");
if (!($fimgp)) return ("wrong image data");
/* if it's an svg, there won't be any smaller images */
if (wp_get_image_mime($fimgp) == "svg" )
$simgp=$fimgp;
else {
/* otherwise get the path to the fitting smaller image */
$simgp=wp_get_attachment_image_url($imid,$wpsize);
if (!($simgp)) $simgp=$fimgp;
}
if (array_key_exists(2,$atts)) {
$alt=$atts[2];
if (array_key_exists(3,$atts)) {
$style=$atts[3];
if (array_key_exists(4,$atts))
$align=$atts[4];
}
}
$res=
'<figure class="wp-block-image ' . $align . ' size-medium is-resized ' . $style . '">' .
'<a href="' . $fimgp . '" data-fancybox="">' .
'<img src="' . $simgp . '" alt="'. $alt .'" width="' . $width . '" >' .
'</a>' .
'</figure>';
return $res;
}
add_shortcode('fcbpic', 'fcbPicCode');
- Check that your
functions.php
contains the following code for displaying inline images:
/*
* Simple short code for inserting a fancy inline img
* (C) 2023 Karsten Reincke
* SPDX-License-Identifier: MIT
*/
function fciPicCode($atts){
$atts = (array) $atts;
$imid = $atts[0]; // obligatoric parameter
$width = $atts[1]; // obligatoric parameter
$wpsize = ""; // thumbnail, medium, large, or full
$alt=""; // optional parameter
$style="is-style-default"; // optional parameter
/* IDEA: try to download the best fitting image size be default.
* Download the full sized image if and when the user had said
* she wants to see it by a click (fancy)
*/
if ($width <= 150)
$wpsize = "thumbnail";
elseif ($width <=300)
$wpsize = "medium";
elseif ($width <= 1024)
$wpsize = "large";
else
$wpsize = "full";
/* get the path to the original image */
$fimgp=wp_get_attachment_image_url($imid,"full");
if (!($fimgp)) return ("wrong image data");
/* if it's an svg, there won't be any smaller images */
if (wp_get_image_mime($fimgp) == "svg" )
$simgp=$fimgp;
else {
/* otherwise get the path to the fitting smaller image */
$simgp=wp_get_attachment_image_url($imid,$wpsize);
if (!($simgp)) $simgp=$fimgp;
}
if (array_key_exists(2,$atts)) {
$alt=$atts[2];
if (array_key_exists(3,$atts)) {
$style=$atts[3];
}
}
$res=
'<a href="' . $fimgp . '" data-fancybox="">' .
'<img src="' . $simgp . '" alt="'. $alt .'" width="' . $width . '" >' .
'</a>' ;
return $res;
}
add_shortcode('fcipic', 'fciPicCode');
Background
The idea of these functions is clear: We let WordPress find the image file that is closest to the requested display width. But for SVG
s, WordPress (or the corresponding plugin) does not pre-calculate other versions.2 Therefore, we first get the original image, check if it is an SVG, and in the case of raster graphics let WordPress find the link to the image that fits best to the desired size. This would create an entry on the page like this
<a href="ORIGINAL_IMAGE_URL" data-fancy=""><img src="SMALL_IMAGE_URL" width="SMALLER_PLEASE"></a>
BINGO.
And how does this …
… support our migration to bootScore? Well, once a web designer has taken the first general steps and has checked SEO, she will soon turn to a really thick board, her properly clustered and presented menu without getting too fancy. Eventually, she is going to spruce and speed up the display of fixed and scalable images. This post talks about a part of this way.
- Of course, I have already updated the elder versions in the elder posts. So, probably you will already have incorporated the improved version into your
functions.php
. If so, take this post an explanation how to deal with the pictures WordPress pre-calculates when you upload your image. [↩] - Computing other sizes in advance would make no sense, because SVGs do not have an intended initial size. [↩]