{"id":10130,"date":"2020-06-19T16:07:20","date_gmt":"2020-06-19T16:07:20","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/http\/code.oxyset.com\/?p=228"},"modified":"2026-10-10T19:53:10","modified_gmt":"2026-10-10T19:53:10","slug":"remove-wordpress-default-image-compression","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/remove-wordpress-default-image-compression\/","title":{"rendered":"Remove WordPress default image compression"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Every time you upload a JPEG, WordPress saves the thumbnails and the other image sizes at quality 82, and since WordPress 5.3 a large upload is also scaled down and saved again at that quality. Photographers, portfolio sites and shops with detailed product photos often see that as soft edges and banding. This snippet removes the WordPress default image compression for the sizes it creates, or sets your own quality per format.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The older version of this post said the default is 90%. That was true before WordPress 4.5; it has been 82 since then.<\/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 * Change the quality WordPress uses when it saves resized images (default 82).\n *\/\n\n\/\/ ===== Config =====\nfunction dpc_image_quality_config(): array {\n\treturn array(\n\t\t\/\/ Quality 1-100 per format. Leave a format out to keep the WordPress default.\n\t\t'image\/jpeg' =&gt; 100,\n\t\t'image\/webp' =&gt; 100,\n\t\t\/\/ 'image\/avif' =&gt; 90,\n\t);\n}\n\/\/ ===== End of config =====\n\nadd_filter( 'wp_editor_set_quality', 'dpc_image_quality', 10, 2 );\n\nfunction dpc_image_quality( $quality, $mime_type = '' ) {\n\t$config = dpc_image_quality_config();\n\n\tif ( isset( $config&#91; $mime_type &#93; ) ) {\n\t\treturn max( 1, min( 100, (int) $config&#91; $mime_type &#93; ) );\n\t}\n\n\treturn $quality;\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>Every resized image goes through <code>WP_Image_Editor::set_quality()<\/code>, which asks the <code>wp_editor_set_quality<\/code> filter for the quality of that image&#8217;s MIME type. The snippet returns your value for the formats in the config.<\/li>\n\n\n<li>The older <code>jpeg_quality<\/code> filter from the original post only covered JPEGs. <code>wp_editor_set_quality<\/code> also handles WebP and AVIF, which WordPress can create since 5.8 and 6.5.<\/li>\n\n\n<li>Values are limited to 1\u2013100, and formats that are not in the config keep the WordPress default.<\/li>\n\n\n<li>It works the same with GD and Imagick, whichever the server uses.<\/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 balance between size and quality: 90 for JPEG is hard to tell apart from 100 and much smaller.<\/li>\n\n\n<li>PNG is lossless; its quality value only changes the compression level, so it is not in the config.<\/li>\n\n\n<li>Fewer copies of each upload: see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/disable-wordpress-default-image-sizes\/\">Disable WordPress default Image Sizes<\/a>.<\/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 the new quality to existing images.<\/li>\n\n\n<li>The original file you upload is never recompressed. Large uploads over 2560 px are scaled and saved as <code>-scaled<\/code> copies; that copy now uses your quality too. To keep the original as the main image, add <code>add_filter( 'big_image_size_threshold', '__return_false' );<\/code>.<\/li>\n\n\n<li>Quality 100 makes files much larger, often two to three times the size at 82. Check your page weight after the change.<\/li>\n\n\n<li>Image optimisation plugins and CDNs may recompress the files afterwards, which undoes this setting.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Stop WordPress compressing resized images at quality 82: set your own JPEG and WebP quality with the wp_editor_set_quality filter.<\/p>\n","protected":false},"author":1,"featured_media":46705,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Remove WordPress default image compression - Code DPlugins","description":"Stop WordPress compressing resized images at quality 82: set your own JPEG and WebP quality with the wp_editor_set_quality filter."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[],"dpc_library":[204],"class_list":["post-10130","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10130","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=10130"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10130\/revisions"}],"predecessor-version":[{"id":46704,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/10130\/revisions\/46704"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46705"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=10130"}],"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=10130"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=10130"},{"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=10130"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}