Problem/Motivation
All three image-based field validation rules — AI image constraint (AiImageConstraintValidator, vision chat), AI image classification constraint (AiImageClassificationConstraintValidator) and AI object detection constraint (AiObjectDetectionConstraintValidator) — send the full-size original upload to the provider. Each loads the File and wraps it unchanged with $image->setFileFromFile($file).
For the vision-chat rule that is wasteful: most vision models bill image input as tokens proportional to the image's dimensions, so a multi-megapixel upload costs many times more tokens than is needed to answer a yes/no validation question. For the classification and object-detection rules a full-size original means a larger payload, slower requests and, on hosted providers, higher cost — even though the model rarely needs the original resolution. Drupal already ships the tool to fix this: image styles produce a downscaled derivative.
Proposed resolution
Add an optional Image style setting to each of the three image rules. When a style is selected, the validator runs the upload through that image style and sends the derivative to the provider instead of the original; when left empty it falls back to the original (current behaviour), so the change is fully backward compatible.
Implementation
Mirror the change across the three rule/validator pairs.
1. Rule plugins — in AiImageConstraintFieldValidationRule, AiImageClassificationConstraintFieldValidationRule and AiObjectDetectionConstraintFieldValidationRule: add 'image_style' => NULL to defaultConfiguration(), add a select element in buildConfigurationForm() populated by \Drupal\image\Entity\ImageStyle::loadMultiple() (with an empty "— Original —" option), and persist it in submitConfigurationForm(). Expose the value on the constraint so the validator can read $constraint->image_style.
2. Validators — after the File is loaded and before building the ImageFile, branch on the configured style:$image = new ImageFile();
if a style id is set, load it, compute $uri = $style->buildUri($file->getFileUri());, create the derivative when missing with $style->createDerivative($file->getFileUri(), $uri); and feed it in with $image->setFileFromUri($uri); — otherwise keep $image->setFileFromFile($file);. AbstractFileBase already provides setFileFromUri(), so no new AI-core API is needed.
Files to touch: src/Plugin/FieldValidationRule/AiImageConstraintFieldValidationRule.php, src/Plugin/FieldValidationRule/AiImageClassificationConstraintFieldValidationRule.php, src/Plugin/FieldValidationRule/AiObjectDetectionConstraintFieldValidationRule.php, src/Plugin/Validation/Constraint/AiImageConstraintValidator.php, src/Plugin/Validation/Constraint/AiImageClassificationConstraintValidator.php, src/Plugin/Validation/Constraint/AiObjectDetectionConstraintValidator.php (plus the matching *Constraint.php classes to declare the new public property).
Notes
Image styles only apply to raster derivatives — guard against failure and fall back to the original when createDerivative() returns FALSE (e.g. SVG or an unsupported MIME type) so validation never hard-fails on a derivative problem. The token saving is most pronounced for the vision-chat AI image constraint, where input tokens scale with image dimensions, but reduced upload size benefits the classification and detection rules too. Recommend a sensible downscale (for example a ~1024px-bounded style) as guidance in the field description, and add the selected style as a config dependency of the field so exports stay consistent.
Issue fork ai_validations-3601250
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
marcus_johansson commentedComment #3
abhisekmazumdarComment #5
abhisekmazumdarI have implemented the proposed resolution. Its ready for review.
What changed across all three image-based rule plugins
ImageStyle::loadMultiple()with a blank "Original (no style)" option at the top.imageStyle(lowerCamel, required by Drupal coding standards). The three matching*Constraint.phpclasses each declarepublic $imageStyle = NULL;.applyImageStyle()helper added toAiConstraintValidatorBase. It loads the image style entity, builds the derivative URI via$style->buildUri(), creates the derivative if missing, and callssetFileFromUri(). If derivative creation fails (SVG, unsupported MIME, read-only filesystem), it falls back tosetFileFromFile().calculateDependencies()is implemented on all three rule plugins so the selected style is tracked as a config dependency of the rule set.drupal:imageadded as a hard dependency inai_validations.info.yml.Existing rule sets with no
imageStylestored are unaffected.Comment #6
abhisekmazumdarTesting steps
Repeat the steps below for each of the three rule types: AI image constraint, AI image classification constraint, and AI object detection constraint.
Optional: config export check
Export config after saving a rule with a style selected. Open the rule set YAML and confirm the chosen image style appears under
dependencies.config, e.g.image.style.medium.Comment #7
marcus_johansson commentedComment #8
marcus_johansson commentedJust added NIT. Feel free ot merge and skip if you want or fix and merge.
Comment #9
abhisekmazumdarComment #15
abhisekmazumdar