Memory and 503 errors with gif animation converts

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
dethfire
Posts: 6
Joined: 2017-04-14T13:17:48-07:00
Authentication code: 1151

Memory and 503 errors with gif animation converts

Post by dethfire »

I'm using imagemagick to resize images and any time I come across gif animations I get memory allocation errors or a 503. I have 8GB RAM free. Is there a config file where I need to increase memory?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Memory and 503 errors with gif animation converts

Post by fmw42 »

What is your IM version and platform? Have you checked your policy.xml file? That is where resources are allocated for Imagemagick.
dethfire
Posts: 6
Joined: 2017-04-14T13:17:48-07:00
Authentication code: 1151

Re: Memory and 503 errors with gif animation converts

Post by dethfire »

IM 6.7.2-7 CentOS 6

policy.xml is default

What attribute to increase?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Memory and 503 errors with gif animation converts

Post by fmw42 »

What does your policy.xml file show?

Code: Select all

convert -list resource
will tell you. Also see http://www.imagemagick.org/source/policy.xml

I am not an expert on this, but I suspect it is one of the memory, map or disk parameters.
dethfire
Posts: 6
Joined: 2017-04-14T13:17:48-07:00
Authentication code: 1151

Re: Memory and 503 errors with gif animation converts

Post by dethfire »

File Area Memory Map Disk Thread Time
-------------------------------------------------------------------------------
3072 4.1719GB 15.542GiB 31.083GiB unlimited 24 unlimited

Those feel like solid numbers and not which a single gif should trip. For CentOS 6 what is the latest version I can run?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Memory and 503 errors with gif animation converts

Post by fmw42 »

They look adequate to me. I do not run lInux, so cannot say what version of IM comes with it, but IM is at 6.9.8.3 (so over 250 versions old). I would be surprised if it was IM and not some communication issues? Are your file and folder permission adequate. Does IM run to convert normal image?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Memory and 503 errors with gif animation converts

Post by fmw42 »

Perhaps you have a hung animation. Try killing any IM convert processes. Also perhaps you have some dead animations that are hanging around in your MAGICK_TEMP directory or your /tmp directory. Check for any files with Imagemagick in the name. If they are too full, then there may not be enough room to finish an animation.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Memory and 503 errors with gif animation converts

Post by snibgo »

dethfire wrote:I'm using imagemagick to resize images and any time I come across gif animations I get memory allocation errors or a 503.
What is your command?

How large is your input, in pixels? (How many frames? Width and height of each frame?)
snibgo's IM pages: im.snibgo.com
dethfire
Posts: 6
Joined: 2017-04-14T13:17:48-07:00
Authentication code: 1151

Re: Memory and 503 errors with gif animation converts

Post by dethfire »

I'm running imagemagick via PHP. Here are some snippets of the code.


//#####################################
// resize image
//#####################################

// resize if width exceeds maximum allowed
if ($width > $attachmentMaxWidth)
{
$imagick = new Imagick();
$imagick->readImage($tempFullPath);
$imagick->resizeImage($attachmentMaxWidth, 0, Imagick::FILTER_LANCZOS, 1);
$imagick->writeImage($tempFullPath);
$imagick->clear();
$imagick->destroy();

// get new image size
list($width, $height) = getimagesize($tempFullPath);
}

// resize if height exceeds maximum allowed
if ($height > $attachmentMaxHeight)
{
$imagick = new Imagick();
$imagick->readImage($tempFullPath);
$imagick->resizeImage(0, $attachmentMaxHeight, Imagick::FILTER_LANCZOS, 1);
$imagick->writeImage($tempFullPath);
$imagick->clear();
$imagick->destroy();

// get new image size
list($width, $height) = getimagesize($tempFullPath);
}

//#####################################
// create thumbnail
//#####################################

// declare variable
$thumbSaved = '';

// define thumbpath
$thumbpath = $externalDataPath . '/attachments/' . $lastfolder . '/' . $dataId . '-' . $filehash . '.jpg';

// create blank file
if (!file_exists($thumbpath))
{
touch($thumbpath);
chmod($thumbpath, octdec($filePermission));
}

// save thumbnail if smaller than maximum width and height
if ($width <= $attachmentThumbnailDimensions AND $height <= $attachmentThumbnailDimensions)
{
// copy attachment
copy("$attachmentFullPath", "$thumbpath");

// thumbSaved
$thumbSaved = 'yes';
}

// if thumbnail not saved
if ($thumbSaved == '')
{
// define imagick
$imagick = new Imagick();
$imagick->readImage($attachmentFullPath);

// if image has a transparency
if ($imagick->getImageAlphaChannel())
{
// define thumbpathTemp
$thumbpathTemp = $temporaryImageDirectory . $filehash;

// create image
$image = new Imagick($attachmentFullPath);

// resize image
if ($width == $height)
{
$image->thumbnailImage($attachmentThumbnailDimensions, $attachmentThumbnailDimensions);
}
if ($width > $height)
{
$image->thumbnailImage($attachmentThumbnailDimensions, 0);
}
if ($width < $height)
{
$image->thumbnailImage(0, $attachmentThumbnailDimensions);
}

// save image
$image->writeImage($thumbpathTemp);
$image->clear();
$image->destroy();

// update attachment file
rename("$thumbpathTemp", "$thumbpath");

// thumbSaved
$thumbSaved = 'yes';
}

// if image is animated
if ($numberImages > 1)
{
// define thumbpathTemp
$thumbpathTemp = $temporaryImageDirectory . $filehash;

// create image
$image = new Imagick($attachmentFullPath);

// coalesce images
$image = $image->coalesceImages();

// resize image
foreach ($image as $frame)
{
if ($width == $height)
{
$frame->thumbnailImage($attachmentThumbnailDimensions, $attachmentThumbnailDimensions);
}
if ($width > $height)
{
$frame->thumbnailImage($attachmentThumbnailDimensions, 0);
}
if ($width < $height)
{
$frame->thumbnailImage(0, $attachmentThumbnailDimensions);
}
}

// deconstruct images
$image = $image->deconstructImages();

// save image
$image->writeImages($thumbpathTemp, true);
$image->clear();
$image->destroy();

// update attachment file
rename("$thumbpathTemp", "$thumbpath");

// thumbSaved
$thumbSaved = 'yes';
}
}

// if thumbnail not saved
if ($thumbSaved == '')
{
// resize image
$imagick->resizeImage($attachmentThumbnailDimensions, $attachmentThumbnailDimensions, Imagick::FILTER_QUADRATIC, .5, true);
$imagick->writeImage($thumbpath);
$imagick->clear();
$imagick->destroy();
}


A few example gif animations that cause the errors are:
https://upload.wikimedia.org/wikipedia/ ... wave3D.gif
http://animatedphysics.com/photons/anim ... on_mid.gif
http://www.spoonfedrelativity.com/web_i ... mentum.gif
https://upload.wikimedia.org/wikipedia/ ... 8-cell.gif

The exact error is:
memory allocation failed `.../public_html/tmp/ 9c1f51293667f29d3664c295cb02429f' @ error/gif.c/WriteGIFImage/1623

/tmp has 1.3G free space
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Memory and 503 errors with gif animation converts

Post by snibgo »

It is trying to write to ".../public_html/tmp/", and failing. I suppose there isn't enough space there.
snibgo's IM pages: im.snibgo.com
dethfire
Posts: 6
Joined: 2017-04-14T13:17:48-07:00
Authentication code: 1151

Re: Memory and 503 errors with gif animation converts

Post by dethfire »

There is no disk space restriction on that folder and the disk has 100G free space. I can clear that folder and it still gives the error.
dethfire
Posts: 6
Joined: 2017-04-14T13:17:48-07:00
Authentication code: 1151

Re: Memory and 503 errors with gif animation converts

Post by dethfire »

Should I update to 6.9.8.3? Is that the latest version for CentOS 6?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Memory and 503 errors with gif animation converts

Post by fmw42 »

memory allocation failed `.../public_html/tmp/ 9c1f51293667f29d3664c295cb02429f'
I do not know if this is your error or just the way the message is presented, but there is a space between tmp/ and 9c....

If you have not quoted your path/file, the space may be a problem.

IM 6.8.9.3 is the latest IM version, but CentOS may not be providing that version. Thus you would need to check with CentOS for IM 6 or use the IM 7 binary that you can get at http://www.imagemagick.org/script/binary-releases.php

Can you test the animation resize in the command line and not via Imagick? Does it work there? If so, then it is an Imagick/PHP issue.

If you cannot do it directly in the command line, then try

<?php
exec("convert animated_photon_mid.gif -coalesce -resize 50% -layers optimize test.gif 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>

Does that work? If not then do you get any messages in your browsers window when it is run? You may need to put the full path to convert and to your gifs.

Have you checked directory permissions?
Post Reply