Using responsive image sizes with background images.
PermalinkI'm trying to wrap my head around trying to use the responsive thumbnails concept for the following scenario.
First I have a section of my template which has a background image. I needed to customize the background image for each page, so I followed the directions on this page:
https://www.concrete5.org/documentation/how-tos/designers/use-attrib...
And instead of using the "body" element to drop the background into, I use the Class for the element which I set a background image on. It works out quite nicely.
But where I'm trying failing to grasp is how do I implement responsive thumbnails for this image when the page is at various media sizes(desktop, tablet, smartphone)?
Do I need to connect the picturefils.js to my template or is it enough to create the create the various thumbnails sizes that will be generated when I upload the full size image? And how do these images get activate in a template. (The developer docs are a bit sketchy on that.)
Thanks.

Picturefill is a polyfill for the <picture> element. It doesn't support background images.
You can use the image helper and media queries to create responsive background images.
Here is an example using three breakpoints and three sizes of an image. It could easily be modified to add more breakpoints and image sizes.
<?php // get the image attribute object for the page $background_image = $c->getAttribute('background_image'); // use the image helper to get access to the getThumbnail() method $imageHelper = Core::make('helper/image'); // getThumbnail(image object, height, width in pixels, crop - true or false) // - height or width of 9999 means no maximum for that dimension $bgImageLarge = $imageHelper->getThumbnail($background_image, 9999, 1140, false); $bgImageMedium = $imageHelper->getThumbnail($background_image, 9999, 940, false); $bgImageSmall = $imageHelper->getThumbnail($background_image, 9999, 740, false); ?> <style> .your-class { background: url('<?php echo $bgImageSmall->src; ?>') no-repeat; /* additional background settings can be added like background-size and background-position */
I worked out a solution that doesn't use the thumbnails at all. Since I don't want multiple versions of all the images on the site being generated and filling up the server.
So what I did instead was define two image attributes for a page: Background Image and a Tablet Size Background image.
Then put the following into the header of my template:
<?php $backgroundImage = $c->getAttribute('background_image'); if ($backgroundImage) { $backgroundImageURL = $backgroundImage->getURL(); } ?> <?php $TabletbackgroundImage = $c->getAttribute('tabltbackgroundImg'); if ($TabletbackgroundImage) { $TbltbackgroundImageURL = $TabletbackgroundImage->getURL(); } ?> <style> @media (min-width: 1025px) { .my-background-image-class {
I know it's a little hacky/clunky but it so far it works..
http://srobbin.com/jquery-plugins/backstretch/...
That saves you adding multiple images. Or wouldn't it stretch to your likings and you really want device specific backgrounds?
But on this project I don't think that'd work.
Long story short, I have a div that gets a background image which at over 1200 wide browsers stretches 2000px and has a 500px height. Then at tablet size browsers I have the image cropped and focused differently (due to overlapping divs) and it has a width of 1024px by 500px. So essentially because of the cropping and size it has to be two different images.
This is also why the thumbnail approach wouldn't work.
Cheers,
Ed
<?php $backgroundImage = $c->getAttribute('background_image'); if ($backgroundImage) { $backgroundImageURL = $backgroundImage->getURL(); } ?> <?php $TabletbackgroundImage = $c->getAttribute('tabltbackgroundImg'); if ($TabletbackgroundImage) { $TbltbackgroundImageURL = $TabletbackgroundImage->getURL(); } ?> <style> @media (min-width: 1025px) { .my-background-image-class {
Now one thing to keep in mind is that, I'm not sure how much these PHP calls add to your page's upfront loads. Ideally you've want this to load last so that it doesn't block the page. If I figure out how to load this last on a page, I'll post the solution here.