Very slow "writeImages"

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
sugrob
Posts: 8
Joined: 2017-03-22T08:14:58-07:00
Authentication code: 1151

Very slow "writeImages"

Post by sugrob »

Hey. I read many forums and did not find the answer.
I use:
PHP Version 5.3.3
imagick module version 3.1.2
imagick classes Imagick, ImagickDraw, ImagickPixel, ImagickPixelIterator
ImageMagick version ImageMagick 6.7.2-7 2016-06-16 Q16


Everything works on a virtual server on "centos" - there are access to all the settings. I need to crop the animated GIF, resize it and save it - make a thumbnail for the preview.

Solve the problem so:

Code: Select all

$image = new Imagick($original_file);

$image = $image->coalesceImages(); // We do coalesceimages again because now we need to resize

foreach ($image as $frame) {
            $i++;
            $frame->cropImage($width, $height, $x, $y); // You crop the big image first
            $frame->resizeImage($square_size, $square_size,Imagick::FILTER_LANCZOS,1);
            $frame->setImagePage(0, 0, 0, 0); // Remove canvas
        }
        $image = $image->deconstructImages();           
        $image->setImageCompressionQuality(60);
        $image->stripImage();

        $image->writeImages($destination_file, true);

        $image->clear();
        $image->destroy();
The standard solution. Everything is working.
But when i open GIF size bigger 5MB with the number of frames from 20. (stalker-zona-tvorchestva.ru/temp/579.gif)
And the problem begins - the function $image->writeImages($destination_file, true); - It makes 50 seconds. Its very slow
To track the execution time, I surrounded each function with the output to the log.

Fragment of the log below:
21:34:07.0.3866069316864 frame 21
21:34:07.0.38687705993652 cropImage start
21:34:07.0.38782596588135 cropImage end
21:34:07.0.3881208896637 resizeImage start
21:34:07.0.39612603187561 resizeImage end
21:34:07.0.39630103111267 setImagePage start
21:34:07.0.39666104316711 setImagePage end
21:34:07.0.3970320224762 frame 22
21:34:07.0.39730310440063 cropImage start
21:34:07.0.39823389053345 cropImage end
21:34:07.0.39853692054749 resizeImage start
21:34:07.0.40652990341187 resizeImage end
21:34:07.0.40671300888062 setImagePage start
21:34:07.0.40713906288147 setImagePage end
21:34:07.0.40750408172607 frame 23
21:34:07.0.40775609016418 cropImage start
21:34:07.0.4087290763855 cropImage end
21:34:07.0.40902996063232 resizeImage start
21:34:07.0.41764903068542 resizeImage end
21:34:07.0.41790294647217 setImagePage start
21:34:07.0.41828489303589 setImagePage end
21:34:07.0.41867995262146 deconstructImages start
21:34:07.0.43926596641541 deconstructImages end
21:34:07.0.43949699401855 setImageCompressionQuality start
21:34:07.0.43990802764893 setImageCompressionQuality end
21:34:07.0.44032788276672 writeImages start
21:34:57.0.19726300239563 writeImages end
I can not understand why the image saved in the file so slowly. Nowhere is such a problem.
On online services processing of the specified GIF occurs in 1-2 seconds, I have without saving 1-2 seconds.

I ask help how to fix the problem and find its solution !!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Very slow "writeImages"

Post by snibgo »

Perhaps you have run out of memory, so IM is using disk?
snibgo's IM pages: im.snibgo.com
sugrob
Posts: 8
Joined: 2017-03-22T08:14:58-07:00
Authentication code: 1151

Re: Very slow "writeImages"

Post by sugrob »

Fragment of command "top":
Mem: 1922100k total, 1433476k used, 488624k free, 185996k buffers
Swap: 502780k total, 15496k used, 487284k free, 718312k cached

and on disk i have more 20GB free.

If i out of memory how can i see it?
sugrob
Posts: 8
Joined: 2017-03-22T08:14:58-07:00
Authentication code: 1151

Re: Very slow "writeImages"

Post by sugrob »

IM command
$image->writeImages($destination_file, true);
is write on disk. it is slow operation? but 50 seconds? I copy this file by half a second.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Very slow "writeImages"

Post by fmw42 »

GIF compression to 256 colors or fewer is known to be rather slow. See http://www.imagemagick.org/script/quantize.php Your processing is changing the number of colors (due to resampling in the resizing). So you are compressing each frame separately down to 256 colors. You might want to use a common color map. See http://www.imagemagick.org/Usage/quantize/#remap

To make files smaller again, you should use the equivalent of the command line -layers optimize.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Very slow "writeImages"

Post by snibgo »

Where did you run the "top" command? Within PHP, I hope?

You are running ImageMagick under IMagick under PHP. We can only guess where the bottleneck is.

I suggest you replicate the operation as a "convert" or "magick" command, at the command line. If it takes 50 seconds or 2 seconds, then you have narrowed down where the problem is.
snibgo's IM pages: im.snibgo.com
sugrob
Posts: 8
Joined: 2017-03-22T08:14:58-07:00
Authentication code: 1151

Re: Very slow "writeImages"

Post by sugrob »

snibgo
"Where did you run the "top" command?" - in centos command line - i have many memory free.
"You are running ImageMagick under IMagick under PHP" - under PHP 5.3.3 i set it at top of first message
I understand. i try to make command at command line, but it takes some time - i need reed how made it. Thanks for very good idea!

I try view result with
//header('Content-Type: image/' . $image->getImageFormat());
//echo $image->getimageblob();
2-5 second. and in log i see that command writeImages slow - but why? I try work under command line, maybe it help to understand.

fmw42
i dont need small file - i need fast crop and resize.
i try $image->optimizeImageLayers(); - dont help - nothing change.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Very slow "writeImages"

Post by snibgo »

sugrob wrote:"Where did you run the "top" command?" - in centos command line - i have many memory free.
So "top" told you how much memory was available to the command line. But that doesn't say anything useful. It doesn't tell you how much memory is available to ImageMagick under IMagick under PHP.
snibgo's IM pages: im.snibgo.com
sugrob
Posts: 8
Joined: 2017-03-22T08:14:58-07:00
Authentication code: 1151

Re: Very slow "writeImages"

Post by sugrob »

snibgo - good question. I am not good linux user and dont know how look how much memory is available to ImageMagick under Magick under PHP.
you give me direction for seach answer. thanks.
sugrob
Posts: 8
Joined: 2017-03-22T08:14:58-07:00
Authentication code: 1151

Re: Very slow "writeImages"

Post by sugrob »

i made command line

Code: Select all

convert /var/www/579.gif -coalesce  -resize 200x200  /var/www//1.gif
40 seconds.
php IM work like command line

I think the operation "writeondisk" - slow, but why? files copy very fast.
Now i try to find how many memory used IM.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Very slow "writeImages"

Post by snibgo »

How many pixels per frame? How many frames?

If you don't resize, how long does it take?

If you use "-sample" instead of "-resize", is this faster? Fast enough? Is the quality good enough?

There are other possibilities. See http://www.imagemagick.org/Usage/anim_opt/#color_opt
snibgo's IM pages: im.snibgo.com
sugrob
Posts: 8
Joined: 2017-03-22T08:14:58-07:00
Authentication code: 1151

Re: Very slow "writeImages"

Post by sugrob »

snibgo - link to the image in first message. (stalker-zona-tvorchestva.ru/temp/579.gif)
I need 2 images from original - 200x200 and 100x100 where first i made crop and then resize - alwase. alwase i have differense images with differense pixels per frame and frames.

I try command in PHP

Code: Select all

system("convert ".$original_file. " -coalesce -repage 0x0 -crop ".$width."x".$height." -resize 200x200 -layers Optimize ". $destination_file);
8 seconds (but crop dont work well because use not right).

I turn off in first code resize ($frame->resizeImage($square_size, $square_size,Imagick::FILTER_LANCZOS,1);) and it works <5sec

the problen in resize. i try work with parameters resize.

thanks for help.
sugrob
Posts: 8
Joined: 2017-03-22T08:14:58-07:00
Authentication code: 1151

Re: Very slow "writeImages"

Post by sugrob »

For this time i find good solution

Code: Select all

			$image = new Imagick();
			$image->readImageFile($handle);
			$image = $image->coalesceImages();

			foreach ($image as $frame) {
				$frame->cropImage($width, $height, $x, $y); 
				$frame->setImagePage(0, 0, 0, 0); 
			}
			$image = $image->coalesceImages(); 
			
			foreach ($image as $frame) { 
				$frame->resizeImage($square_size, $square_size,Imagick::FILTER_POINT,1); 
			}
			$image->setImageCompressionQuality(60);
			$image->stripImage();
			$image->writeImages($destination_file, true);
			$image->clear();
			$image->destroy();
it works <3seconds.
Quality good for situation. I check image size and number of frames and set type of filter dynamically.

Thanks for all help.
Post Reply