fuel/core — Issue #852: Image Class - Large Image resize exhausts memory
Author: mikebranderhorst
Created: 2012-02-28 01:40:09+00:00
State: open

The Image Class is fantastic!

But when resizing large images (+/- 2.5mb+) you'll get errors.

Case: I have a large jpg and want to resize it to 1080 height 75 quality (landscape and portrait).

```
$config = array('quality' => 75);

$file = 'image.jpg'; // large image

$image = \Image::forge($config)->load($file);

$sizes = $image->sizes();

$image->resize(ceil(($sizes->width / $sizes->height) * 1080), 1080)->save($file);

```

After trying for a while I just did it the old fashion way:

```
$url = 'http://...'; // remote large image

ini_set('gd.jpeg_ignore_warning', 1);

$src_image = @imagecreatefromjpeg($url);

if ($src_image)
{
$src_w = imagesx($src_image);
$src_h = imagesy($src_image);

$dst_w = ($src_w / $src_h) * 1080;
$dst_h = 1080;

$dst_image = imagecreatetruecolor($dst_w, $dst_h);

if ($dst_image)
{
if (imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h))
{
if (imagejpeg($dst_image, $file, 75))
{
// log and add info to db
}
}

imagedestroy($dst_image);
}

imagedestroy($src_image);
}
```

This way is to the eye three times faster and without errors and a nice image.

I have no time to dig in the very huge but nice Image Class.

I saw a few things playing with the class:

1) Missing a fallback driver, say imagick default and gd as fallback.
2) It gave me images one pixel width and xxx height or 1 pixel height...
Maybe an extra dimensions (sizes) check at some places?
3) Missing a max width / max height setting to say always fit a screen size for example 1920x1080.
4) a watermark is not remembered, if you set it, resize, save and resize and save again the second is without watermark.

```
$image
->watermark(...)
->resize(...)->save()
->resize(...)->save();
```

5) some images gave me an error on resize but doing this did the trick (resize first to it's own dimensions)

```
$config = array('quality' => 75);

$file = 'image.jpg'; // large image

$image = \Image::forge($config)->load($file);

$sizes = $image->sizes();

$image
->resize($sizes->width, $sizes->height)
->resize(...)
->save($file);

```