Set PHP ImageMagick tmp directory

一曲冷凌霜 提交于 2019-12-19 03:25:26

问题


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.


回答1:


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');


来源:https://stackoverflow.com/questions/40868089/set-php-imagemagick-tmp-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!