Last active
May 29, 2024 14:54
-
-
Save adczk/f4916c36188039efe6b1580061041189 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Forminator - image upload width/height limit | |
* Plugin URI: https://gist.github.com/adczk | |
* Description: Sets custom width/size image limit for upload fields | |
* Author: adczk | |
* | |
* Author URI: https://gist.github.com/adczk | |
* License: GPLv2 or later | |
* | |
* Use as MU plugin; | |
* | |
* Tested with Forminatro 1.30.2 | |
* | |
* NOTE: THIS IS ONLY BETA/DEV CODE, it is not fully tested and may not work in 100% cases | |
* | |
* USE ONLY WITH UPLOAD FIELDS SET TO "SINGLE" upload mode!!! | |
* | |
* Update May 29th 2024 - support for separate limits for different upload fileds | |
*/ | |
add_filter( 'forminator_custom_form_submit_errors', 'wpmudev_upload_image_dimensions_error', 99, 3 ); | |
function wpmudev_upload_image_dimensions_error( $submit_errors, $form_id, $field_data_array ) { | |
## CONFIG ## | |
$form_ids = array( 3010, 123 ); // form IDs - one or more; if more - separate numbers with commas | |
$fields_ids = array( 'upload-1', 'upload-2', 'upload-3' ); // IDs of fields to check, one or more, if more - separate with commas | |
// max image sizes per each upload field (note no comma after last upload field line | |
// if size not defined here but field defined in $fields_ids, a "fallback" value below | |
// will be used | |
$max_sizes = array( | |
'upload-1' => array( 'width'=>500, 'height'=>500 ), | |
'upload-2' => array( 'width'=>150, 'height' => 200 ) | |
); | |
// fallback sizes, if not defined per field above | |
$max_width = 20; // max image width in px - fallback if not defined per field | |
$max_height = 50; // max image height in px - fallback if not defined per field | |
$err_msg = 'Maximum image width/height: [wdt] x [hdt]!'; // custom error message for width/height; [wdt] and [hdt] will be replaced with max width/height accordingly. | |
$no_image_msg = 'Error fetching image sizes!'; // other error e.g. if it's not image file or doesn't exist due to upload issues | |
## END OF CONFIG; DO NOT EDIT BELOW ## | |
if ( !in_array( $form_id, $form_ids ) ) { | |
return $submit_errors; // just bail out and skip checks | |
} | |
// check form fields to find uploads | |
foreach( $field_data_array as $key => $value ) { | |
$field_name = $value['name']; | |
// if field is defined upload field, check it | |
if ( in_array( $field_name, $fields_ids ) ) { | |
$max_width_image = $max_width; | |
$max_height_image = $max_height; | |
// get field-specific limits if set | |
if ( array_key_exists( $field_name, $max_sizes ) ) { | |
$max_width_image = $max_sizes[$field_name]['width']; | |
$max_height_image = $max_sizes[$field_name]['height']; | |
} | |
// get temp path of the uploaded file | |
$temp_image = $value['value']['file']['tmp_name']; | |
if ( empty( $temp_image) ) { | |
$temp_image = $value['value']['file']['file_path']; | |
} | |
if ( file_exists( $temp_image ) ) { | |
// try to get size | |
$temp_image_size = wp_getimagesize( $temp_image ); | |
// if success, check width/height; | |
if ( is_array( $temp_image_size) ) { | |
$width = $temp_image_size[0]; | |
$height = $temp_image_size[1]; | |
// now compare against max | |
if ( ( $width > $max_width_image) || ( $height > $max_height_image ) ) { | |
// and if width or height bigger than max issue error | |
$submit_errors[][$field_name] = str_replace( '[hdt]', $max_height_image, str_replace( '[wdt]', $max_width_image, $err_msg ) );; | |
} | |
} else { | |
error_log('EXISTS - NO SIZE'); | |
// if could't get sizes | |
$submit_errors[][$field_name] = $no_image_msg; | |
} | |
} else { | |
// if temp upload file doesn't exist (e.g. due to upload error | |
$submit_errors[][$field_name] = $no_image_msg; | |
} | |
} | |
} | |
return $submit_errors; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment