Avoiding distorted featured images is possible. We have shown that. However, our method is not yet optimal: if we let WordPress when uploading pictures, also crop the medium-sized thumbnails as squares, we give up what is commonly expected. Maybe other plugins need the ‘medium’ thumbnails in the original aspect ratio after all. So it would be better if we left the common thumbnail formats untouched. But we still need larger picture squares:
Solution
- Enter the following lines in your
functions.php
:
// let WordPress also derive this square version from the uploaded images
add_image_size( 'bsTeaser', 600, 600, true );
- Replace the
true
or1
withfalse
or0
in the recently added lines, with which we had also enabled cropping of the middle size:
//do not(!) crop the medium size images to the defined values exactly
// as it is done for thumbnails
if(false === get_option("medium_crop")) {
add_option("medium_crop", "0");
} else {
update_option("medium_crop", "0");
}
- Copy
bootscore-main/index.php
tobootscore-child/home.php
. - In
home.php
, replace the stringget_the_post_thumbnail(null, 'medium')
in both places with ‘get_the_post_thumbnail(null, ‘bsTeaser’)
Background
Let’s review these steps for understanding the idea:
With the directive add_image-size, we enforce WordPress — when it loads an image into the media library — not only to compute the traditional smaller sizes as they are configured in Settings/Media, but also to create a 600x600
thumbnail. With the name bsTeaser we can request the path to this new image from WordPress to embed it in a post.1 And with the inserted value true we make WordPress cropping the original to a square.
So, why 600x600 and not 300x300 or 400x400 squares? This is due to bootScore! Bootstrap — and thus bootScore — do not work with absolute sizes. Rather, they divide the available space into 12 columns of equal width. And sizes are then specified relative to column widths. Something like as wide as including column 4 up to and including column 6. Of course, Bootstrap says this much more elegantly and in a more ‘encapsulated’ way.
But the effect is the same: In a responsive design, the size of the screen showing a page determines how much space there actually is from column 4 to column 6. So, the browser of another machine mostly must convert an image that by default fits into the space defined by the size of the first.
Rendering a picture down preserves the sharpness. Only some details become invisible. But rendering a picture up blurs it: Neither WordPress nor the browser knows, how to fill the new space in accordance with reality. So if we want to add images to bootstrap areas in a way that they look good on large screens, we need to increase the likelihood that the images will be rendered down rather than rendered up. The best way to do that would be to just embed the original images into the pages and let the client machine decrease them.
But this slows down the delivery of a page: transferring larger images over the net takes more time than smaller ones. And decreasing larger images needs more computing time than decreasing smaller ones. In fact, we are trying to find empirically the best solution. And thus — for now — 600x600 seems to us to be the best size.
After all, in our previous post, we had urged WordPress to crop the medium size as well. We have to resolve that. Just deleting the corresponding lines from the functions.php
is not enough, though. WordPress has remembered our previous settings. So we have to overwrite them explicitly.
Finally, we copy the file index.php
from the bootScore main theme under the name home.php
into our child theme folder. This enforces WordPress to start on a higher level of its template hierarchy. So we don’t deprive ourselves of the fallback solution.
Remaining to mention, why we don’t replace the previous solution altogether with this ‘better’ one? Well, sometimes good is good enough. The better one would only make more work without advancing the result. So if you are sure that you don’t need the medium files in the original aspect ratio, you can well stay with our first approach.
And how does this …
… support our migration to bootScore? Well, once started with improving the image handling, a web designer will also notice the blurred ‘featured images’ of bootScore. She will try and refine solutions. And she may also tackle them with new HTML‑5 techniques. Because with that, a fancier image strategy combined with an integrated license fulfillment process and its own logo will really make sense. However, pictures bring colors to reading. So they should be integrated into a customized color concept. This post also contributes something to this topic.
- on php level [↩]