{"record":{"id":"5f729a38ab28aa74","repo":"Intervention/image","slug":"division-by-zero-5f729a","errorCode":null,"errorMessage":"Division by zero","messagePattern":"Division by zero","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/Size.php","lineNumber":242,"sourceCode":"        return new Point(\n            $this->pivot()->x() - $size->pivot()->x(),\n            $this->pivot()->y() - $size->pivot()->y(),\n        );\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see SizeInterface::aspectRatio()\n     *\n     * @throws RuntimeException\n     */\n    public function aspectRatio(): float\n    {\n        try {\n            return $this->width() / $this->height();\n        } catch (DivisionByZeroError) {\n            throw new RuntimeException('Division by zero');\n        }\n    }\n\n    /**\n     * {@inheritdoc}\n     *\n     * @see SizeInterface::fitsWithin()\n     */\n    public function fitsWithin(SizeInterface $size): bool\n    {\n        if ($this->width() > $size->width()) {\n            return false;\n        }\n\n        if ($this->height() > $size->height()) {\n            return false;\n        }\n","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/Intervention/image/blob/5598b9e39751c34afc5cdee84abef77f92c26f68/src/Size.php#L224-L260","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\n$ratio = $image->size()->aspectRatio(); // RuntimeException when height is 0\n\n// after\n$size = $image->size();\n$ratio = $size->height() > 0 ? $size->aspectRatio() : null;\nif ($ratio === null) {\n    // handle degenerate image\n}","handlingStrategy":"validation","validationCode":"// Guard zero-height sizes before computing the ratio\n$size = $image->size();\n\nif ($size->height() === 0) {\n    throw new \\RuntimeException('Image has zero height; aspect ratio undefined');\n}\n\n$ratio = $size->aspectRatio();","typeGuard":"function hasDefinedAspectRatio(\\Intervention\\Image\\Interfaces\\SizeInterface $size): bool\n{\n    return $size->height() !== 0;\n}","tryCatchPattern":"use Intervention\\Image\\Exceptions\\RuntimeException;\n\ntry {\n    $ratio = $size->aspectRatio();\n} catch (RuntimeException $e) {\n    // message: 'Division by zero'\n    $ratio = null; // degenerate zero-height image\n}","preventionTips":["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"],"tags":["size","aspect-ratio","division-by-zero","math","intervention-image"],"backgroundTag":"division-by-zero","analyzedSha":"5598b9e39751c34afc5cdee84abef77f92c26f68","analyzedAt":"2026-08-23T02:17:31.068Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}