{"id":318,"date":"2026-02-12T06:37:59","date_gmt":"2026-02-12T06:37:59","guid":{"rendered":"https:\/\/sanny.test\/tutorials\/uncategorized\/image-optimization-2\/"},"modified":"2026-02-12T06:37:59","modified_gmt":"2026-02-12T06:37:59","slug":"image-optimization-2","status":"publish","type":"lesson","link":"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/","title":{"rendered":"Image Optimization"},"content":{"rendered":"<p>## Why Images Matter<\/p>\n<p>Images typically account for 50-75% of page weight. Optimizing them has the biggest performance impact.<\/p>\n<p>## Modern Image Formats<\/p>\n<p>| Format | Best For | Browser Support |<br \/>\n|&#8212;&#8212;&#8211;|&#8212;&#8212;&#8212;-|&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;|<br \/>\n| WebP | Photos, graphics | 97%+ |<br \/>\n| AVIF | Best compression | 85%+ |<br \/>\n| SVG | Icons, logos | 100% |<br \/>\n| PNG | Transparency | 100% |<br \/>\n| JPEG | Photos (fallback) | 100% |<\/p>\n<p>### WebP Conversion<\/p>\n<p>&#8220;`php<br \/>\n\/\/ Generate WebP on upload<br \/>\nadd_filter(&#8216;wp_generate_attachment_metadata&#8217;, function($metadata, $attachment_id) {<br \/>\n    $file = get_attached_file($attachment_id);<\/p>\n<p>    if (wp_image_editor_supports([&#8216;mime_type&#8217; => &#8216;image\/webp&#8217;])) {<br \/>\n        $editor = wp_get_image_editor($file);<br \/>\n        if (!is_wp_error($editor)) {<br \/>\n            $webp_path = preg_replace(&#8216;\/.(jpg|jpeg|png)$\/i&#8217;, &#8216;.webp&#8217;, $file);<br \/>\n            $editor->save($webp_path, &#8216;image\/webp&#8217;);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return $metadata;<br \/>\n}, 10, 2);<br \/>\n&#8220;`<\/p>\n<p>### Serving WebP with Fallback<\/p>\n<p>&#8220;`html\n<picture><source srcset=\"image.avif\" type=\"image\/avif\"><source srcset=\"image.webp\" type=\"image\/webp\"><img decoding=\"async\" src=\"image.jpg\" alt=\"Description\" width=\"800\" height=\"600\">\n<\/picture>\n&#8220;`<\/p>\n<p>## Lazy Loading<\/p>\n<p>Defer loading images until they&#8217;re needed:<\/p>\n<p>&#8220;`html<br \/>\n<!-- Native lazy loading (WordPress 5.5+) --><br \/>\n<img decoding=\"async\" src=\"image.jpg\" loading=\"lazy\" alt=\"\"><\/p>\n<p><!-- Disable for above-fold images --><br \/>\n<img decoding=\"async\" src=\"hero.jpg\" loading=\"eager\" fetchpriority=\"high\" alt=\"\"><br \/>\n&#8220;`<\/p>\n<p>### WordPress Lazy Loading<\/p>\n<p>&#8220;`php<br \/>\n\/\/ WordPress adds loading=&#8221;lazy&#8221; by default<br \/>\n\/\/ Disable for specific images<br \/>\nadd_filter(&#8216;wp_img_tag_add_loading_attr&#8217;, function($value, $image, $context) {<br \/>\n    \/\/ Don&#8217;t lazy load featured images<br \/>\n    if ($context === &#8216;the_content&#8217; &#038;&#038; strpos($image, &#8216;wp-post-image&#8217;) !== false) {<br \/>\n        return false;<br \/>\n    }<br \/>\n    return $value;<br \/>\n}, 10, 3);<\/p>\n<p>\/\/ Or use fetchpriority for LCP images<br \/>\nadd_filter(&#8216;wp_get_attachment_image_attributes&#8217;, function($attr, $attachment) {<br \/>\n    if (is_singular() &#038;&#038; has_post_thumbnail() &#038;&#038; get_post_thumbnail_id() == $attachment->ID) {<br \/>\n        $attr[&#8216;fetchpriority&#8217;] = &#8216;high&#8217;;<br \/>\n        $attr[&#8216;loading&#8217;] = &#8216;eager&#8217;;<br \/>\n    }<br \/>\n    return $attr;<br \/>\n}, 10, 2);<br \/>\n&#8220;`<\/p>\n<p>## Responsive Images<\/p>\n<p>Serve appropriate sizes for each device:<\/p>\n<p>&#8220;`html<br \/>\n<!-- Responsive with srcset --><br \/>\n<img decoding=\"async\" \n    src=\"image-800.jpg\"\n    srcset=\"image-400.jpg 400w,\n            image-800.jpg 800w,\n            image-1200.jpg 1200w,\n            image-1600.jpg 1600w\"\n    sizes=\"(max-width: 600px) 100vw,\n           (max-width: 1200px) 50vw,\n           800px\"\n    alt=\"Description\"\n><br \/>\n&#8220;`<\/p>\n<p>### WordPress Responsive Images<\/p>\n<p>WordPress generates srcset automatically. Customize sizes:<\/p>\n<p>&#8220;`php<br \/>\n\/\/ Add custom image sizes<br \/>\nadd_image_size(&#8216;card-thumbnail&#8217;, 400, 300, true);<br \/>\nadd_image_size(&#8216;hero-large&#8217;, 1600, 900, true);<\/p>\n<p>\/\/ Customize srcset sizes<br \/>\nadd_filter(&#8216;wp_calculate_image_sizes&#8217;, function($sizes, $size, $image_src, $image_meta) {<br \/>\n    \/\/ For full-width images<br \/>\n    if ($size[0] >= 1200) {<br \/>\n        return &#8216;100vw&#8217;;<br \/>\n    }<br \/>\n    return $sizes;<br \/>\n}, 10, 4);<br \/>\n&#8220;`<\/p>\n<p>## Image Compression<\/p>\n<p>### Recommended Tools<\/p>\n<p>| Tool | Type | Compression |<br \/>\n|&#8212;&#8212;|&#8212;&#8212;|&#8212;&#8212;&#8212;&#8212;-|<br \/>\n| ShortPixel | Plugin | Lossy\/Lossless |<br \/>\n| Imagify | Plugin | Lossy\/Lossless |<br \/>\n| Smush | Plugin | Lossless |<br \/>\n| Squoosh | Web app | Manual |<br \/>\n| ImageOptim | Mac app | Lossless |<\/p>\n<p>### Command Line Compression<\/p>\n<p>&#8220;`bash<br \/>\n# WebP conversion with cwebp<br \/>\ncwebp -q 80 input.jpg -o output.webp<\/p>\n<p># Batch conversion<br \/>\nfor file in *.jpg; do<br \/>\n    cwebp -q 80 &#8220;$file&#8221; -o &#8220;${file%.jpg}.webp&#8221;<br \/>\ndone<\/p>\n<p># AVIF conversion<br \/>\navifenc &#8211;speed 6 &#8211;quality 70 input.jpg output.avif<\/p>\n<p># Optimize existing JPEGs<br \/>\njpegoptim &#8211;strip-all &#8211;max=85 *.jpg<\/p>\n<p># Optimize PNGs<br \/>\noptipng -o5 *.png<br \/>\n&#8220;`<\/p>\n<p>## SVG Optimization<\/p>\n<p>&#8220;`bash<br \/>\n# Install SVGO<br \/>\nnpm install -g svgo<\/p>\n<p># Optimize SVG<br \/>\nsvgo input.svg -o output.svg<\/p>\n<p># Batch optimize<br \/>\nsvgo -f .\/icons\/ -o .\/icons-optimized\/<br \/>\n&#8220;`<\/p>\n<p>### Safe SVG Upload<\/p>\n<p>&#8220;`php<br \/>\n\/\/ Allow SVG uploads (with sanitization)<br \/>\nadd_filter(&#8216;upload_mimes&#8217;, function($mimes) {<br \/>\n    $mimes[&#8216;svg&#8217;] = &#8216;image\/svg+xml&#8217;;<br \/>\n    return $mimes;<br \/>\n});<\/p>\n<p>\/\/ Sanitize SVG content<br \/>\nadd_filter(&#8216;wp_handle_upload_prefilter&#8217;, function($file) {<br \/>\n    if ($file[&#8216;type&#8217;] === &#8216;image\/svg+xml&#8217;) {<br \/>\n        \/\/ Use a library like SVG Sanitizer<br \/>\n        $sanitizer = new enshrinedsvgSanitizeSanitizer();<br \/>\n        $content = file_get_contents($file[&#8216;tmp_name&#8217;]);<br \/>\n        $clean = $sanitizer->sanitize($content);<br \/>\n        file_put_contents($file[&#8216;tmp_name&#8217;], $clean);<br \/>\n    }<br \/>\n    return $file;<br \/>\n});<br \/>\n&#8220;`<\/p>\n<p>## Preventing Layout Shift<\/p>\n<p>&#8220;`css<br \/>\n\/* Aspect ratio boxes *\/<br \/>\n.image-container {<br \/>\n    aspect-ratio: 16 \/ 9;<br \/>\n    overflow: hidden;<br \/>\n}<\/p>\n<p>.image-container img {<br \/>\n    width: 100%;<br \/>\n    height: 100%;<br \/>\n    object-fit: cover;<br \/>\n}<\/p>\n<p>\/* Progressive loading placeholder *\/<br \/>\n.image-placeholder {<br \/>\n    background: linear-gradient(135deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);<br \/>\n    background-size: 200% 100%;<br \/>\n    animation: shimmer 1.5s infinite;<br \/>\n}<\/p>\n<p>@keyframes shimmer {<br \/>\n    0% { background-position: 200% 0; }<br \/>\n    100% { background-position: -200% 0; }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>## CDN Image Optimization<\/p>\n<p>Many CDNs offer automatic image optimization:<\/p>\n<p>&#8220;`php<br \/>\n\/\/ Cloudflare Polish (automatic)<br \/>\n\/\/ BunnyCDN Image Processing<br \/>\n\/\/ Imgix\/Cloudinary URLs<\/p>\n<p>\/\/ Example: Dynamic resizing via CDN<br \/>\nfunction cdn_image_url($url, $width, $height = null) {<br \/>\n    $base = &#8216;https:\/\/cdn.example.com\/&#8217;;<br \/>\n    $params = &#8220;w={$width}&#8221;;<br \/>\n    if ($height) {<br \/>\n        $params .= &#8220;&#038;h={$height}&#038;fit=crop&#8221;;<br \/>\n    }<br \/>\n    return &#8220;{$base}{$url}?{$params}&#038;format=auto&#038;quality=80&#8221;;<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>## Next Steps<\/p>\n<p>In the next lesson, we&#8217;ll optimize database queries and clean up bloat.<\/p>\n<div class=\"callout callout-success\">\n<strong>\ud83c\udfaf Lesson Complete!<\/strong> You can now implement comprehensive image optimization.\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Optimize images with modern formats, lazy loading, responsive images, and compression.<\/p>\n","protected":false},"featured_media":0,"template":"","meta":[],"tutorial_series":[],"lesson_level":[],"class_list":["post-318","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Image Optimization - Sanny Srivastava<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Image Optimization - Sanny Srivastava\" \/>\n<meta property=\"og:description\" content=\"Optimize images with modern formats, lazy loading, responsive images, and compression.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/\" \/>\n<meta property=\"og:site_name\" content=\"Sanny Srivastava\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/\",\"url\":\"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/\",\"name\":\"Image Optimization - Sanny Srivastava\",\"isPartOf\":{\"@id\":\"https:\/\/sanny.dev\/#website\"},\"datePublished\":\"2026-02-12T06:37:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/sanny.dev\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Image Optimization\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/sanny.dev\/#website\",\"url\":\"https:\/\/sanny.dev\/\",\"name\":\"Sanny Srivastava\",\"description\":\"WordPress Full Stack Developer\",\"publisher\":{\"@id\":\"https:\/\/sanny.dev\/#\/schema\/person\/d6ccd6d5b8e689dd0c0e6c52db9de766\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/sanny.dev\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/sanny.dev\/#\/schema\/person\/d6ccd6d5b8e689dd0c0e6c52db9de766\",\"name\":\"raazsrivastava@gmail.com\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/sanny.dev\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/sanny.dev\/wp-content\/uploads\/2023\/12\/pic.jpeg\",\"contentUrl\":\"https:\/\/sanny.dev\/wp-content\/uploads\/2023\/12\/pic.jpeg\",\"width\":380,\"height\":380,\"caption\":\"raazsrivastava@gmail.com\"},\"logo\":{\"@id\":\"https:\/\/sanny.dev\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/sanny.dev\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Image Optimization - Sanny Srivastava","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/","og_locale":"en_US","og_type":"article","og_title":"Image Optimization - Sanny Srivastava","og_description":"Optimize images with modern formats, lazy loading, responsive images, and compression.","og_url":"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/","og_site_name":"Sanny Srivastava","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/","url":"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/","name":"Image Optimization - Sanny Srivastava","isPartOf":{"@id":"https:\/\/sanny.dev\/#website"},"datePublished":"2026-02-12T06:37:59+00:00","breadcrumb":{"@id":"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/sanny.dev\/tutorials\/wordpress-performance-optimization\/image-optimization-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/sanny.dev\/"},{"@type":"ListItem","position":2,"name":"Image Optimization"}]},{"@type":"WebSite","@id":"https:\/\/sanny.dev\/#website","url":"https:\/\/sanny.dev\/","name":"Sanny Srivastava","description":"WordPress Full Stack Developer","publisher":{"@id":"https:\/\/sanny.dev\/#\/schema\/person\/d6ccd6d5b8e689dd0c0e6c52db9de766"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/sanny.dev\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/sanny.dev\/#\/schema\/person\/d6ccd6d5b8e689dd0c0e6c52db9de766","name":"raazsrivastava@gmail.com","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/sanny.dev\/#\/schema\/person\/image\/","url":"https:\/\/sanny.dev\/wp-content\/uploads\/2023\/12\/pic.jpeg","contentUrl":"https:\/\/sanny.dev\/wp-content\/uploads\/2023\/12\/pic.jpeg","width":380,"height":380,"caption":"raazsrivastava@gmail.com"},"logo":{"@id":"https:\/\/sanny.dev\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/sanny.dev"]}]}},"_links":{"self":[{"href":"https:\/\/sanny.dev\/wp-json\/wp\/v2\/lesson\/318","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sanny.dev\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/sanny.dev\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/sanny.dev\/wp-json\/wp\/v2\/media?parent=318"}],"wp:term":[{"taxonomy":"tutorial_series","embeddable":true,"href":"https:\/\/sanny.dev\/wp-json\/wp\/v2\/tutorial_series?post=318"},{"taxonomy":"lesson_level","embeddable":true,"href":"https:\/\/sanny.dev\/wp-json\/wp\/v2\/lesson_level?post=318"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}