1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 14:00:56 +01:00

Support imagemagick on new image transform pathway

Summary: Ref T7707. For animated GIFs, use imagemagick if it is available.

Test Plan: Generated small versions of a bunch of different GIFs.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T7707

Differential Revision: https://secure.phabricator.com/D12813
This commit is contained in:
epriestley 2015-05-12 11:51:55 -07:00
parent fd1e0bc4d3
commit 200e525df1

View file

@ -42,7 +42,19 @@ abstract class PhabricatorFileImageTransform extends PhabricatorFileTransform {
$off_x = ($dst_w - $cpy_w) / 2;
$off_y = ($dst_h - $cpy_h) / 2;
// TODO: Support imagemagick for animated GIFs.
if ($this->shouldUseImagemagick()) {
$argv = array();
$argv[] = '-coalesce';
$argv[] = '-shave';
$argv[] = $src_x.'x'.$src_y;
$argv[] = '-resize';
$argv[] = $dst_w.'x'.$dst_h.'>';
$argv[] = '-bordercolor';
$argv[] = 'rgba(255, 255, 255, 0)';
$argv[] = '-border';
$argv[] = $off_x.'x'.$off_y;
return $this->applyImagemagick($argv);
}
$src = $this->getImage();
$dst = $this->newEmptyImage($dst_w, $dst_h);
@ -72,6 +84,22 @@ abstract class PhabricatorFileImageTransform extends PhabricatorFileTransform {
return $this->newFileFromData($data);
}
protected function applyImagemagick(array $argv) {
$tmp = new TempFile();
Filesystem::writeFile($tmp, $this->getData());
$out = new TempFile();
$future = new ExecFuture('convert %s %Ls %s', $tmp, $argv, $out);
// Don't spend more than 10 seconds resizing; just fail if it takes longer
// than that.
$future->setTimeout(10)->resolvex();
$data = Filesystem::readFile($out);
return $this->newFileFromData($data);
}
/**
* Create a new @{class:PhabricatorFile} from raw data.
@ -299,4 +327,22 @@ abstract class PhabricatorFileImageTransform extends PhabricatorFileTransform {
return $this->image;
}
private function shouldUseImagemagick() {
if (!PhabricatorEnv::getEnvConfig('files.enable-imagemagick')) {
return false;
}
if ($this->file->getMimeType() != 'image/gif') {
return false;
}
// Don't try to preserve the animation in huge GIFs.
list($x, $y) = $this->getImageDimensions();
if (($x * $y) > (512 * 512)) {
return false;
}
return true;
}
}