1

Craft will let us define what file-type is valid for a given asset field, but there are many times where I'd like to enforce other properties too - e.g., a minimum dimension for valid images uploaded to that field (i.e., dimensions must be larger than 800x600). Is there a way to do that?

Currently I can only see Craft enforcing file-types and file-size limits if they're too large (e.g., 4Mb is too big). I'd like some more options to avoid clients uploading unsuitable files.

Matt Wilcox
  • 3,199
  • 1
  • 14
  • 28
  • That's a similar issue though not quite the same, and was not really answered :/ – Matt Wilcox Oct 07 '14 at 09:43
  • What's the difference? Both, min. and exact dimensions would need some sort of check before uploading. And in my opinion the answer there is rather clear: not possible with default CP. You need a plugin with custom uploader. – carlcs Oct 07 '14 at 11:07
  • The difference is I'm asking in general, not just about images - that was one example of a property to enforce for a valid upload file. – Matt Wilcox Oct 08 '14 at 08:56
  • Could you be more specific on the properties you want to check besides type, size and dimensions? – carlcs Oct 08 '14 at 10:11

1 Answers1

4

Currently, no. The only file-based validation Assets fields have right now is the type. If you want to enforce a certain size, you can do that on the template end using image transforms. I would suggest adding some instruction text to your Assets field that provides a minimum suggested size, and note that smaller images will be scaled up and look gross.

That said, if you really want to enforce a minimum width/height, or other properties, you can do so by writing a plugin that provides its own field type, which extends AssetsFieldType and overrides its validate() method with your custom validation:

public function validate($value)
{
    // Let AssetsFieldType do its validation first
    $errors = parent::validate($value);

    if (!is_array($errors))
    {
        $errors = array();
    }

    if (is_array($value) && !empty($value))
    {
        foreach ($value as $fileId)
        {
            $file = craft()->assets->getFileById($fileId);

            if ($file)
            {
                if ($file->width < 600 || $file->height < 400)
                {
                    $errors[] = Craft::t(
                        '{filename} must be at least 600x400px.',
                        array('filename' => $file->filename)
                    );
                }
            }
        }
    }

    if ($errors)
    {
        return $errors;
    }
    else
    {
        return true;
    }
}
Brandon Kelly
  • 34,307
  • 2
  • 71
  • 137
  • I do want to enforce this, the site has public members we won't be able to give training to, and from experience: people don't read instructions - though I've put some there already. Thanks Brandon, this is what I need! – Matt Wilcox Oct 09 '14 at 08:57
  • Brandon, could you please repost this as an answer to the earlier question about this topic, as this one here got closed. – carlcs Oct 09 '14 at 09:20
  • @carlcs just did. – Brandon Kelly Oct 09 '14 at 10:31