{"id":46408,"date":"2025-08-16T08:42:11","date_gmt":"2025-08-16T08:42:11","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46408"},"modified":"2026-10-10T21:00:33","modified_gmt":"2026-10-10T21:00:33","slug":"image-compression","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/image-compression\/","title":{"rendered":"Set WordPress Image Compression Quality per Image Size"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">WordPress compresses every image size it creates at quality 82. On large hero images and full-width photos that compression can show as soft edges and banding, while on thumbnails nobody notices it. Setting everything to 100 fixes the large images but makes every thumbnail and medium size much heavier too.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet sets the WordPress image compression quality by image size: copies at least 1000 pixels wide are saved at quality 100, smaller ones keep the WordPress default.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Save large WordPress image sizes with less compression and keep small thumbnails light.\n *\/\n\n\/\/ ===== Config =====\nfunction dpc_image_compression_config(): array {\n\t$config = array(\n\t\t\/\/ Images at least this wide (in pixels) get 'large_quality'.\n\t\t'min_width'     =&gt; 1000,\n\t\t\/\/ Quality 1-100 for those images. WordPress uses 82 by default.\n\t\t'large_quality' =&gt; 100,\n\t\t\/\/ Formats this applies to.\n\t\t'formats'       =&gt; array( 'image\/jpeg', 'image\/webp' ),\n\t);\n\t\/\/ ===== End of config =====\n\n\treturn (array) apply_filters( 'dpc_image_compression_config', $config );\n}\n\nadd_filter( 'wp_editor_set_quality', 'dpc_image_compression_quality', 10, 3 );\n\nfunction dpc_image_compression_quality( $quality, $mime_type = '', $size = array() ) {\n\t$config = dpc_image_compression_config();\n\n\tif ( ! in_array( $mime_type, (array) $config&#91;'formats'&#93;, true ) ) {\n\t\treturn $quality;\n\t}\n\n\t$width = is_array( $size ) ? (int) ( $size&#91;'width'&#93; ?? 0 ) : 0;\n\tif ( $width &lt; (int) $config&#91;'min_width'&#93; ) {\n\t\treturn $quality;\n\t}\n\n\treturn max( 1, min( 100, (int) $config&#91;'large_quality'&#93; ) );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When WordPress resizes an upload, it asks the <code>wp_editor_set_quality<\/code> filter which quality to save that copy with. Since WordPress 6.8 the filter also gets the width and height of the copy.<\/li>\n\n\n<li>The snippet returns <code>large_quality<\/code> for JPEG and WebP copies that are at least <code>min_width<\/code> wide. Everything else gets the quality WordPress would use anyway.<\/li>\n\n\n<li>Values are limited to 1\u2013100.<\/li>\n\n\n<li>It works with both GD and Imagick, whichever your server uses.<\/li>\n\n\n<li>The older version of this post said the default is 90%. That was true before WordPress 4.5; it has been 82 since then.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor, edit the config and publish. Leave <strong>Action\/Hook<\/strong> empty.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Images are resized in wp-admin, in the REST API requests the block editor uploads through, and sometimes in cron. <strong>Everywhere<\/strong> covers all of them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A lighter setting: 90 is hard to tell apart from 100 and noticeably smaller.<\/li>\n\n\n<li>Other breakpoint: change <code>min_width<\/code>, for example to <code>1536<\/code> to raise only the <code>1536x1536<\/code>, <code>2048x2048<\/code> and scaled copies.<\/li>\n\n\n<li>Same quality for every size: set <code>min_width<\/code> to <code>0<\/code>, or use <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/remove-wordpress-default-image-compression\/\">Remove WordPress default image compression<\/a>, which sets one quality per format.<\/li>\n\n\n<li>AVIF: add <code>'image\/avif'<\/code> to <code>formats<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Only new uploads are affected. Regenerate thumbnails, for example with <code>wp media regenerate<\/code>, to apply it to existing images.<\/li>\n\n\n<li>The original file you upload is never recompressed. Uploads larger than 2560 pixels get a <code>-scaled<\/code> copy that WordPress uses as the main image; that copy is wide, so it gets <code>large_quality<\/code>.<\/li>\n\n\n<li>WordPress versions before 6.8 do not pass the size, so the snippet leaves the quality unchanged there.<\/li>\n\n\n<li>Image optimisation plugins and CDNs may compress the files again afterwards.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Save large WordPress image sizes at quality 100 and keep thumbnails at the default 82, with the wp_editor_set_quality filter.<\/p>\n","protected":false},"author":1,"featured_media":46884,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Set WordPress Image Compression Quality per Image Size","description":"Save large WordPress image sizes at quality 100 and keep thumbnails at the default 82, with the wp_editor_set_quality filter."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[119,59,117],"dpc_library":[204],"class_list":["post-46408","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-compression","tag-image","tag-img"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46408"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46408\/revisions"}],"predecessor-version":[{"id":46883,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46408\/revisions\/46883"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46884"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46408"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46408"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}