问题描述

我担心添加新的图像大小时创建的文件太多。

所有上传的文件都会生成新的大小,我需要一个特定的图像大小,只能将该文件设置为 Fetured 的图像,有没有办法呢?

缩略图,媒体等都可以,但是不需要为每个文件创建新的大小。

我认为这应该在设置精选图像后立即工作。

最佳解决方案

Research

问题 Filter For Featured Image 导致这个答案:How to hook update_post_meta and delete_post_meta?

加上一个很好的全局变量 ($_wp_additional_image_sizes),在这里显示:How to get a list of all the possible thumbnail sizes set within a theme,带给我们的代码,捕捉 「用作特色图像」 动作点击。


Code

这个代码在每个 「使用为特色图像」 点击时触发。然后通过所有其他图像大小并删除它们,保留默认的 WordPress(缩略图,中等,大) 。在进行 LIVE 之前,请仔细测试此代码。 GPL 代码。不保证。检查评论。

/**
    DRAWBACK
    - Setting the Featured Image can be done ONLY ONCE
    -- When first setting the FI, all other attachments intermediate sizes will be deleted
    -- If swapping the FI, the first image's remaining intermediate will be deleted, and the second DON'T HAVE any additional intermediates!

    TODO: Restoring deleted intermediates
    - this post may have an answer: https://wordpress.stackexchange.com/a/8082/12615
*/

add_action( 'added_post_meta', 'wpse_57369_selective_delete_intermediates', 10, 4 );
add_action( 'updated_post_meta', 'wpse_57369_selective_delete_intermediates', 10, 4 );

/**
 * Catches the "Used as featured image" action
*/
function wpse_57369_selective_delete_intermediates( $meta_id, $post_id, $meta_key, $meta_value )
{
    if ( '_thumbnail_id' == $meta_key )
    {
        global $_wp_additional_image_sizes;

       /**
        * The global holds all additional image sizes and contains this:
        *
           array(
           ['post-thumbnail'] => array(
               ['width'] => 1000
               ['height'] => 288
               ['crop'] => 1
           )
           ['large-feature'] => array(
               ['width'] => 1000
               ['height'] => 288
               ['crop'] => 1
           )
           ['small-feature'] => array(
               ['width'] => 500
               ['height'] => 300
               ['crop'] =>
           )
        )
       */

        // Populate a new array with single values based on the keys of the global
        $all_sizes = array();
        foreach ( $_wp_additional_image_sizes as $key => $value )
        {
            $all_sizes[] = $key;
        }

        // Retrieve all attachments of current post/page/cpt
        $all_attachs = get_children( array(
                'post_parent' => $post_id,
                'post_type' => 'attachment',
                'numberposts' => -1,
                'post_mime_type' => 'image'
            ));

        // Determine upload path
        $uploads   = wp_upload_dir();
        $path_pos  = strpos( $uploads['basedir'], 'wp-content/' ); // start position of the string
        $base_path = substr( $uploads['basedir'], 0, $path_pos);  // path before wp-content, e.g., /etc/public_html/

        // Loop through all attachments
        foreach ( $all_attachs as $key => $value )
        {
            // This is the featured image
            if ( $key == $meta_value)
            {
                wpse_57369_delete_files( $key, $all_sizes, $base_path, 'small-feature' );
            }
            // Rest of attached images
            else
            {
                wpse_57369_delete_files( $key, $all_sizes, $base_path, false );
            }
        }
    }
}

/**
 * Delete all custom intermediates files, except when $keep_size is defined
*/
function wpse_57369_delete_files( $ID, $all_sizes, $base_path, $keep_size=false )
{
    foreach ( $all_sizes as $intermediate )
    {
        /* We need to know the image url [0] and if it exists [3] */
        $the_url = wp_get_attachment_image_src( $ID, $intermediate );

        /* If additional image exist, go ahead */
        if( $the_url[3] )
        {
            // Path of the image to be deleted
            $url_pos  = strpos( $the_url[0], 'wp-content/' );
            $url_end  = substr( $the_url[0], $url_pos);

            // Delete all intermediates
            if ( !$keep_size )
            {
                // loga( $ID . ' - ' . $intermediate, 'delete-me');
                unlink( $base_path . $url_end );
            }

            // Featured image, Selective delete
            else
            {
                // Delete intermediate
                if ( $intermediate != $keep_size )
                {
                    // loga( $ID . ' - ' . $intermediate, 'delete-me');
                    unlink( $base_path . $url_end );
                }

                // Keep intermediate, no action needed
                // PROBABLY, RESTORING AN INEXISTENT IMAGE SIZE MUST BE DONE HERE
                else
                {
                    // loga( $ID . ' - ' . $intermediate, 'keep-me');
                }
            }
        }
    }
}

function loga()
{
    // This is the FireBug FirePHP console call
    // http://www.firephp.org/HQ/Use.html
}

Result

上传图像后的文件夹内容,没有设置功能图像

fondo-restauraciones 设置为精选图像后的文件夹内容


其他笔记

要处理所有额外的图像大小 (WordPress 默认值和自定义) 使用:

$all_sizes = get_intermediate_image_sizes();

/**
 * $all_images contains all intermediate image sizes, WordPress default and declared custom sizes:
 *
    array(
        [0] => 'thumbnail'
        [1] => 'medium'
        [2] => 'large'
        [3] => 'post-thumbnail'
        [4] => 'large-feature'
        [5] => 'small-feature'
    )
*/

次佳解决方案

您可以通过在核心功能 ($post,应该使用 global $post 在前面调用) 中使用以下参数来获取功能图像 src /source:

wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );

How-to 使用它

我写了一个免费的插件 available on GitHub,称为 「动态图像调整大小」 。

你可以下载& 免费使用它

短代码

[dynamic_image]放在您的内容中。短码有四个参数:

  • src 上传目录或 ID 中图像的完整路径

  • width 整数值

  • height 整数值

  • classes Css 类 – 由空格分隔

… 但也有一个:

模板标签

global $post;
// Use the template tag with ID *OR* full path
dynamic_image_resize( array(
     // The full path to the image in your uploads folder
     'src'     => wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
     // OR: the ID
     'src'     => get_post_thumbnail_id( $post->ID )

    ,'width'   => 60
    ,'height'  => 100
    ,'classes' => 'some classes to align and style the image'
) );

只需将其转储到您需要的模板中即可。

注意:它基于 Konstantin Kovshenin 的想法/建议。


边注:

如果要在默认情况下跳过/禁用默认图像大小,只需在管理设置中将 0 添加为 widthheight

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。