最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post thumbnails - How to set minimum required size for featured image?

programmeradmin0浏览0评论

I would like to set minimum required size for featured image. I want to prevent using images smaller than the featured image size, because otherwise some images can be displayed improperly.

For example, I set image size

add_image_size('article-retina', 758, 400, true);

but when I chose as featured image file smaller than this size, thumbnails with this size aren't generated so image will not be displayed properly.

So is this can be done simply, or it's more complicated?

I would like to set minimum required size for featured image. I want to prevent using images smaller than the featured image size, because otherwise some images can be displayed improperly.

For example, I set image size

add_image_size('article-retina', 758, 400, true);

but when I chose as featured image file smaller than this size, thumbnails with this size aren't generated so image will not be displayed properly.

So is this can be done simply, or it's more complicated?

Share Improve this question edited Sep 23, 2019 at 22:19 Damian asked Sep 23, 2019 at 21:51 DamianDamian 971 silver badge11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

The best solution I can come up with is a two-parter which adds metadata, then filters the images users are allowed to select from.

If I find a way to update the selector to add an alert, I'll add that as a separate answer.

1st: Add image metadata that will allow us to filter images later

add_filter('wp_generate_attachment_metadata', 'wpse_add_meta', 10, 2);
function wpse_add_meta($meta, $id){
    if (array_key_exists('height',$meta)){
        update_post_meta($id, 'height', (int) $meta['height']);
        update_post_meta($id, 'width', (int) $meta['width']);
    }
    return $meta;
}

2d: Filter images returned by the Featured Image selector, using the new meta values for Height and Width (added in filter above).

add_filter('ajax_query_attachments_args', 'e2_attachments_ajax' );
function e2_attachments_ajax($query){
    $minHeight = 400;
    $minWidth = 758;
    $query['meta_query'] = array(
        'relation' => 'AND',
        array(
            'key'     => 'height',
            'value'   => $minHeight,
            'type'    => 'numeric',
            'compare' => '>',
        ),
        array(
            'key'     => 'width',
            'value'   => $minWidth,
            'type'    => 'numeric',
            'compare' => '>',
        )
    );

    return $query;
}

NOTE: For existing sites, you will want to run a process to update existing uploaded images. Run this after the you have added the first filter, "wp_generate_attachment_metadata" so it will update the height and width attributes. I recommend something like Ajax Rebuild Thumbnails.

Add the following to your functions.php file. You can adjust the "height" and "width" dimensions as desired. This will reject uploaded images that fail to meet your minimum dimensions

add_filter('wp_handle_upload_prefilter','wpse_handle_upload_prefilter');
function wpse_handle_upload_prefilter($file)
{

    $image_types = array('png','jpg','jpeg','gif');
    $upload_type = explode('/',$file['type'])[1];
    $minimum = array('width' => '758', 'height' => '400');        

    // only check images...
    if (in_array($upload_type,$image_types)){

        $img = getimagesize($file['tmp_name']);
        $width = $img[0];
        $height = $img[1];

        if ($width < $minimum['width'] ){
            return array("error"=>"The uploaded image is too small. Minimum width is {$minimum['width']}px. The uploaded image width is {$width}px.");    
        } elseif ($height <  $minimum['height']) {
            return array("error"=>"The uploaded image is too small. Minimum height is {$minimum['height']}px. The uploaded image height is {$height}px.");
        } else {
            return $file; 
        } // end if width or height
    } // end if upload type is image type

} // end function
发布评论

评论列表(0)

  1. 暂无评论