Add file size limit label in image field

This commit is contained in:
champsupertramp
2016-01-30 23:21:54 +08:00
parent 722622bd53
commit 8062099f1e
2 changed files with 52 additions and 11 deletions
+15 -11
View File
@@ -290,12 +290,6 @@ class UM_Files {
$array['image'] = @getimagesize($file);
if ( empty( $_FILES['tmp_name'] ) ){
$array['invalid_image'] = true;
$array['error_type'] = 'too_large';
return $array;
}
if ( $array['image'] > 0 ) {
$array['invalid_image'] = false;
@@ -335,11 +329,7 @@ class UM_Files {
}
if ( $fileinfo['invalid_image'] == true ) {
if( isset( $fileinfo['error_type'] ) && $fileinfo['error_type'] == 'too_large' ){
$error = sprintf(__('The uploaded image was too large. You must upload a file smaller than %s','ultimatemember') , ini_get("upload_max_filesize") );
}else{
$error = sprintf(__('Your image is invalid or too large!','ultimatemember') );
}
$error = sprintf(__('Your image is invalid or too large!','ultimatemember') );
} elseif ( isset( $data['allowed_types'] ) && !$this->in_array( $fileinfo['extension'], $data['allowed_types'] ) ) {
$error = ( isset( $data['extension_error'] ) && !empty( $data['extension_error'] ) ) ? $data['extension_error'] : 'not allowed';
} elseif ( isset($data['min_size']) && ( $fileinfo['size'] < $data['min_size'] ) ) {
@@ -580,4 +570,18 @@ class UM_Files {
}
}
/***
*** @Format Bytes
****/
function format_bytes( $size , $precision = 1 ) {
$base = log($size, 1024);
$suffixes = array('', 'kb', 'MB', 'GB', 'TB');
$computed_size = round(pow(1024, $base - floor($base)), $precision);
$unit = $suffixes[ floor($base) ];
return $computed_size.' '.$unit;
}
}
+37
View File
@@ -59,3 +59,40 @@
return (strlen($title)!==strlen(utf8_decode($title))) ? $title : utf8_encode($title);
}
/***
*** @Add cover photo label of file size limit
***/
add_filter('um_predefined_fields_hook','um_change_profile_cover_photo_label',10,1);
function um_change_profile_cover_photo_label( $args ){
global $ultimatemember;
$max_size = $ultimatemember->files->format_bytes( $args['cover_photo']['max_size'] );
list( $file_size, $unit ) = explode(' ', $max_size );
if( $file_size >= 999999999 ){
}else{
$args['cover_photo']['upload_text'] .= '( '.__('maximum file size','ultimatemember').': '.$file_size.$unit.' )';
}
return $args;
}
/***
*** @Add profile photo label of file size limit
***/
add_filter('um_predefined_fields_hook','um_change_profile_photo_label',10,1);
function um_change_profile_photo_label( $args ){
global $ultimatemember;
$max_size = $ultimatemember->files->format_bytes( $args['profile_photo']['max_size'] );
list( $file_size, $unit ) = explode(' ', $max_size );
if( $file_size >= 999999999 ){
}else{
$args['profile_photo']['upload_text'] .= '( '.__('maximum file size','ultimatemember').': '.$file_size.$unit.' )';
}
return $args;
}