How to Require a maximum and minimum Image Dimension for Uploading Woocommerce products?

Question

I am using the below code to restrict vendors from uploading images under specific dimensions. 533px X 533px but i want this happen only for woocommerce products not all images they uploads .. Right now i am using this code

add_action( 'admin_init', 'wpse_28359_block_authors_from_uploading_small_images' );

function wpse_28359_block_authors_from_uploading_small_images()
{
    if( !current_user_can( 'administrator') )
        add_filter( 'wp_handle_upload_prefilter', 'wpse_28359_block_small_images_upload' ); 
}

function wpse_28359_block_small_images_upload( $file )
{
    // Mime type with dimensions, check to exit earlier
    $mimes = array( 'image/jpeg', 'image/png', 'image/gif' );

    if( !in_array( $file['type'], $mimes ) )
        return $file;

    $img = getimagesize( $file['tmp_name'] );
    $minimum = array( 'width' => 533, 'height' => 533 );

    if ( $img[0] < $minimum['width'] )
        $file['error'] = 
            'images width is too low, The minimum is ' 
            . $minimum['width'] 
            . 'px.  ' 
            . $img[0] . 'px';

    elseif ( $img[1] < $minimum['height'] )
        $file['error'] = 
            'image height is not accepted , Please note we only accept height more than ' 
            . $minimum['height'] 
            . 'px.  ' 
            . $img[1] . 'px';

    return $file;
}

and i am asking if there anyway to make images uploaded for products not more than 1300px width and height and if it’s more than this , It should open the cropping tool auotmatic.

is that possible ?

0
, Bassem salah 2 years 2019-12-31T08:38:34-05:00 0 Answers 83 views 0

Leave an answer

Browse
Browse