Intervention/image · error · RuntimeException
Division by zero
Error message
Division by zero
What it means
Size::aspectRatio() returns width / height as a float. A Size with height 0 is legal (the constructor only forbids negative values), but dividing by that height raises PHP's DivisionByZeroError, which the method catches and rethrows as a library RuntimeException with the message 'Division by zero'. This gives callers a predictable, driver-independent exception for zero-height images.
Source
Thrown at src/Size.php:242
return new Point(
$this->pivot()->x() - $size->pivot()->x(),
$this->pivot()->y() - $size->pivot()->y(),
);
}
/**
* {@inheritdoc}
*
* @see SizeInterface::aspectRatio()
*
* @throws RuntimeException
*/
public function aspectRatio(): float
{
try {
return $this->width() / $this->height();
} catch (DivisionByZeroError) {
throw new RuntimeException('Division by zero');
}
}
/**
* {@inheritdoc}
*
* @see SizeInterface::fitsWithin()
*/
public function fitsWithin(SizeInterface $size): bool
{
if ($this->width() > $size->width()) {
return false;
}
if ($this->height() > $size->height()) {
return false;
}
View on GitHub (pinned to 5598b9e397)
Solutions
- Guard before dividing: if ($size->height() === 0) handle the degenerate case instead of calling aspectRatio()
- Fix the upstream code that produced a zero-height Size
- Treat zero-height images as invalid input and reject them early in your pipeline
Example fix
// before
$ratio = $image->size()->aspectRatio(); // RuntimeException when height is 0
// after
$size = $image->size();
$ratio = $size->height() > 0 ? $size->aspectRatio() : null;
if ($ratio === null) {
// handle degenerate image
} Defensive patterns
Strategy: validation
Validate before calling
// Guard zero-height sizes before computing the ratio
$size = $image->size();
if ($size->height() === 0) {
throw new \RuntimeException('Image has zero height; aspect ratio undefined');
}
$ratio = $size->aspectRatio(); Type guard
function hasDefinedAspectRatio(\Intervention\Image\Interfaces\SizeInterface $size): bool
{
return $size->height() !== 0;
} Try / catch
use Intervention\Image\Exceptions\RuntimeException;
try {
$ratio = $size->aspectRatio();
} catch (RuntimeException $e) {
// message: 'Division by zero'
$ratio = null; // degenerate zero-height image
} Prevention
- Remember height 0 passes Size's constructor validation — only aspectRatio() rejects it
- Reject zero-height/zero-width images as invalid input early in your pipeline
- Note this surfaces as the library RuntimeException, not PHP's DivisionByZeroError
When it happens
Trigger: Calling (new Size(120, 0))->aspectRatio(), or $image->size()->aspectRatio() on a degenerate zero-height image/crop result. Also reachable indirectly through proportional sizing math that divides by a zero height.
Common situations: Edge-crop results that produce a zero-height region; test fixtures using 0-height canvases; images or crops created from bad user dimensions that passed the >= 0 constructor check; analytics code computing aspect ratios for arbitrary stored sizes.
Related errors
- Width of {class} must be greater than or equal to 0
- Height of {class} must be greater than or equal to 0
- Invalid target size {width}x{height}
- Unable to set array key, use setWidth() or setHeight()
- Unable to unset array key
AI-assisted analysis of Intervention/image@5598b9e397 (2026-08-23).
Data as JSON: /api/errors/5f729a38ab28aa74.
Report an issue: GitHub.