prashantcs053
7/3/2017 - 11:15 AM

plupload file upload hook for adding max_img_resolution validation 500x500 or 1200x600 (gravity form file upload)

plupload file upload hook for adding max_img_resolution validation 500x500 or 1200x600 (gravity form file upload)

/* plupload file upload hook for adding max_img_resolution */
add_filter('gform_plupload_settings_2', 'update_image_dimensions', 10, 3);
/* function for max_img_resolution value set */

function update_image_dimensions($plupload_init, $form_id, $this) {
    $specific_field_id = $plupload_init['multipart_params']['field_id'];
    if ($specific_field_id === 44) {
        $plupload_init['filters']['max_img_resolution'] = 250000;
    } else if ($specific_field_id === 45) {
        $plupload_init['filters']['max_img_resolution'] = 720000;
    }
    return $plupload_init;
}
if (typeof plupload != 'undefined') {
        /* File Upload image dimensions restrict code */
        plupload.addFileFilter('max_img_resolution', function (maxRes, file, cb) {
            var self = this, img = new o.Image(), message_location_id = self.settings.gf_vars.message_id, message;
            function finalize(result) {
                // cleanup
                img.destroy();
                img = null;

                // if rule has been violated in one way or another, trigger an error
                if (!result) {
                    if (message_location_id == 'gform_multifile_messages_2_44') {
                        message = 'Minimum image dimemsions should be <strong>500x500</strong>';
                    } else if (message_location_id == 'gform_multifile_messages_2_45') {
                        message = 'Minimum image dimemsions should be <strong>1200x600</strong>';
                    }
                    self.trigger('Error', {
                        code: "<p style='display:none;'>" + plupload.IMAGE_DIMENSIONS_ERROR,
                        message: "</p>" + message + "<p style='display:none;'>",
                        file: file + "</p>"
                    });
                    jQuery("#" + message_location_id + " li:empty").each(function () {
                        jQuery(this).remove();
                    });
                }
                cb(result);
            }
            img.onload = function () {
                // check if resolution cap is less than or equal to.

                if (message_location_id == 'gform_multifile_messages_2_44') {
                    if ((img.width === 500) && (img.height === 500)) {
                        finalize(img.width * img.height === maxRes);
                    } else {
                        finalize(false);
                    }
                } else if (message_location_id == 'gform_multifile_messages_2_45') {
                    if ((img.width === 1200) && (img.height === 600)) {
                        finalize(img.width * img.height === maxRes);
                    } else {
                        finalize(false);
                    }
                }

            };

            img.onerror = function () {
                finalize(false);
            };

            img.load(file.getSource());
        });
    }