• File size limit is working, but I need restrictions on the dimensions of submitted images.

    I tried this snippet that I found online — slightly modded by me to include max dimensions — but it isn’t working. It returns the correct randomized filename with $file['tmp_name'] in a string, but it doesn’t grab the dimensions (list($width, $height) = getimagesize( $file['tmp_name'] ); returns null), so no matter what size the image is, the result is the minimum width error on validation.

    add_filter( 'wpcf7_validate_file*', 'custom_cf7_file_validation_filter', 7, 2 );
    add_filter( 'wpcf7_validate_file', 'custom_cf7_file_validation_filter', 7, 2 );
    
    function custom_cf7_file_validation_filter( $result, $tag ) {
       if ( 'test_file' == $tag->name ) {
    	$name = $tag->name;
    	$file = isset( $_FILES[$name] ) ? $_FILES[$name] : null;
    	$minimum = array('width' => '100', 'height' => '50');
    	$maximum = array('width' => '400', 'height' => '150');
    	list($width, $height) = getimagesize( $file['tmp_name'] );
    	if ($width < $minimum['width']) {
                $result->invalidate( $tag, "Minimum image width is {$minimum['width']}px. Your image width is $width px");
            } elseif ($height < $minimum['height']) {
                $result->invalidate( $tag, "Minimum image height is {$minimum['height']}px. Your image height is $height px");
            } elseif ($width > $maximum['width']) {
                $result->invalidate( $tag, "Maximum width is {$maximum['width']}px. Your image width is $width px");
            } elseif ($height > $maximum['height']) {
                $result->invalidate( $tag, "Maximum height is {$maximum['height']}px. Your image height is $height px");
            } 
       }
       return $result;
    }

    Help?

Viewing 16 replies (of 16 total)
  • Thread Starter IT Hertz

    (@it-hertz)

    I understood and that’s what I did. It isn’t working as such, at least not with my form.

Viewing 16 replies (of 16 total)
  • The topic ‘image dimension restrictions’ is closed to new replies.