Skip to main content
KadiCache
Browse 37 Docs Guides & Search

Image Lazyload, Hero LCP Protection & Missing Width/Height Injection

Defer below-the-fold <img> and <picture> tags while automatically keeping above-the-fold hero images eager and injecting missing width/height attributes across images and SVGs.

Optimization/ImageLazyOptimizer.phpOptimization/Images/ImageContainerClassifier.phpOptimization/Images/ImageDimensionResolver.php
WP Admin -> KadiCache -> Lazyload
MODULE 19 · 2X RETINA
Lazy-load offscreen images, CSS backgrounds, and YouTube embeds while injecting missing width/height dimensions.2880×1840 WebP
Quick Summary · 30-Second Operational SpecSource-Verified Guide
WP Admin Path
WP Admin -> KadiCache -> Lazyload
Expected Outcome
60–85% Fewer Initial Image Requests · 0 Unsized-Image CLS
Key Marker / Engine
ImageLazyOptimizer.php | ImageDimensionResolver.php
  • Defers below-the-fold <img> and <picture> downloads (ImageLazyOptimizer.php) so mobile bandwidth focuses 100% on visible above-the-fold content.
  • Automatically keeps logos, <header> graphics, and above-the-fold hero images eager (ImageContainerClassifier.php) so Largest Contentful Paint (LCP) is never penalized.
  • Resolves intrinsic pixel dimensions from local image headers and SVG viewBox attributes (ImageDimensionResolver.php), injecting missing width and height attributes to prevent image layout shifts.
  • Supports filename, CSS class (.no-lazy, .skip-lazy), page-path, and per-post Gutenberg exclusions.

How Image Lazyload & Dimension Injection Protect Both LCP and CLS#

On a typical WooCommerce shop category or long-form blog tutorial containing 45 images, only 2 to 4 images actually appear inside the visitor’s initial mobile screen (390 × 844 px). Downloading all 45 images eagerly on page load wastes 2 MB to 6 MB of cellular bandwidth and starves your critical hero image of network throughput.

At the same time, two classic mistakes in basic WordPress lazy-loading snippets damage Core Web Vitals:

  1. Accidentally Lazyloading the Hero Image: If your above-the-fold banner carries loading="lazy", Chrome waits until layout completes before even queuing the hero image download—adding 450 – 900 ms to mobile LCP!
  2. Leaving <img> Tags Without width & height: Theme templates, custom shortcodes, and page-builder HTML widgets frequently output <img src="/uploads/feature.png"> with no pixel dimensions. Before a lazy-loaded image downloads, the browser assumes its height is 0 px—and when the user scrolls down and the image arrives, the paragraph below jumps 400 px downward (CLS > 0.15)!

KadiCache Free Core (ImageLazyOptimizer.php, ImageContainerClassifier.php, and ImageDimensionResolver.php) solves both sides automatically inside WP Admin -> KadiCache -> Lazyload.

FIG 01Enabling Lazy-load Images, Add Missing Image Dimensions, CSS Backgrounds, and YouTube Preview inside WP Admin -> KadiCache -> LazyloadZoom 2x

Inside ImageContainerClassifier.php: Automatic Above-the-Fold Protection#

Before ImageLazyOptimizer.php adds lazy-loading to any <img> or <picture> element, ImageContainerClassifier.php inspects the tag and its surrounding HTML structure:

Image Context Detected in HTMLImageLazyOptimizer DecisionWhy It Matters
Header & Brand Logos (<header>, .custom-logo, .site-logo)Kept Eager (Immediate)Brand identity paints on the very first frame
Above-the-Fold Hero & First Product Gallery ImageKept Eager (Immediate)Avoids Lighthouse LCP image was lazily loaded penalty
Explicit Priority Attributes (fetchpriority="high", loading="eager")Kept Eager (Immediate)Honors WordPress core & Pro Local Improve LCP hints
Tracking Pixels & <noscript> Fallbacks (1×1 pixels, <noscript>)Left UntouchedPreserves analytics beacons and non-JS crawler markup
Below-the-Fold Content, Grid & Footer Imagesloading="lazy" + decoding="async"Defers download & offloads image decoding off the main thread

Inside ImageDimensionResolver.php: Fixing Unsized images Across Raster & SVG Files#

Enabling Add missing image dimensions (image_dimensions_enabled) scans <img> tags that are missing either width or height (while skipping tags that already define both attributes or inline aspect-ratio styles):

  • Local JPEG, PNG, WebP, GIF & AVIF Uploads: Reads lightweight image header metadata from local files inside wp-content/ (caching dimensions in memory/transients so disk I/O happens once, never on repeat hits) and injects exact integer width="..." and height="..." attributes.
  • Vector .svg Logos & Icons: Parses the SVG root <svg width="..." height="..." viewBox="0 0 W H"> coordinates cleanly so vector icons reserve their exact aspect ratio instead of collapsing to 0 × 0 or flashing full-width before CSS loads!
  • Responsive Fluid CSS Compatibility: Modern WordPress CSS (img { max-width: 100%; height: auto; }) uses the injected HTML width and height attributes purely to compute the intrinsic aspect ratio (width / height) prior to download—your responsive images still scale fluidly across every screen size!
FIG 02Adding custom image filenames, CSS classes, or route exclusions inside WP Admin -> KadiCache -> LazyloadZoom 2x

Configuring Image & Page Exclusions#

If a custom above-the-fold slider or JavaScript gallery plugin needs a specific image kept eager manually:

  1. Excluded Image Files (lazy_excluded_images): Expand the drawer in WP Admin -> KadiCache -> Lazyload and enter one filename (hero-slide-2.webp), CSS class (skip-lazy), or path fragment (/uploads/2026/11/featured-banner) per line.
  2. Excluded Lazyload Page Paths (lazy_excluded_paths): Bypass lazy-loading on specific routes (/print-catalog/*), or check Disable: Lazy load inside the Gutenberg sidebar (Per-Page Exclusions).
  3. HTML Attribute / Class Opt-Outs: KadiCache honors data-no-lazy, skip-lazy, and no-lazy classes natively in theme templates.

WooCommerce Product Grids, Variation Galleries & AJAX Filters#

High-catalog WooCommerce storefronts introduce three dynamic image workflows that break fragile JavaScript lazy-loaders—and why KadiCache relies on standards-compliant HTML attributes instead:

  1. AJAX Faceted Product Filters (FacetWP, Filter Everything, WooCommerce Product Filters): When a shopper filters a 48-product category grid by size or color over AJAX, JavaScript-only lazy-loaders frequently leave newly inserted <img data-src="..."> cards blank because no scroll event re-initialized their observer. Because ImageLazyOptimizer.php emits native browser <img src="..." loading="lazy" decoding="async" width="..." height="..."> markup without stripping src, AJAX-inserted product cards render immediately on every filter interaction!
  2. Single-Product Variation Swatch Switching: Clicking a color swatch on /product/merino-travel-hoodie/ swaps the primary product gallery src and srcset attributes cleanly while keeping the initial main product image eager for fast mobile LCP.
  3. RSS Feeds, REST API Responses & AMP / Print Views: ImageLazyOptimizer.php runs strictly on public HTML frontend documents—leaving /feed/ XML entries, /wp-json/ REST payloads, and admin previews untouched.

Frequently Asked Operational Questions

Will adding width and height attributes distort responsive images on mobile screens?
No. Every modern browser uses HTML width and height attributes together with CSS height: auto; max-width: 100% to compute the image’s aspect ratio before download, preventing layout shifts while scaling fluidly to mobile containers.
Does ImageDimensionResolver open image files on disk on every cached pageview?
Never. Dimensions are resolved once when generating the cached HTML snapshot (with internal dimension caching) and baked straight into the .html.gz file served by Early Delivery in ~14 ms.
Why does KadiCache use native browser loading="lazy" instead of a heavy jQuery lazyload library?
Native browser loading="lazy" runs inside the browser’s C++ rendering engine with 0 KB of client JavaScript, zero scroll-event listener lag, and native network-speed threshold adaptation.
Does Image Lazyload work with responsive <picture> tags and srcset attributes?
Yes! ImageLazyOptimizer handles standard <img> tags, responsive srcset/sizes attributes, and <picture><source> elements cleanly.
How do I stop one specific above-the-fold badge or slider image from being lazy-loaded?
Either add the class skip-lazy (or loading="eager") to the <img> tag in the editor, or paste its filename into Excluded Image Files inside WP Admin -> KadiCache -> Lazyload.
Gergis — Lead Systems & Cache Architect
Principal WordPress & Cloud Edge Architect
E-E-A-T Source Verified

Creator of KadiCache’s Early PHP Drop-In, 7-Driver Object Cache Engine, Cloud Remove Unused CSS, and Zero-CLS Lazy Render pipeline.

apps/wordpress-plugin/src/Optimization/ImageLazyOptimizer.phpapps/wordpress-plugin/src/Optimization/Images/ImageContainerClassifier.phpapps/wordpress-plugin/src/Optimization/Images/ImageDimensionResolver.php
Was this engineering guide helpful?