WordPress.org

Support

Support » How-To and Troubleshooting » Set minimum height and width for featured image?

Set minimum height and width for featured image?

  • I want to make sure all authors upload a featured image that can actually be used as a featured image on the homepage without being stretched.

    I am looking for a method to restrict an original image that is smaller than 640×360 to be used as featured image.

    I have found a snippet of code that restricts the image height and width of ALL uploaded images (http://wordpress.stackexchange.com/questions/28359/minimum-image-size-for-uploading), but this will not work because I want authors to be able to add additional, smaller images to the post, if they like.

    I prefer a snippet of code to add to the functions.php, but I am open to suggestions for different methods/plugins/functions code, if anyone has one.

Viewing 10 replies - 1 through 10 (of 10 total)
  • esmi

    @esmi

    Forum Moderator

    Try:

    <?php if ( has_post_thumbnail() ) :
    $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id(), array( 640, 360 ) );
    if ( $image[1] >= 640 &&   $image[2] >= 360 ) echo get_the_post_thumbnail( $post->ID, array( 640, 360 ) );
    endif; ?>

    esmi, thanks for the reply.

    I am not sure this code works the way I want, however.

    I want to make sure authors add an image that is at least 640×360. If an author attaches an image as the “Featured Image” that is smaller, they will get an error and need to upload a new image.

    Anyone, please

    I can’t be the first person to need/want something like this?

    Ok, if this is not possible, does anyone have a suggestion for how to handle a situation when a user adds an image that is small or does nto add an image at all?

    I have also added this question on Stack Exchange. Hopefully between the two sites someone will have a suggestion.

    esmi

    @esmi

    Forum Moderator

    OK – the reason I never got back to you is that it seems you need to control the images at the upload point – which isn’t something I’ve played with before. So I don’t have an awful lot to contribute. Plus you want to use minimums whilst most people (and their code solutions) are trying to impose maximums.

    Sorry 🙁

    Ya, your reply is the norm I have been hearing.

    The thing though, if a user uploads an image that is too small, it just looks odd in my space reserved for a 640×360 image.

    The hunt continues…

    esmi

    @esmi

    Forum Moderator

    Is the layout dependant upon this image size? If not, what about outputting the CSS for the image block in the head of each page? That way you could possibly calculate the image dimensions & generate the necessary CSS dynamically.

    Not sure how I missed your reply. Yes, I want consistency throughout the posts, so all posts must have an image that is 640×360.

    GREAT NEWS!

    This task has been completed and I’m sure many WordPress multi-author websites will be ecstatic!

    The current method requires the use of the WyPiekacz plugin. In the settings, select “Post thumbnail (Featured image) is required:” then edit the wypiekacz.php file.

    Find:

    $has_thumbnail = false;
                if ( ( $post_id > 0 ) && function_exists( 'has_post_thumbnail' ) ) {
                    $has_thumbnail = has_post_thumbnail( $post_id );
                }
    
                $has_thumbnail = apply_filters( 'wypiekacz_check_thumbnail', $has_thumbnail, $post_id, $post_data );
    
                if ( !$has_thumbnail ) {
                    $this->errors[] = array( 'post_thumbnail', __('Post thumbnail (Featured image) is required.', 'wypiekacz') );
                }

    Change to:

    $has_thumbnail_proper_dimension = false;
            if ( ( $post_id > 0 ) && function_exists( 'has_post_thumbnail' ) ) {
                $has_thumbnail = has_post_thumbnail( $post_id );
                  list($url, $width, $height) = wp_get_attachment_image_src(get_post_thumbnail_id( $post->ID ), "Full");
                    echo $imgsrc[0];
                  if($width>=640 and $height>=360){
                      $has_thumbnail_proper_dimension=true;
                   }
            }
    
            $has_thumbnail = apply_filters( 'wypiekacz_check_thumbnail', $has_thumbnail, $post_id, $post_data );
    
            if ( !$has_thumbnail ) {
                $this->errors[] = array( 'post_thumbnail', __('Post thumbnail (Featured image) is required.', 'wypiekacz') );
            }
            if ( !$has_thumbnail_proper_dimension ) {
                $this->errors[] = array( 'post_thumbnail', __('Post thumbnail (Featured image) should be atleast 640x360.', 'wypiekacz') );
            }

    Change 640 and 360 above to fit your needs.

    I am currently working to add this in the settings of the plugin.

    I am not really a coder at all, so if someone has the time, I would love the help.

    THANKS – To Rajeev Vyas on wordpress.stackexchange.com for his help!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Set minimum height and width for featured image?’ is closed to new replies.