Skip Navigation

[Resolved] Set image dimension in the custom field to save space as it is uploaded to media

This thread is resolved. Here is a description of the problem and solution.

Problem:
Set image dimension in the custom field to save space as it is uploaded to media

Solution:
With Types 3.3 release we added the various usability improvements for custom field inputs with main focus on image fields. This includes better drag-and-drop functionality, uploading multiple files at once, and more. But there is no native way to validate the image but I can show you the way using which you can achieve this.

So, to validate the image while uploading, you need to use the hook: wp_handle_upload_prefilter

For example:

function func_validate_image_size( $file ) {
  $size = $file['size'];
  $size = $size / 1024;
  $type = $file['type'];
  $is_image = strpos( $type, 'image' ) !== false;
  $limit = 250;
  $limit_output = '250kb';
  if ( $is_image && $size > $limit ) {
    $file['error'] = 'Image files must be smaller than ' . $limit_output;
  }//end if
  return $file;
}//end nelio_max_image_size()
add_filter( 'wp_handle_upload_prefilter', 'func_validate_image_size' );

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/set-image-dimension-in-the-custom-field-to-save-space-as-it-is-uploaded-to-media/#post-1242980

Relevant Documentation:
https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_handle_upload_prefilter

This support ticket is created 5 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 2 replies, has 2 voices.

Last updated by acts-1224C 5 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#1242625

I am trying to: Membership and Directory site

In this era that all phone pictures are high definition, images could be as much as 15mb on a regular phone, so I needed to create some limitation for the uploads field.

I expected to see: I have created a page where subscribers can upload their headshot image for the directory in the company
I needed to limit the size of the image so my website does not become cumbersome. the field needed to have a limit like 1mb or file size to be no large than 2mb.
Instead, I got: high resolution picture of 15 mb for an application of a photo that needed to be only 150x150 pixels

#1242980

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - I would like to know that you want to validate the image on the backend (admin) while you try to upload image using custom field or featured image?

With Types 3.3 release we added the various usability improvements for custom field inputs with main focus on image fields. This includes better drag-and-drop functionality, uploading multiple files at once, and more. But there is no native way to validate the image but I can show you the way using which you can achieve this.

So, to validate the image while uploading, you need to use the hook: wp_handle_upload_prefilter

For example:

function func_validate_image_size( $file ) {
  $size = $file['size'];
  $size = $size / 1024;
  $type = $file['type'];
  $is_image = strpos( $type, 'image' ) !== false;
  $limit = 250;
  $limit_output = '250kb';
  if ( $is_image && $size > $limit ) {
    $file['error'] = 'Image files must be smaller than ' . $limit_output;
  }//end if
  return $file;
}//end nelio_max_image_size()
add_filter( 'wp_handle_upload_prefilter', 'func_validate_image_size' );

Where:
- Please feel free to adjust the code as required with your custom validation logic.

More info:
=> https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_handle_upload_prefilter

#1243875

My issue is resolved now. Thank you!