image icon indicating copy to clipboard operation
image copied to clipboard

Resizing an image can throw "division by zero" exception for extreme ratios

Open FrittenKeeZ opened this issue 5 years ago • 1 comments

Resizing images (creating thumbnails) with extreme ratios, like a divider of 1920x3 px, will throw a "division by zero" exception as the width/height ends up being less than 0.5 px and subject to rounding. All images should be ensure to be at least 1x1 px.

See statamic/v2-hub#2051 for stacktrace.

FrittenKeeZ avatar Jul 09 '18 15:07 FrittenKeeZ

I know this is an old topic, but I've stumbled unto this issue too. The image ratio doesn't even need to be that extreme.

Quick fix:

1: edit Size.php: add 1 line to the getRatio() function at line 93:


    public function getRatio()
    {
        if (! $this->height) return $this->width;  // <- add this line
        return $this->width / $this->height;
    }

2: edit ResizeCommand.php: add 1 line to th modify function at line 44:

     protected function modify($image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)
    {
        // create new image
        if (!$dst_h) return false;  // <- add this line
        $modified = imagecreatetruecolor($dst_w, $dst_h);

        // get current image
        $resource = $image->getCore();

        ....

The height can be set to zero, or become zero at some point (perhaps due to bad usage of this package). At first I've tried to prevent the height to a minimum of 1, but this resulted in unexpected behavior.

AforDesign avatar May 18 '22 13:05 AforDesign

Is fixed also for Version 3 in https://github.com/Intervention/image/commit/66b28952384a7e6e5918fbd2238689fcd4677574.

olivervogel avatar Jan 07 '24 11:01 olivervogel