Advanced Image Configuration Example
Learn how to configure responsive image sizes and optimize image handling with the Combo plugin.
Custom Image Sizes Configuration
This example demonstrates how to configure custom image sizes for different use cases:
config.yml
1# Advanced image configuration2pictures:3yearmonth_folders: true # Organize uploads by year/month45placeholder: # Image placeholder settings6text: 'Image not available'78thumbnail: # Default thumbnail size9width: 15010height: 15011crop: true1213sizes: # Custom image sizes14# Content images15medium:16width: 76817height: 018crop: false19large:20width: 120021height: 022crop: false2324# Hero images25hero-small:26width: 57627height: 30028crop: true29hero-medium:30width: 76831height: 40032crop: true33hero-large:34width: 120035height: 50036crop: true37hero-xlarge:38width: 192039height: 60040crop: true4142# Square images for cards and thumbnails43square-small:44width: 15045height: 15046crop: true47square-medium:48width: 30049height: 30050crop: true51square-large:52width: 60053height: 60054crop: true5556# Portrait images for team members57portrait-small:58width: 20059height: 30060crop: true61portrait-medium:62width: 40063height: 60064crop: true6566# Landscape images for featured posts67landscape-small:68width: 40069height: 30070crop: true71landscape-medium:72width: 80073height: 60074crop: true7576# Gallery images77gallery-thumbnail:78width: 20079height: 20080crop: true81gallery-full:82width: 120083height: 80084crop: false
Using Custom Image Sizes
Here's how to use these custom image sizes in your theme:
Hero Section
1<?php2// Get the hero image from ACF3$hero_image = get_field('hero_image');45// Use responsive image sizes with srcset6cbo_acf_img($hero_image, 'hero-large', [7['hero-small', 576],8['hero-medium', 768],9['hero-large', 1200],10['hero-xlarge', 1920],11['sizes', '100vw']12], [13'class' => 'hero-image',14'lazy' => true15]);16?>
Team Member Grid
1<?php2// Get team members3$team_members = get_field('team_members');45if ($team_members) {6echo '<div class="team-grid">';78foreach ($team_members as $member) {9echo '<div class="team-member">';1011// Display team member photo12cbo_acf_img($member['photo'], 'portrait-medium', [13['portrait-small', 576],14['portrait-medium', 992]15], [16'class' => 'team-member-photo',17'lazy' => true18]);1920echo '<h3>' . $member['name'] . '</h3>';21echo '<p>' . $member['position'] . '</p>';22echo '</div>';23}2425echo '</div>';26}27?>
Featured Posts
1<?php2// Get featured posts3$featured_posts = get_field('featured_posts');45if ($featured_posts) {6echo '<div class="featured-posts">';78foreach ($featured_posts as $post) {9setup_postdata($post);1011echo '<div class="featured-post">';1213// Display featured image14if (has_post_thumbnail()) {15$featured_image_id = get_post_thumbnail_id();1617cbo_img($featured_image_id, [18'size' => 'landscape-medium',19'class' => 'featured-post-image',20'lazy' => true21]);22}2324echo '<h3><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>';25echo '<div class="excerpt">' . get_the_excerpt() . '</div>';26echo '</div>';27}2829wp_reset_postdata();30echo '</div>';31}32?>
Image Gallery
1<?php2// Get gallery images3$gallery_images = get_field('gallery');45if ($gallery_images) {6echo '<div class="gallery">';78foreach ($gallery_images as $image) {9echo '<div class="gallery-item">';1011// Create a link to the full-size image12echo '<a href="' . $image['url'] . '" class="gallery-link" data-fancybox="gallery">';1314// Display thumbnail15cbo_acf_img($image, 'gallery-thumbnail', null, [16'class' => 'gallery-thumbnail',17'lazy' => true18]);1920echo '</a>';21echo '</div>';22}2324echo '</div>';25}26?>
Optimizing Image Loading
Configure lazy loading for images to improve page load performance:
1# Configure lazy loading2assets:3version: "1.0.0"4lazyload: true # Enable native lazy loading56# Or use vanilla JavaScript lazy loading7# lazyload: 'vanilla'
When using vanilla JavaScript lazy loading, you'll need to include a lazy loading script:
1// Simple vanilla JavaScript lazy loading2document.addEventListener('DOMContentLoaded', function() {3var lazyImages = [].slice.call(document.querySelectorAll('img.lazy'));45if ('IntersectionObserver' in window) {6let lazyImageObserver = new IntersectionObserver(function(entries, observer) {7entries.forEach(function(entry) {8if (entry.isIntersecting) {9let lazyImage = entry.target;10lazyImage.src = lazyImage.dataset.src;11lazyImage.srcset = lazyImage.dataset.srcset;12lazyImage.classList.remove('lazy');13lazyImageObserver.unobserve(lazyImage);14}15});16});1718lazyImages.forEach(function(lazyImage) {19lazyImageObserver.observe(lazyImage);20});21} else {22// Fallback for browsers without IntersectionObserver support23let active = false;2425const lazyLoad = function() {26if (active === false) {27active = true;2829setTimeout(function() {30lazyImages.forEach(function(lazyImage) {31if ((lazyImage.getBoundingClientRect().top <= window.innerHeight && lazyImage.getBoundingClientRect().bottom >= 0) && getComputedStyle(lazyImage).display !== 'none') {32lazyImage.src = lazyImage.dataset.src;33lazyImage.srcset = lazyImage.dataset.srcset;34lazyImage.classList.remove('lazy');3536lazyImages = lazyImages.filter(function(image) {37return image !== lazyImage;38});3940if (lazyImages.length === 0) {41document.removeEventListener('scroll', lazyLoad);42window.removeEventListener('resize', lazyLoad);43window.removeEventListener('orientationchange', lazyLoad);44}45}46});4748active = false;49}, 200);50}51};5253document.addEventListener('scroll', lazyLoad);54window.addEventListener('resize', lazyLoad);55window.addEventListener('orientationchange', lazyLoad);56}57});
Best Practices for Image Handling
- Define image sizes based on their purpose and where they'll be used in your theme
- Use descriptive names for custom image sizes
- Enable lazy loading for images below the fold
- Use srcset and sizes attributes for responsive images
- Set appropriate crop parameters based on the image's purpose
- Consider using WebP format for better compression (requires additional configuration)