Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tempdir for postprocessors #779

Merged
merged 4 commits into from
Sep 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,34 @@ class JpegOptimPostProcessor implements PostProcessorInterface, ConfigurablePost
*/
protected $progressive;

/**
* Directory where temporary file will be written.
*
* @var string
*/
protected $tempDir;

/**
* Constructor.
*
* @param string $jpegoptimBin Path to the jpegoptim binary
* @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();
}

/**
Expand Down Expand Up @@ -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));
Expand Down
18 changes: 14 additions & 4 deletions Imagine/Filter/PostProcessor/OptiPngPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -52,7 +61,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());
}

/**
Expand All @@ -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));
Expand Down
4 changes: 4 additions & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@
<parameter key="liip_imagine.jpegoptim.stripAll">true</parameter>
<parameter key="liip_imagine.jpegoptim.max">null</parameter>
<parameter key="liip_imagine.jpegoptim.progressive">true</parameter>
<parameter key="liip_imagine.jpegoptim.tempDir">null</parameter>

<parameter key="liip_imagine.filter.post_processor.optipng.class">Liip\ImagineBundle\Imagine\Filter\PostProcessor\OptiPngPostProcessor</parameter>
<parameter key="liip_imagine.optipng.binary">/usr/bin/optipng</parameter>
<parameter key="liip_imagine.optipng.level">7</parameter>
<parameter key="liip_imagine.optipng.stripAll">true</parameter>
<parameter key="liip_imagine.optipng.tempDir">null</parameter>

<parameter key="liip_imagine.filter.post_processor.pngquant.class">Liip\ImagineBundle\Imagine\Filter\PostProcessor\PngquantPostProcessor</parameter>
<parameter key="liip_imagine.pngquant.binary">/usr/bin/pngquant</parameter>
Expand Down Expand Up @@ -307,12 +309,14 @@
<argument>%liip_imagine.jpegoptim.stripAll%</argument>
<argument>%liip_imagine.jpegoptim.max%</argument>
<argument>%liip_imagine.jpegoptim.progressive%</argument>
<argument>%liip_imagine.jpegoptim.tempDir%</argument>
<tag name="liip_imagine.filter.post_processor" post_processor="jpegoptim" />
</service>
<service id="liip_imagine.filter.post_processor.optipng" class="%liip_imagine.filter.post_processor.optipng.class%">
<argument>%liip_imagine.optipng.binary%</argument>
<argument>%liip_imagine.optipng.level%</argument>
<argument>%liip_imagine.optipng.stripAll%</argument>
<argument>%liip_imagine.optipng.tempDir%</argument>
<tag name="liip_imagine.filter.post_processor" post_processor="optipng" />
</service>
<service id="liip_imagine.filter.post_processor.pngquant" class="%liip_imagine.filter.post_processor.pngquant.class%">
Expand Down
8 changes: 8 additions & 0 deletions Resources/doc/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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


Expand Down