Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set PHP ImageMagick tmp directory

I am trying to set the temporary directory that ImageMagick uses to convert files. Currently, when converting large PDFs, the temp folder quickly gets to 2 or 3 terabytes. This is too much to hold on the servers disk, so I plan to use an AWS EFS to store everything. I have the EFS drive mounted at /efs and am trying to use it for the temp path.

How can I set this in ImageMagick? I have tried the following:

  • Setting PHP's temp upload folder in php.ini - this breaks file uploads and is no good
  • Changing ImagickMagick's config.xml file, which doesn't work.
  • Adding environment variable MAGICK_TMPDIR=/efs
  • Adding environment variable MAGICK_TEMPORARY_PATH=/efs

No matter what I do, it always does the conversion in /tmp folder. How can I set this?

Is it different because it is PHP? Apparently in the command line version you can do this:

convert -define registry:temporary-path=/Volumes/external/tmp

My current PHP code is this, I was wondering if there is a function to set the tmp dir here? Like a $imagick->setTmpDir('/efs') sort of thing. I have searched the PHP API and cannot find any way to do so.

$imagick = new Imagick();
$imagick->setResourceLimit( Imagick::RESOURCETYPE_MEMORY, 5 ); //Limit RAM and force script to convert using disk swap
$imagick->setResolution(600,600); //Set DPI to 600
$imagick->setCompressionQuality(100);
$imagick->readImageBlob($file); //Load the image
$imagick->deskewImage(40); //Deskew image
$imagick->setImageFormat('jpg'); //Set format
$imagick->writeImages(storage_path("app/docs/".$doc->id.".jpg"), false); //Save the converted image

Any ideas? I have been doing this for days!

Ubuntu server.

like image 572
samiles Avatar asked Oct 17 '22 21:10

samiles


1 Answers

Imagick has a "setRegistry" method. http://php.net/manual/en/imagick.setregistry.php

You can use it to set the imagemagick temp path like so:

$i = new Imagick();
$i->setRegistry('temporary-path', '/efs');
like image 151
Brice Bentler Avatar answered Oct 21 '22 02:10

Brice Bentler