I have some very large images (10,000 x 10,000) and I want to create small thumbnails for them. Unfortunately, I can't read them into memory using imagick (imagemagick php) because I run out of memory.
On Android devices, when loading a Bitmap from disk, there is an option to downsample it as it is being loaded into memory. That allows me to load smaller versions of the image into memory (by only reading every 2 or every 4 pixels, etc) and avoid the problem of gigantic files breaking everything.
I'm trying to find a way to do something like that with imagick. I've found the setOption()
function which lets me send parameters similar to the command line params to the imagemagick tools.
One of those options is sample:offset
which says I can give it a geometry definition. One of the possible geometry definitions is to give it a percent, like 25%
, and says that means Height and width both scaled by specified percentage.
When I try to load a png
that is 10988 x 5782
and I use the following code the png is still loaded into memory at full size (it can just barely handle it on my localhost, but on the server runs out of memory):
$im = new \Imagick();
$im->setOption('sample:offset', '25%');
$im->readImage($path);
Am I using setOption()
correctly? Is this the correct option to set? Do I need a different value?
Here are some other formats I tried that also didn't seem to have any effect:
$im->setOption('sample:offset', '25%+0+0');
$im->setOption('sample:offset', '700x700'); // to scale it down to 700 pixels by 700 pixels
$im->setOption('sample:offset', '700x700+0+0');
I've been able to achieve a solution with jpg
files using $im->setOption('jpeg:size', $smallwidth.'x'.$smallheight);
but I need to be able to handle png
and other image types as well.