上传时区分特色图像和帖子图像

时间:2014-02-21 作者:Howdy_McGee

我想区分特色图片和帖子图片。

计划是为特色图片提供多种尺寸,但帖子内/连接到帖子的图片只需要默认尺寸。

我知道我可以用intermediate_image_sizes 要过滤将哪些自定义帖子类型分配给哪些图像大小,请执行以下操作:

/** Don\'t Make Extra Sizes **/
function post_type_sizes( $image_sizes ){
    $post_type_size_array = array(\'icon\', \'med-thumb\', \'large\', \'full\');
    $post_image_sizes = array(\'thumbnail\', \'medium\', \'large\', \'full\');

    if( isset($_REQUEST[\'post_id\']) && \'cpt_test\' === get_post_type($_REQUEST[\'post_id\']) )
        return $post_type_size_array;
    else
        return $post_image_sizes;

    return $image_sizes;
}
add_filter(\'intermediate_image_sizes\', \'post_type_sizes\', 999 ); 
但我只想给特色图片多个自定义大小,并保留上传到TinyMCE默认大小(缩略图、中、大、全)的任何帖子。

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

您可以通过过滤_thumbnail_id.

add_filter( \'update_post_metadata\', \'generate_featured_image_sizes\', 10, 5 );
add_filter( \'add_post_metadata\', \'generate_featured_image_sizes\', 10, 5 );

function generate_featured_image_sizes( $check, $object_id, $meta_key, $meta_value, $unique_or_prev ) {
    if ( $meta_key == \'_thumbnail_id\' ) {

        // regenerate previous featured image thumbs if it exists
        if ( false !== ( $prev_thumb_id = get_post_meta( $object_id, \'_thumbnail_id\', true ) ) ) {
            $prev_new_metadata = wp_generate_attachment_metadata( $prev_thumb_id, get_attached_file( $prev_thumb_id ) );
            if ( ! is_wp_error( $prev_new_metadata ) )
                wp_update_attachment_metadata( $prev_thumb_id, $prev_new_metadata );
        }

                    // remove all normal image sizes before we add our filter
                    cleanup_attachment_images( $meta_value );

        // filter image sizes for featured
        add_filter( \'intermediate_image_sizes\', \'featured_image_sizes\', 9999 );

        // regenerate thumbnails for featured
        $new_metadata = wp_generate_attachment_metadata( $meta_value, get_attached_file( $meta_value ) );
        if ( ! is_wp_error( $new_metadata ) )
            wp_update_attachment_metadata( $meta_value, $new_metadata );

                    // remove featured sizes from previous featured image
                    if ( isset( $prev_thumb_id ) && $prev_thumb_id )
                        cleanup_attachment_images( $prev_thumb_id );

        // tidy up
        remove_filter( \'intermediate_image_sizes\', \'featured_image_sizes\', 9999 );
    }

    return $check;
}

function featured_image_sizes( $sizes ) {
    return array( \'large\', \'featured\', \'slider\' );
}

function cleanup_attachment_images( $attachment_id ) {
    $uploadpath = wp_upload_dir();
    $intermediate_sizes = array();
    foreach ( get_intermediate_image_sizes() as $size ) {
        if ( $intermediate = image_get_intermediate_size( $attachment_id, $size ) )
            $intermediate_sizes[] = $intermediate;
    }
    // remove intermediate and backup images if there are any
    foreach ( $intermediate_sizes as $intermediate ) {
        /** This filter is documented in wp-admin/custom-header.php */
        $intermediate_file = apply_filters( \'wp_delete_file\', $intermediate[ \'path\' ] );
        @ unlink( path_join( $uploadpath[ \'basedir\' ], $intermediate_file ) );
    }
}
重新创建缩略图只是生成和更新附件元数据的一种情况,因此只要特征图像发生更改,就可以通过重新生成来获得所需的效果。

这将适用于上传,但也适用于更改特色图像。此外,它将为旧缩略图重新生成拇指,使其像一个正常大小的正常图像一样。

使用的原因add_post_metadataupdate_post_metadata 挂钩是为了在数据库中更新当前值之前,我们可以访问当前值。

NOTE为特色图像或编辑器添加媒体弹出窗口之间没有真正的区别,这些链接只是在不同的状态下打开弹出窗口,因此在上传图像时,没有简单的方法来判断请求的状态(特色图像或编辑器),因此我在上面介绍了这种方法。

UPDATE我添加了一个函数,您可以调用该函数来删除附件生成的缩略图集。您可以在生成新的附件元数据之前调用此函数。您甚至可以过滤它删除的图像大小或修改函数,以便可以将它们作为参数传递。

SO网友:Ravinder Kumar

你也可以试试这个

function post_type_sizes( $image_sizes ){

    $feature_image_sizes = array(\'thumbnail\');

    // checking for feature image uploading or not
    if( isset($_REQUEST[\'post_id\']) && \'post\' === get_post_type($_REQUEST[\'post_id\']) && get_post_meta( $_REQUEST[\'post_id\'], \'feature_image_uploading\', true ) ){
        update_post_meta( $_POST[\'post_id\'],\'feature_image_uploading\', false );
        return $feature_image_sizes;
    }

    return $image_sizes;
}
add_filter(\'intermediate_image_sizes\', \'post_type_sizes\', 999 ); 

add_action(\'admin_footer\', \'monksinc_notify_feature_image_event\', 9999 );
function monksinc_notify_feature_image_event(){

    global $pagenow, $post;

    if( \'post.php\' !== $pagenow && \'post\' !== get_post_type( $post->ID ) )
        return;

    ?>
    <script>
        (function(){
            jQuery(\'#set-post-thumbnail\').click(function(e){
                e.preventDefault();
                jQuery.post(
                    ajaxurl,
                    { action: \'set_post_thumbnail_notify\', post_id: jQuery(\'#post_ID\').val() },
                    function( response ){

                        console.log(response);
                    }
                );
            });
        })();
    </script>
    <?php
}

add_action( \'wp_ajax_set_post_thumbnail_notify\', \'set_post_thumbnail_notify_callback\' );
function set_post_thumbnail_notify_callback(){

    $result = array( \'status\'=> \'fail\'  );

    if( isset( $_POST ) && \'set_post_thumbnail_notify\' === $_POST[\'action\'] && \'\' !== $_POST[\'post_id\'] ){
        update_post_meta( $_POST[\'post_id\'],\'feature_image_uploading\', true );
        $result = array( \'status\'=> \'success\' );
    }

    echo json_encode($result);
    die();
}
在这个脚本中,我向功能图像链接添加了一个ajax调用,通知我用户正在上载功能图像,我在用户上载功能图像时设置一个post meta,并使用此post meta进行条件检查,以生成功能图像的特定大小

结束