skip to Main Content

so users can upload images on my website and I want to put some restrictions on these images. The catch is that these image restriction can only apply on one page, being the edit product page. I already tried checking by template and ID of the page but this page lives below a parent page so that did not work.
I currently have the code below but now it is rejecting all images and not displaying the correct message. Anyone that can help me out?

function resize_image_resolution($file) {
if (is_user_wcmp_vendor(get_current_user_id()) && isset($_SERVER['REQUEST_URI']) && false !== strpos( $_SERVER['REQUEST_URI'], 'edit-product' ) ) {
    $image = getimagesize($file['tmp_name']);
    $minimum = array(
        'width' => '1080', //set your minimum
        'height' => '1080'
    );
    $maximum = array(
        'width' => '12000', //set your maximum
        'height' => '12000'
    );
    $image_width = $image[0];
    $image_height = $image[1];

    $too_small = "Image dimensions are too small.";
    $too_large = "Image dimensions are too large.";

    if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
        $file['error'] = $too_small;
        return $file;
    } elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
        $file['error'] = $too_large;
        return $file;
    } else {
        return $file;
    }
}

}
add_filter(‘wp_handle_upload_prefilter’, ‘resize_image_resolution’);

2

Answers


  1. Chosen as BEST ANSWER

    Thanks everyone for helping me. Eventually I have been able to get it working with following code:

        /*Check image resolution*/
    /** Upload image restriction ****/
    function resize_image_resolution($file) {
        $current_url = $_SERVER['HTTP_REFERER'];
        if(is_user_wcmp_vendor(get_current_user_id()) && strpos($current_url, 'dashboard/edit-product') !== false){
            $image = getimagesize($file['tmp_name']);
                $minimum = array(
                    'width' => '1080',   //set your minimum
                    'height' => '1080'
                );
                $maximum = array(
                    'width' => '12000',  //set your maximum
                    'height' => '12000'
                );
                $image_width = $image[0];
                $image_height = $image[1];
    
                $too_small = "The minimum size of your design has to be 1080 pixels";
                $too_large = "The maximum size of your design has to be 12000 pixels";
    
                if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
                    $file['error'] = $too_small; 
                    return $file;
                }
                elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
                    $file['error'] = $too_large; 
                    return $file;
                }
                else
                    return $file;
        } else {
                return $file;
        }
    }
    add_filter('wp_handle_upload_prefilter', 'resize_image_resolution');
    

  2. See if this works for you. It would appear there are a few typos, and you should return $file at the end.

    function resize_image_resolution( $file ) {
        if ( is_user_wcmp_vendor( get_current_user_id() ) && isset( $_SERVER['REQUEST_URI'] ) && false !== strpos( $_SERVER['REQUEST_URI'], 'edit-product' ) ) {
            $image        = getimagesize( $file['tmp_name'] );
            $minimum      = array(
                'width'  => '1080', // set your minimum.
                'height' => '1080',
            );
            $maximum      = array(
                'width'  => '12000', // set your maximum.
                'height' => '12000',
            );
            $image_width  = $image[0];
            $image_height = $image[1];
    
            if ( $image_width <= $minimum['width'] || $image_height >= $minimum['height'] ) {
                $file['error'] = 'Image dimensions are too small.';
            } elseif ( $image_width > $maximum['width'] || $image_height > $maximum['height'] ) {
                $file['error'] = 'Image dimensions are too large.';
            }
        }
        return $file;
    }
    add_filter( 'wp_handle_upload_prefilter', 'resize_image_resolution' );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search