如何设置特定缩略图大小的jpg图像压缩?

时间:2014-01-05 作者:Jonathan

此问题是在上提出的Set JPEG compression for specific custom image sizes 艾哈迈德·M回答得很好

我当然会对那个帖子发表评论,但我没有50分的声誉。。。

我使用的代码与Ahmed的回答中的代码几乎相同,但我仍然面临的问题是,如果我在主题中使用特定的缩略图大小。例如,我在frontpage中有一个由较小方框组成的网格,它们有260px宽的图像。如果上传的图像最初小于Ahmed函数调整图像大小的值,那么它当然不会调整大小(也不应该),但也不会压缩图像。

Example:

如果我上传一张1200x800的图像并在头版中使用,它会被调整到更小的尺寸(520px宽,对于视网膜显示器来说是260px*2),质量也会被压缩。美好的但如果我上传的图像已经是500px宽(小于520px),它不会被调整大小,但也不会被压缩。很快,我的客户网站上就有很多大文件大小的图像,加载速度很慢。

我应该如何更改此函数以始终将图像压缩到我希望缩略图的任何质量,而不考虑其原始尺寸?

代码:

    //featured-image support
    add_theme_support( \'post-thumbnails\' ); 

    add_image_size( \'newsbox-thumb\', 520, 9999 ); // masonry news box-images =260px (520 retina) and unlimited height
    add_image_size( \'fprelease-thumb\', 112, 9999 ); // fprelese feed logo, 56px (112px retina)


    // https://wordpress.stackexchange.com/questions/74103/set-jpeg-compression-for-specific-custom-image-sizes
    // 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 == \'newsbox-thumb\') {
                    // set the jpeg quality for \'newsbox-thumb\' size
                    imagejpeg($resource, $image, 60);
                } elseif ($size == \'fprelease-thumb\') {
                    // set the jpeg quality for the \'fprelease-thumb\' size
                    imagejpeg($resource, $image, 85);
                } else {
                    // set the jpeg quality for the rest of sizes
                    imagejpeg($resource, $image, 80);
                }

                // or you can skip a paticular image size
                // and set the quality for the rest:
                // if ($size == \'splash\') continue;

                imagedestroy($resource);
            }
        }
    }
}

1 个回复
SO网友:vmassuchetto

我找到的一个解决方案是在$sizes 大堆这样,在所有转换为其他大小后,上载的图像也将转换为相同大小和其他给定质量的图像。

请注意,这样做会丢失原始图像,如果在首次上载图像后添加任何其他大小,则以下转换的质量将受此影响。

add_theme_support( \'post-thumbnails\' ); 
add_image_size( \'newsbox-thumb\', 520, 9999 ); // masonry news box-images =260px (520 retina) and unlimited height
add_image_size( \'fprelease-thumb\', 112, 9999 ); // fprelese feed logo, 56px (112px retina)


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\' )
        return false;

    if ( ! $post = get_post( $attach_id ) )
        return false;

    if ( \'image/jpeg\' != $post->post_mime_type )
        return false;

    $original = array(
        \'original\' => array(
            \'file\' => $attach_meta[\'file\'],
            \'width\' => $attach_meta[\'width\'],
            \'height\' => $attach_meta[\'height\']
        )
    );
    $sizes = !empty( $attach_meta[\'sizes\'] ) && is_array( $attach_meta[\'sizes\'] )
        ? $attach_meta[\'sizes\']
        : array();
    $sizes = array_merge( $sizes, $original );

    $pathinfo = pathinfo( $attach_meta[\'file\'] );
    $uploads = wp_upload_dir();
    $dir = $uploads[\'basedir\'] . \'/\' . $pathinfo[\'dirname\'];

    foreach ( $sizes as $size => $value ) {

        $image = \'original\' == $size
            ? $uploads[\'basedir\'] . \'/\' . $value[\'file\']
            : $dir . \'/\' . $value[\'file\'];
        $resource = imagecreatefromjpeg( $image );

        if ( $size == \'original\' )
            $q = 70; // quality for the original image
        elseif ( $size == \'newsbox-thumb\' )
            $q = 60;
        elseif ( $size == \'fprelease-thumb\' )
            $q = 85;
        else
            $q = 80;

        imagejpeg( $resource, $image, $q );
        imagedestroy( $resource );

    }

}
如果要重新运行转换以更改客户端网站中图像的质量,可以尝试Regenerate Thumbnails 插件。

结束

相关推荐

WP_Query in functions.php

我有一些代码要转换成函数。它工作得很好,直到我将其包装到所述函数中: $args = array( \'posts_per_page\' => -1, \'post_type\' => \'asset\', \'category_name\' => $cat ); $cat_query = new WP_Query( $args );