为特定的自定义图像大小设置JPEG压缩

时间:2012-11-27 作者:Amanda Duke

我使用各种自定义图像大小(按add_image_size) 我已使用此过滤器将JPEG压缩设置为30%:

function jpeg_quality_callback($arg) {
   return (int)30;
}
add_filter(\'jpeg_quality\', \'jpeg_quality_callback\');
如果我没有弄错的话,上面的代码会压缩all 我的自定义图像大小的30%。现在,对于我的两个自定义图像大小splash1splash2, 我想把压缩设置为80%。这怎么可能?

或者,从30%压缩过滤器中排除这些图像大小。

1 个回复
最合适的回答,由SO网友:Ahmad M 整理而成

这个\'jpeg_quality\' 筛选器挂钩函数接受两个参数:$jpeg_quality$function 这是从过滤器挂钩中激发的函数,可以是image_resizewp_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);
        }
    }
}
?>
现在访问插件页面并点击activateUpdate JPEG Quality 插件。这将循环浏览您之前上传的所有内容.jpeg 并根据插件中指定的值和条件调整其质量。然后您可以安全地停用和删除此插件在应用于生产现场之前,请先在测试环境中进行测试。

结束

相关推荐

Problem uploading images

多站点3。4.1 wordpress我用htaccess规则重定向了wp admin,并通过wp config重命名了wp内容和插件http://codex.wordpress.org/Editing_wp-config.php#Moving_wp-content现在我不能再上传图片了。当我尝试上载图像时,它会开始处理,但随后出现http错误,或者说您确定要这样做,但不允许我上载图像。this is in .htacess RewriteEngine On RewriteBase /