Intervention/image · error · RuntimeException
Unable to unset array key
Error message
Unable to unset array key
What it means
Size implements ArrayAccess only for reading width/height, so offsetUnset() always throws RuntimeException: there is no way to remove a dimension from a size object. A size without width or height is meaningless in this library, so unset() is rejected by design. Note that offsetExists('width') returns true, so an exists() check does not make unset() safe.
Source
Thrown at src/Size.php:494
* @see ArrayAccess::offsetExists()
*
* @throws RuntimeException
*/
public function offsetSet(mixed $offset, mixed $value): void
{
throw new RuntimeException('Unable to set array key, use setWidth() or setHeight()');
}
/**
* {@inheritdoc}
*
* @see ArrayAccess::offsetExists()
*
* @throws RuntimeException
*/
public function offsetUnset(mixed $offset): void
{
throw new RuntimeException('Unable to unset array key');
}
/**
* Show debug info for the current size.
*
* @return array<string, int|object>
*/
public function __debugInfo(): array
{
return [
'width' => $this->width(),
'height' => $this->height(),
'pivot' => $this->pivot,
];
}
}
View on GitHub (pinned to 5598b9e397)
Solutions
- Remove the unset() call; Size has no removable fields. If you want a fresh size, create a new Intervention\Image\Size instance instead.
- To 'reset' dimensions, call setWidth()/setHeight() with the new values rather than clearing them.
- Guard generic unset loops so they skip objects whose offsetUnset() throws, catching Intervention\Image\Exceptions\RuntimeException.
Example fix
// before unset($size['width']); // RuntimeException: Unable to unset array key // after // nothing to unset - assign a new value instead $size->setWidth(0);
Defensive patterns
Strategy: try-catch
Try / catch
try {
unset($size[$key]);
} catch (\Intervention\Image\Exceptions\RuntimeException $e) {
// nothing to unset: Size has no removable fields; ignore or assign defaults
$size->setWidth(0)->setHeight(0); // if a 'reset' was intended
} Prevention
- Skip unset() on Size objects entirely — dimensions cannot be removed.
- Do not trust offsetExists(): it returns true for 'width'/'height' even though unset() throws.
- In generic cleanup loops, skip objects of class Intervention\Image\Size or wrap in try-catch.
When it happens
Trigger: Calling unset($size['width']), unset($size[0]) or any unset($size[...]) on the result of $image->size(). Typically appears in cleanup/unset-all loops (foreach (array_keys($data) as $k) unset($obj[$k]);) or serializer code that 'clears' objects through ArrayAccess.
Common situations: Serialization/normalization helpers that reset objects before re-populating them; code ported from Intervention Image v2; copy-paste cleanup patterns that iterate ArrayAccess objects the same way as arrays.
Related errors
- Unable to set array key, use setWidth() or setHeight()
- Unable to remove color from palette via array access
- Failed to import color {colorClass} to {class}
- Invalid cmyk() color syntax "{input}"
- Normalized color channel value must be between 0 to 1
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/c0fa426736a69601.
Report an issue: GitHub.