wp_editor_set_quality
云策文档标注
概述
wp_editor_set_quality 是一个 WordPress 过滤器,用于修改默认的图像压缩质量设置。它主要影响 WP_Image_Editor 类在初始化或手动调用 set_quality() 方法时的行为。
关键要点
- 过滤器应用于图像压缩质量,范围从 1(低)到 100(高)。
- 仅适用于编辑器初始化或 set_quality() 方法无 $quality 参数时调用的情况。
- WP_Image_Editor::set_quality() 方法优先级高于此过滤器。
- 对于 JPEG 图像(mime 类型为 'image/jpeg'),此过滤器会被 jpeg_quality 过滤器覆盖,因此可能无效。
- 从 WordPress 6.8.0 版本开始,添加了 size 参数,用于传递图像尺寸信息。
- 默认质量设置:WEBP 和 AVIF 图像为 90,但需注意用户贡献笔记中的更新信息。
代码示例
$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $dims ? $dims : $this->size );注意事项
- 使用此过滤器时,需考虑 mime_type 和 size 参数,以针对不同图像类型和尺寸调整质量。
- 注意版本变化:WordPress 6.8.0 引入了 size 参数,3.5.0 版本首次引入此过滤器。
- 用户贡献笔记指出,对于 JPEG 图像,应优先使用 jpeg_quality 过滤器。
原文内容
Filters the default image compression quality setting.
Description
Applies only during initial editor instantiation, or when set_quality() is run manually without the $quality argument.
The WP_Image_Editor::set_quality() method has priority over the filter.
Parameters
$qualityint-
Quality level between 1 (low) and 100 (high).
$mime_typestring-
Image mime type.
$sizearray-
Dimensions of the image.
widthintThe image width.heightintThe image height.
Source
$quality = apply_filters( 'wp_editor_set_quality', $default_quality, $mime_type, $dims ? $dims : $this->size );
Skip to note 5 content
Tom Auger
This sets the default compression quality used in WP_Image_Editor, the class used for manipulating images. Do note that the if the image is a JPEG image (mime type == ‘image/jpeg’) this filter is superceded by the jpeg_quality filter and effectively will not be used.
Skip to note 6 content
golden_g73
Default image quality is 90 for WEBP and AVIF.
https://github.com/WordPress/wordpress-develop/blob/6.7/src/wp-admin/includes/image-edit.php#L390
Skip to note 7 content
Lovro Hrust
Since there is no way to remove past comment – I have to correct this one, webp default quality is now set to 90, as commenters above had mention.
Skip to note 8 content
Lovro Hrust
Default quality for webp image (
image/webp) is 86.