很多时候用WordPress来建图片展示类的网站的时候,对于生成的缩略图要求100%的无损的质量,但是默认WordPress并不是按照100%来生成缩略图的,而是90%的压缩格式,这样对于精度要求较高的用户来说显然并不满意,那么如何做到无损压缩生成原精度的缩略图呢?请将以下代码加入WordPress模版文件的functions.php里边:
add_filter( 'jpeg_quality', 'tgm_image_full_quality' );
add_filter( 'wp_editor_set_quality', 'tgm_image_full_quality' );
/**
* Filters the image quality for thumbnails to be at the highest ratio possible.
*
* Supports the new 'wp_editor_set_quality' filter added in WP 3.5.
*
* @since 1.0.0
*
* @param int $quality The default quality (90)
* @return int $quality Amended quality (100)
*/
function tgm_image_full_quality( $quality ) {
return 100;
}
当然以上这种方法仅仅针对插入代码以后上传的图片质量,如果需要对已存在的缩略图的质量进行更改,则需要用到类似 Regenerate Thumbnails 这种插件,该插件可以批量重新按照新的质量比生成新的缩略图。