Support

Account

Home Forums Feature Requests Image Field aspect ratio rule

Solving

Image Field aspect ratio rule

  • Hi,

    It’d be useful if you could add an aspect ratio rule to the image field, to require people to only upload images with a certain aspect ratio, like 1:1 or 16:9.

    Thanks!

  • There are filters in ACF that would let you do this.

    You can add a custom setting using the acf/render_field_settings hook, see an example of this here https://github.com/Hube2/acf-filters-and-functions/blob/master/default-image-for-image-field.php.

    There are filters in the attachment validation that you can then you to validate the image. Take a look at /api/api-helpers.php line 3839. The filters are called on line 3984.

    
    // filter for 3rd party customization
    $errors = apply_filters("acf/validate_attachment", $errors, $file, $attachment, $field);
    $errors = apply_filters("acf/validate_attachment/type={$field['type']}", $errors, $file, $attachment, $field );
    $errors = apply_filters("acf/validate_attachment/name={$field['name']}", $errors, $file, $attachment, $field );
    $errors = apply_filters("acf/validate_attachment/key={$field['key']}", $errors, $file, $attachment, $field );
    
  • Thanks! That’ll do.

    Would still be cool if this could be added to the backend though. Given that it already has min and max size rules.

  • Thanks, that’s really useful.

    I still maintain that it would be a good idea to add this validation to the plugin itself though, since it seems like a fairly common thing to require.

  • I can agree to a point, but I’m not the developer. Something like this might eventually make it in, but Elliot has several more important things on the to do list right now and waiting for it would not be a good idea. He’s also not very fond of adding things for what would be considered edge cases, which is why he’s supplied a plethora of hooks for those of us that fit into specific niches. While image aspect ratio may be important to some, it would simply confuse most of the clients I work for. It’s really easier to create a custom image size in WP that crops the images to the correct ratio and tell them that’s it’s going to be cropped. I for one never use full size images and generally make them impossible to use by clients simply because telling them they need to reduce image sizes even when they meet the 2M upload limit is just more than I can expect some of them to understand or be able to do. Telling them that images have to be a specific aspect ratio would likely get me a lot of blank stares as if I were speaking a language they don’t understand 😛

  • Well, we mostly use the custom fields internally, and the people that would be uploading new images would understand the words aspect ration. I already fixed this with your first answer, so that wasn’t really an issue anymore.

    Thanks for all the extra info though!

  • I figured you probably did, just wanted to make it easier for others that might be looking for a similar solution.

    I don’t do content. I build sites so that clients and other people that are not technically savvy can do their own content. I think that’s the main point of using a CMS.

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Image Field aspect ratio rule’ is closed to new replies.