仅适用于特色图像文件的新图像大小版本

时间:2012-07-03 作者:Diana

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

所有上传的文件将生成新的大小,I need a specific image size only for the file set as Fetured image, is there some way to do this?.

缩略图、中等大小等都可以,但无需为每个文件创建新的大小。

我认为这应该在设置了特色图像后立即起作用。

3 个回复
最合适的回答,由SO网友:brasofilo 整理而成

研究这个问题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, 让我们了解捕获“的代码”;“用作特色图像”;操作单击。

enter image description here

该代码在每个;“用作特色图像”;点击然后,它检查并删除所有额外的图像大小,保留默认的WordPress大小(缩略图、中、大)
测试此代码throughlyGPL代码。未授予保修。检查注释

/**
    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
}
上载图像后的结果文件夹内容,尚未设置特征图像enter image description here

设置后的文件夹内容fondo-restauraciones 作为特色图片
enter image description here

其他注意事项要处理所有额外的图像大小(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\'
    )
*/

SO网友:kaiser

您可以获得特色图片src/通过在核心函数上使用以下参数(其中$post, 应调用global $post 前面):

wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'single-post-thumbnail\' );
如何使用它我写了一个免费的插件available on GitHub, 称为“动态图像调整大小”。

您可以下载(&A);免费使用。

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

  • src 上载目录中图像的完整路径或IDwidth 整数值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的想法/提案

    旁注:

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

SO网友:Dvaeer

现在还有一个插件解决方案可以优雅地处理这个问题。它叫Fly Dynamic Image Resizer, 并在第一次调用图像时动态创建图像,而不是在上载图像时。

根据插件的描述:

有了这个插件,您可以创建任意大小的图像,而不会因为不必要的图像大小占用磁盘空间而感到内疚!

其工作方式如下:

您可以设置图像大小上传图像时,不会生成额外的大小在撰写本文时,该插件看起来得到了很好的支持。

结束

相关推荐

Host Images from Link

最近,我从Blogger导入了我的所有帖子,因此帖子中显示的所有图片目前都位于Blogger的服务器上。我想将所有这些图像导入/放入我的服务器,并自动更改每个图像的每个链接。因为有这么多的图片和这么多的帖子,我不能一个接一个地单独去。请指导我如何做到这一点,或者将我重定向到一个插件来完成这项工作。