Image Size Validation

wibadd 07 Apr, 2012
I'm trying to determine if it is possible to display an error using server side validation if an uploaded image does not meet minimum dimensions. I reviewed another post, but the discussion was more geared toward resizing.

I currently validate info on the form, then upload and resize an image before displaying a confirmation page. It appears the image must be uploaded before getimagesize() can be used to retrieve the dimensions. I created a quick form just to test this using the following code, but no error was presented:

<?php
$min_width = 900;
$min_height = 900;
list($width,$height)=getimagesize($form->data['image_path']);
if ($width < $min_width && $height < $min_height) {
$form->validation_errors['image'] = "The image you provided does not meet the minimum required dimensions.";
$valid = false;
}
?>

Also, since the image has to be uploaded before the size can be validated, would there be any issue with moving the upload image event before the CCSV event?
GreyHead 07 Apr, 2012
Hi wibadd,

The uploaded file info is in the $_FILES array before the Upload Files action runs so you could check it from there in a Custom Server-side validation action. The $_FILES array keys are described here

Bob
wibadd 07 Apr, 2012
So I tried changing it to the following:

list($width,$height)=getimagesize($_FILES['image']['name']);

but I receive a failed to open stream: No such file or directory error. If this is supposed to allow me to get the image information before upload, I didn't think I would specify a directory path because it isn't uploaded to the server yet. What am I missing?
GreyHead 28 Apr, 2012
Hi wibadd,

I just came across this post - sorry I missed it earlier. The file *is* uploaded to the server and is in a temporary folder. I'm pretty certain that you need to have the path there too and use the temporary name.

The ChronoForms debug info will give you more details about where the file is uploaded to.

Bob
wibadd 28 May, 2012
So I read some more on php.net and it appears the temporary name of the uploaded file is stored in $_FILES['uploadedfile']['tmp_name']. So I updated the code to be:
list($width,$height,$type,$attr)=getimagesize($_FILES['uploadedfile']['tmp_name']);
if ($width < 900 && $height < 900) {
 $form->validation_errors['logo'] = "The image you provided does not meet the minimum required dimensions.";
$valid = false;
}


I also have the event order On Submit as CCSV before the Upload Files, but I have also reversed them. They both result in an error being displayed along with the validation message:

Warning: getimagesize(): Filename cannot be empty in /usr/local/pem/vhosts/107845/webspace/httpsdocs/administrator/components/com_chronoforms/form_actions/custom_serverside_validation/custom_serverside_validation.php(18) : eval()'d code on line 34

When I check the custom_serverside_validation.php on line 18 is "$return = eval('?>'.$code);" and line 34 is the closing bracket for the function.

Data Array: 
Array
(
    [chronoform] => Sponsorship_Registration
    [event] => submit
    [cf_sid] => 41978720884f96f1fef96fee67f883e1
    [Itemid] => 620
    [option] => com_chronoforms
    [view] => form
    [legal_name] => Test
    [display_name] => Test
    [address] => 123 Main St
    [city] => Anywhere
    [state_province] => WI
    [other_state_province] => 
    [postal_code] => 12345
    [country] => USA
    [other_country] => 
    [first_name] => Tester
    [last_name] => One
    [full_name] => 
    [title_position] => 
    [email] => test@test.com
    [phone] => 5555551212
    [level] => Gold
    [website] => iibanew.org
    [logo] => Test.jpg
    [description] => This is just a test of the description.
    [cc_type] => Visa
    [cc_number] => 1234567891234567
    [cc_name] => Tester One
    [cc_exp_month] => 06
    [cc_exp_year] => 2012
    [cc_ccv] => 120
    [acceptance] => 1
    [input_hidden_57] => 
    [submit] => Submit
    [101ba8004a933532e56f1f254fc42962] => 1
    [logo_rename] => Test
    [logo_size] => 467
    [logo_url] => https://iibanew.org/media/media/images/sponsors/originals/Test.jpg
    [logo_path] => /usr/local/pem/vhosts/107845/webspace/httpsdocs/media/media/images/sponsors/originals/Test.jpg
    [_PLUGINS_] => Array
        (
            [upload_files] => Array
                (
                    [logo] => Array
                        (
                            [name] => Test.jpg
                            [path] => /usr/local/pem/vhosts/107845/webspace/httpsdocs/media/media/images/sponsors/originals/Test.jpg
                            [size] => 477853
                            [link] => https://iibanew.org/media/media/images/sponsors/originals/Test.jpg
                        )

                )

        )

)
 Validation Errors: 
Array
(
    [logo] => The image you provided does not meet the minimum required dimensions.
)

 Debug Data1.
Upload files1. File post array: Array ( [name] => IIBA NE Wisconsinv1-01.jpg [type] => image/jpeg [tmp_name] => /usr/local/pem/vhosts/107845/tmp/phpY1KS5u [error] => 0 [size] => 477853 ) 
2. Upload routine started for file upload by: logo 
3. Success: Test.jpg has been uploaded OK. 
4. File size is 467 kb 
5. File path is:
/usr/local/pem/vhosts/107845/webspace/httpsdocs/media/media/images/sponsors/originals/Test.jpg 
6. File url is:
https://iibanew.org/media/media/images/sponsors/originals/Test.jpg 


I'm still trying to figure out if the array name is uploadedfile or upload_files since the debug data seems to indicate it is the latter. I tried both and they still result in the same error. Also, I verified the dimensions of the .jpg I was uploading are 3055 x 574 pixels, so the dimensions should not be the issue.
GreyHead 29 May, 2012
Hi wibadd,

It may be that the file-type is missing from the temp_name value which is "/usr/local/pem/vhosts/107845/tmp/phpY1KS5u"

It may be easier to let ChronoForms complete the upload and rename and then check the dimensions - you can always delete the file if it doesn't meet the standard.

Bob
This topic is locked and no more replies can be posted.