LargeTuna LargeTuna - 9 months ago 59
PHP Question

imagecopyresampled() expects parameter 2 to be resource, null given

I am attempting to resize an image using php. I keep getting this error when I resize my image greater than 500 pixels, not sure why.

Here's the error:

imagecopyresampled() expects parameter 2 to be resource, null given

Here is my code that doesn't work:

$resize = new ResizeImage($targetFile);
$resize->resizeTo(1800, 1800);
$resize->saveImage($xxlFile, 100);

Here's my code that does work:

$resize = new ResizeImage($targetFile);
$resize->resizeTo(500, 500);
$resize->saveImage($xxlFile, 100);

My script is breaking right here:

$this->newImage = imagecreatetruecolor($this->resizeWidth, $this->resizeHeight);
imagecopyresampled($this->newImage, $this->image, 0, 0, 0, 0, $this->resizeWidth, $this->resizeHeight, $this->origWidth, $this->origHeight);

My image is 5616 × 3744. Why cant it resize 1800 pixels wide?


Answer Source

$this->image is null... Your script failed when it created the destination image, could it be not enough memory allowed for such a big picture?

for a test, execute this command before running ResizeImage(): ini_set("memory_limit", "1024M");.

Also, remember loading a 5616 × 3744px image will require 63mb of memory (24bit / 3byte image), and resize it down to a 1800, 1800px (24bit / 3byte) image will require additional 10mb of memory (usually PHP is set to 64mb or 32mb of memory).

Good luck!

[EDIT] So your 500x500x3 image will require 750kb of memory, your large picture will take 63 078 kb of memory, and together wil land on 63 828 kb, just under the 64mb memory limit :)