2

What I am trying to do is pretty special I think. I am using WC VENDORS PRO plugin. The sellers have 4 upload areas via the front end. They can upload a profile image, a store banner, and product images. There is also a place to upload a zip file.

What I need is for the images that are being uploaded to be a required size for each area. For example: The store banner needs to be 800x100. The product images must be 800x450, and the profile image must be 200x200.

What I want to happen is when they try to upload a size that is NOT what I specify, it will not allow them to upload at all and give an error. I was able to get close with this script (attached ) but it effects ALL the areas of the upload including via front end including the zip file.

I know the actual site will display these images via css but this is not what I want. There is too much cropping, warping, off centers, etc when doing this and things on a site with 1000's of uploaders, turns into a mess and looks awful.

This code ALMOST works. It does what it is supposed to do, but it effects ALL areas of uploads, including the zip file. So it errors out on the zip file because it does not have a "dimension" and where I want a 200x200 image its forces it to be 800x450. If anyone has any idea how I can solve this, please let me know. I am willing to pay someone who can do it, I just have not found the someone yet.

    // check for file upload size //
    {
    if( !current_user_can( 'administrator') )
    add_filter( 'wp_handle_upload_prefilter', 'mdu_validate_image_size' ); 
    }
    add_filter('wp_handle_upload_prefilter','mdu_validate_image_size');
    function mdu_validate_image_size( $file ) {
    $image = getimagesize($file['tmp_name']);
    $minimum = array(
    'width' => '800',
    'height' => '450'
    );
    $maximum = array(
    'width' => '800',
    'height' => '450'
    );
    $image_width = $image[0];
    $image_height = $image[1];

    $too_small = "Image dimensions are too small. Minimum size is                {$minimum['width']} by {$minimum['height']} pixels. Uploaded image is   $image_width by $image_height pixels.";
    $too_large = "Image dimensions are too large. Maximum size is   {$maximum['width']} by {$maximum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";

    if ( $image_width < $minimum['width'] || $image_height <   $minimum['height'] ) {
    // add in the field 'error' of the $file array the message 
    $file['error'] = $too_small; 
    return $file;
    }
    elseif ( $image_width > $maximum['width'] || $image_height >  $maximum['height'] ) {
    //add in the field 'error' of the $file array the message
    $file['error'] = $too_large; 
    return $file;
    }
    else
    return $file;
    }
  • Not sure if this is a big help, but the ACF Plugin (advancedcustomfields.com) allows this in its image fields. A quick look through its code might be of some help. – RaajTram May 25 '16 at 2:03
1

Haven't tested it, but this should work:

// check for file upload size //
{
if( !current_user_can( 'administrator') )
add_filter( 'wp_handle_upload_prefilter', 'mdu_validate_image_size' ); 
}
add_filter('wp_handle_upload_prefilter','mdu_validate_image_size');
function mdu_validate_image_size( $file ) {
    if ( mime_content_type($file) == 'application/zip' ) {
        mdu_validate_zip_image_size($file);
        return $file;
    }
$image = getimagesize($file['tmp_name']);
$minimum = array(
'width' => '800',
'height' => '450'
);
$maximum = array(
'width' => '800',
'height' => '450'
);
$image_width = $image[0];
$image_height = $image[1];

$too_small = "Image dimensions are too small. Minimum size is {$minimum['width']} by {$minimum['height']} pixels. Uploaded image is   $image_width by $image_height pixels.";
$too_large = "Image dimensions are too large. Maximum size is {$maximum['width']} by {$maximum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";

if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
// add in the field 'error' of the $file array the message 
$file['error'] = $too_small; 
return $file;
}
elseif ( $image_width > $maximum['width'] || $image_height >  $maximum['height'] ) {
//add in the field 'error' of the $file array the message
$file['error'] = $too_large; 
return $file;
}
else
return $file;
}

function mdu_validate_zip_image_size($file) {
    $zip = new ZipArchive();
    if (true !== $zip->open($file))
    {
        $file['error'] = 'Could not open ZIP archive';
    }

    // Search for the image file.
    for($i = 0; $i < $zip->numFiles; $i++)
    {
        $entry = $zip->statIndex($i);
        $ext = substr($entry['name'], -3);
        if (in_array($ext, array('jpg', 'png'))
        {
            $filename = $entry['name'];
        }
    }

    if (isset($filename) && ($image = $zip->getFromName($filename)))
    {
        list($image_width, $image_height) = getimagesize($image);
    }
    else
    {
        $file['error'] = 'No image found';
    }

    $minimum = array(
    'width' => '800',
    'height' => '450'
    );
    $maximum = array(
    'width' => '800',
    'height' => '450'
    );

    $too_small = "Image dimensions are too small. Minimum size is {$minimum['width']} by {$minimum['height']} pixels. Uploaded image is   $image_width by $image_height pixels.";
    $too_large = "Image dimensions are too large. Maximum size is {$maximum['width']} by {$maximum['height']} pixels. Uploaded image is $image_width by $image_height pixels.";

    if ( $image_width < $minimum['width'] || $image_height < $minimum['height'] ) {
        // add in the field 'error' of the $file array the message 
        $file['error'] = $too_small; 
        return $file;
    }
    elseif ( $image_width > $maximum['width'] || $image_height >  $maximum['height'] ) {
        //add in the field 'error' of the $file array the message
        $file['error'] = $too_large; 
        return $file;
    }
    else
        return $file;
    }

}
  • Thank you for helping. I opened your script up in dreamweaver and it shows errors. I went ahead and added it to my functions.php and it did error out. so there is some sort of syntax problem with this code. – Ryan May 26 '16 at 3:22

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.