From dbf95fff371b820d7f1f3efcd3ad9597fa611d86 Mon Sep 17 00:00:00 2001 From: Stas Makarov Date: Wed, 31 Aug 2016 14:16:14 +0300 Subject: [PATCH 1/4] Optipng PostProcessor bugfix. --- Imagine/Filter/PostProcessor/OptiPngPostProcessor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php index 292bd735b..b10802f59 100644 --- a/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php +++ b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php @@ -52,7 +52,7 @@ public function __construct($optipngBin = '/usr/bin/optipng', $level = 7, $strip */ public function process(BinaryInterface $binary) { - $this->processWithConfiguration($binary, array()); + return $this->processWithConfiguration($binary, array()); } /** From 0e45aa141721aef9b0bcf9282561e3f3c1305ebf Mon Sep 17 00:00:00 2001 From: Stas Makarov Date: Wed, 31 Aug 2016 14:16:45 +0300 Subject: [PATCH 2/4] Added $tempDir parameter for JpegOptim and OptiPng post processors. --- .../PostProcessor/JpegOptimPostProcessor.php | 23 +++++++++++++++---- .../PostProcessor/OptiPngPostProcessor.php | 16 ++++++++++--- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php b/Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php index 031e575e7..b68d6fd02 100644 --- a/Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php +++ b/Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php @@ -34,6 +34,13 @@ class JpegOptimPostProcessor implements PostProcessorInterface, ConfigurablePost */ protected $progressive; + /** + * Directory where temporary file will be written. + * + * @var string + */ + protected $tempDir; + /** * Constructor. * @@ -41,13 +48,20 @@ class JpegOptimPostProcessor implements PostProcessorInterface, ConfigurablePost * @param bool $stripAll Strip all markers from output * @param int $max Set maximum image quality factor * @param bool $progressive Force output to be progressive + * @param string $tempDir Directory where temporary file will be written */ - public function __construct($jpegoptimBin = '/usr/bin/jpegoptim', $stripAll = true, $max = null, $progressive = true) - { + public function __construct( + $jpegoptimBin = '/usr/bin/jpegoptim', + $stripAll = true, + $max = null, + $progressive = true, + $tempDir = '' + ) { $this->jpegoptimBin = $jpegoptimBin; $this->stripAll = $stripAll; $this->max = $max; $this->progressive = $progressive; + $this->tempDir = $tempDir ?: sys_get_temp_dir(); } /** @@ -119,8 +133,9 @@ public function processWithConfiguration(BinaryInterface $binary, array $options return $binary; } - if (false === $input = tempnam(sys_get_temp_dir(), 'imagine_jpegoptim')) { - throw new \RuntimeException(sprintf('Temp file can not be created in "%s".', sys_get_temp_dir())); + $tempDir = array_key_exists('temp_dir', $options) ? $options['temp_dir'] : $this->tempDir; + if (false === $input = tempnam($tempDir, 'imagine_jpegoptim')) { + throw new \RuntimeException(sprintf('Temp file can not be created in "%s".', $tempDir)); } $pb = new ProcessBuilder(array($this->jpegoptimBin)); diff --git a/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php index b10802f59..f33f0e9a5 100644 --- a/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php +++ b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php @@ -29,18 +29,27 @@ class OptiPngPostProcessor implements PostProcessorInterface, ConfigurablePostPr */ protected $stripAll; + /** + * Directory where temporary file will be written. + * + * @var string + */ + protected $tempDir; + /** * Constructor. * * @param string $optipngBin Path to the optipng binary * @param int $level Optimization level * @param bool $stripAll Strip metadata objects + * @param string $tempDir Directory where temporary file will be written */ - public function __construct($optipngBin = '/usr/bin/optipng', $level = 7, $stripAll = true) + public function __construct($optipngBin = '/usr/bin/optipng', $level = 7, $stripAll = true, $tempDir = '') { $this->optipngBin = $optipngBin; $this->level = $level; $this->stripAll = $stripAll; + $this->tempDir = $tempDir ?: sys_get_temp_dir(); } /** @@ -72,8 +81,9 @@ public function processWithConfiguration(BinaryInterface $binary, array $options return $binary; } - if (false === $input = tempnam(sys_get_temp_dir(), 'imagine_optipng')) { - throw new \RuntimeException(sprintf('Temp file can not be created in "%s".', sys_get_temp_dir())); + $tempDir = array_key_exists('temp_dir', $options) ? $options['temp_dir'] : $this->tempDir; + if (false === $input = tempnam($tempDir, 'imagine_optipng')) { + throw new \RuntimeException(sprintf('Temp file can not be created in "%s".', $tempDir)); } $pb = new ProcessBuilder(array($this->optipngBin)); From 59f5eefe8610f8632af323d7ed36153638e388b5 Mon Sep 17 00:00:00 2001 From: Stas Makarov Date: Mon, 5 Sep 2016 17:42:18 +0300 Subject: [PATCH 3/4] Added parameters for tempDir argument of JpegOptim and OptiPng post processors --- Resources/config/imagine.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Resources/config/imagine.xml b/Resources/config/imagine.xml index da5f45af8..978ebe1ef 100644 --- a/Resources/config/imagine.xml +++ b/Resources/config/imagine.xml @@ -74,11 +74,13 @@ true null true + null Liip\ImagineBundle\Imagine\Filter\PostProcessor\OptiPngPostProcessor /usr/bin/optipng 7 true + null Liip\ImagineBundle\Imagine\Filter\PostProcessor\PngquantPostProcessor /usr/bin/pngquant @@ -307,12 +309,14 @@ %liip_imagine.jpegoptim.stripAll% %liip_imagine.jpegoptim.max% %liip_imagine.jpegoptim.progressive% + %liip_imagine.jpegoptim.tempDir% %liip_imagine.optipng.binary% %liip_imagine.optipng.level% %liip_imagine.optipng.stripAll% + %liip_imagine.optipng.tempDir% From d341d27f52949bab5d48e63de9b394703612e208 Mon Sep 17 00:00:00 2001 From: Stas Makarov Date: Mon, 5 Sep 2016 17:42:54 +0300 Subject: [PATCH 4/4] Added docs for tempDir post processors parameters --- Resources/doc/filters.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Resources/doc/filters.rst b/Resources/doc/filters.rst index 335d8b358..7fb92a6c4 100644 --- a/Resources/doc/filters.rst +++ b/Resources/doc/filters.rst @@ -430,6 +430,10 @@ for example: # When true, --all-progressive is passed to jpegoptim, which results in the output being a progressive jpeg. liip_imagine.jpegoptim.progressive: true + # The directory where temporary file will be written. By default it's empty, and computed using `sys_get_temp_dir()` + # You can set it to `/run/shm` or something similar for writing temporary files in-memory, for decrease of disk load + liip_imagine.jpegoptim.tempDir: "" + .. _`Symfony Service Container`: http://symfony.com/doc/current/book/service_container.html @@ -456,6 +460,10 @@ for example: # The optimisation level to be used by optipng. Defaults to 7. liip_imagine.optipng.level: 7 + # The directory where temporary file will be written. By default is empty, and computed using `sys_get_temp_dir()` + # You can set it to `/run/shm` or something similar for writing temporary files in-memory, for decrease of disk load + liip_imagine.optipng.tempDir: "" + .. _`Symfony Service Container`: http://symfony.com/doc/current/book/service_container.html