这个\'jpeg_quality\'
筛选器挂钩函数接受两个参数:$jpeg_quality
和$function
这是从过滤器挂钩中激发的函数,可以是image_resize
或wp_crop_image
. 因此,无法有选择地设置.jpeg
根据此过滤器挂钩函数中的图像大小生成图像。
但是,您仍然可以在上载附件的过程中挂接到稍后的操作挂钩,并调整.jpeg
上传图像的图像质量根据其具体大小来满足您的需要。首先设置jpeg_quality
最大限度地保持原始图像质量,然后added_post_meta
操作挂钩(在插入附件元数据`的末尾激发)来调整质量,如下所示:
// set the quality to maximum
add_filter(\'jpeg_quality\', create_function(\'$quality\', \'return 100;\'));
add_action(\'added_post_meta\', \'ad_update_jpeg_quality\', 10, 4);
function ad_update_jpeg_quality($meta_id, $attach_id, $meta_key, $attach_meta) {
if ($meta_key == \'_wp_attachment_metadata\') {
$post = get_post($attach_id);
if ($post->post_mime_type == \'image/jpeg\' && is_array($attach_meta[\'sizes\'])) {
$pathinfo = pathinfo($attach_meta[\'file\']);
$uploads = wp_upload_dir();
$dir = $uploads[\'basedir\'] . \'/\' . $pathinfo[\'dirname\'];
foreach ($attach_meta[\'sizes\'] as $size => $value) {
$image = $dir . \'/\' . $value[\'file\'];
$resource = imagecreatefromjpeg($image);
if ($size == \'spalsh\') {
// set the jpeg quality for \'spalsh\' size
imagejpeg($resource, $image, 100);
} elseif ($size == \'spalsh1\') {
// set the jpeg quality for the \'splash1\' size
imagejpeg($resource, $image, 30);
} else {
// set the jpeg quality for the rest of sizes
imagejpeg($resource, $image, 10);
}
// or you can skip a paticular image size
// and set the quality for the rest:
// if ($size == \'splash\') continue;
imagedestroy($resource);
}
}
}
}
以上代码只会影响新上传的图像。如果要更新以前上载的图像的质量,可以利用
register_activation_hook
插件的数量。在中创建新的php文件
wp-content/plugins
目录并命名(
update-jpeg-quality.php
例如),并向其中添加以下代码:
<?php
/*
Plugin Name: Update JPEG Quality
Plugin URI: http://wordpress.stackexchange.com/questions/74103/set-jpeg-compression-for-specific-custom-image-sizes
Description: This plugin will change the jpeg image quality according to its size.
Author: Ahmad M
Version: 1.0
Author URI: http://wordpress.stackexchange.com/users/12961/ahmad-m
*/
register_activation_hook(__FILE__, \'ad_modify_jpeg_quality\');
function ad_modify_jpeg_quality() {
$attachments = get_posts(array(
\'numberposts\' => -1,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image/jpeg\'
));
if (empty($attachments)) return;
$uploads = wp_upload_dir();
foreach ($attachments as $attachment) {
$attach_meta = wp_get_attachment_metadata($attachment->ID);
if (!is_array($attach_meta[\'sizes\'])) break;
$pathinfo = pathinfo($attach_meta[\'file\']);
$dir = $uploads[\'basedir\'] . \'/\' . $pathinfo[\'dirname\'];
foreach ($attach_meta[\'sizes\'] as $size => $value) {
$image = $dir . \'/\' . $value[\'file\'];
$resource = imagecreatefromjpeg($image);
if ($size == \'spalsh\') {
// set the jpeg quality for \'spalsh\' size
imagejpeg($resource, $image, 100);
} elseif ($size == \'spalsh1\') {
// set the jpeg quality for the \'splash1\' size
imagejpeg($resource, $image, 30);
} else {
// set the jpeg quality for the rest of sizes
imagejpeg($resource, $image, 10);
}
imagedestroy($resource);
}
}
}
?>
现在访问插件页面并点击
activate
的
Update JPEG Quality
插件。这将循环浏览您之前上传的所有内容
.jpeg
并根据插件中指定的值和条件调整其质量。然后您可以安全地停用和删除此插件
在应用于生产现场之前,请先在测试环境中进行测试。