7

I think the transform feature is great, although in my case I need to request that certain images be uploaded at specific dimensions to an assets field. (i.e. 500 x 300 pixels). These dimensions will also need to be required on the front-end Guest Entries form. Although I don't see this option in the control panel or in the docs. Is this something that can be done?

Angela
  • 569
  • 2
  • 12

3 Answers3

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
2

You are correct, a front-end would be needed. It is however possible to limit by upload size (how many bytes).

Jeff Clayton
  • 144
  • 2
  • Thanks Jeff - although the byte size limit is not what I need. I need for each image to be a specific width and height at upload. Anything more or less would not work for the layout, and image transform after upload could compromise the quality of the image, depending on what is uploaded. – Angela Jul 16 '14 at 13:12
  • Very true - jQuery may have a nice uploader i would look there first. – Jeff Clayton Jul 16 '14 at 16:33
  • There are new file methods available in modern browsers that are supposed to be able to do that. It may be a solution for you. – Jeff Clayton Jul 16 '14 at 20:17
  • Most welcome, hope it works out! – Jeff Clayton Jul 17 '14 at 20:41
2

I believe you'd need to do some front-end pre-processing of the image to get it to the correct size(s) you require before passing it off to Craft.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143