Intervention/image · error · Intervention\Image\Exceptions\InvalidArgumentException
Color channel {classname} could not be found
Error message
Color channel {classname} could not be found What it means
Font::setAlignmentHorizontal() accepts a string or an Alignment enum. Strings are converted with Alignment::from(), which is an exact, case-sensitive match against the nine enum backing values ('top', 'top-right', 'right', 'bottom-right', 'bottom', 'bottom-left', 'left', 'top-left', 'center'). Any other string — including alias forms that Alignment::create() would accept, like 'center-left' or 'top_left' — raises a ValueError that the setter rethrows as InvalidArgumentException('Invalid value for alignment'). You hit it via FontFactory::align($horizontal, $vertical) or the text API's alignment option.
Source
Thrown at src/Colors/AbstractColor.php:57
return $this->channels;
}
/**
* {@inheritdoc}
*
* @see ColorInterface::channel()
*
* @throws InvalidArgumentException
*/
public function channel(string $classname): ColorChannelInterface
{
$channels = array_filter(
$this->channels(),
fn(ColorChannelInterface $channel): bool => $channel::class === $classname,
);
if (count($channels) === 0) {
throw new InvalidArgumentException('Color channel ' . $classname . ' could not be found');
}
return reset($channels);
}
/**
* {@inheritdoc}
*
* @see ColorInterface::toColorspace()
*
* @throws InvalidArgumentException
*/
public function toColorspace(string|ColorspaceInterface $colorspace): ColorInterface
{
if (is_string($colorspace) && !class_exists($colorspace)) {
throw new InvalidArgumentException('Unknown color space (' . $colorspace . ') as conversion target');
}
View on GitHub (pinned to 5598b9e397)
Solutions
- Use one of the nine exact lowercase values: 'left', 'center', 'right' (pure horizontal), or the diagonal forms like 'top-left'
- Normalize input before passing: $value = strtolower(str_replace('_', '-', $userValue)) and map aliases yourself, or call Alignment::create($value) which understands the alias table, and pass the resulting enum
- Pass an Alignment enum instance instead of a string to get static type safety
Example fix
// before
$font->align('center_left'); // not a backing value -> throws
// after
use Intervention\Image\Alignment;
$font->align(Alignment::create('center_left')); // alias-aware, returns Alignment::LEFT
// or simply: $font->align('left'); Defensive patterns
Strategy: type-guard
Validate before calling
use Intervention\Image\Alignment;
$alignment = Alignment::tryFrom(strtolower((string) $input));
if ($alignment === null) {
$alignment = Alignment::tryCreate((string) $input) // alias-aware fallback
?? throw new \InvalidArgumentException('Unsupported alignment: ' . $input);
}
$font->align($alignment); Type guard
use Intervention\Image\Alignment;
function toHorizontalAlignment(string $value): Alignment
{
return Alignment::tryFrom(strtolower($value))
?? Alignment::tryCreate($value)
?? throw new \InvalidArgumentException('Invalid horizontal alignment: ' . $value);
} Try / catch
use Intervention\Image\Exceptions\InvalidArgumentException;
try {
$font->align($userValue);
} catch (InvalidArgumentException $e) {
if (str_contains($e->getMessage(), 'alignment')) {
$font->align('center'); // safe default
}
} Prevention
- Whitelist horizontal values at the boundary: ['left', 'center', 'right'] (plus diagonal enum cases when needed)
- Normalize external strings: lowercase, underscores/dashes unified, before they reach the font API
- Prefer passing Alignment enum instances over strings in internal code
When it happens
Trigger: $font->align('center-left'), 'top_left', 'Center' (capitalized), 'middle', 'centre' or any custom string; feeding alignment from user input or a config file without validating it; values built by string concatenation ('top' . '-' . 'center').
Common situations: Front-end or API alignment values in snake_case or with mixed case; reusing values that worked with the position/Alignment::create() alias table elsewhere in the library; i18n/localized alignment names ('centro', 'Mitte') passed straight through.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- Unknown color space ({colorspace}) as conversion target
- Unable to parse RGB color from input "{input}"
- Invalid $limit value. Must be int<1, max>
- Call to undefined method Intervention\Image\Image::{name}()
- Argument $extension must not be an empty string
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/e0f861f8c8061ff1.
Report an issue: GitHub.