From bb8e4109672902e37931e0a491ff55ebac93d8e9 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 4 Feb 2016 18:05:19 +0000 Subject: [PATCH] Add FileBinary[Interface] to support large files without loading them unnecessarily --- Binary/FileBinaryInterface.php | 11 +++ Binary/Loader/FileSystemLoader.php | 6 +- Imagine/Filter/FilterManager.php | 7 +- .../PostProcessor/JpegOptimPostProcessor.php | 11 ++- .../PostProcessor/OptiPngPostProcessor.php | 11 ++- Model/FileBinary.php | 67 +++++++++++++++++++ Tests/Binary/Loader/FileSystemLoaderTest.php | 2 +- 7 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 Binary/FileBinaryInterface.php create mode 100644 Model/FileBinary.php diff --git a/Binary/FileBinaryInterface.php b/Binary/FileBinaryInterface.php new file mode 100644 index 000000000..09e143cd5 --- /dev/null +++ b/Binary/FileBinaryInterface.php @@ -0,0 +1,11 @@ +mimeTypeGuesser->guess($absolutePath); - return new Binary( - file_get_contents($absolutePath), + return new FileBinary( + $absolutePath, $mimeType, $this->extensionGuesser->guess($mimeType) ); diff --git a/Imagine/Filter/FilterManager.php b/Imagine/Filter/FilterManager.php index be7c942f0..2bd57866f 100644 --- a/Imagine/Filter/FilterManager.php +++ b/Imagine/Filter/FilterManager.php @@ -4,6 +4,7 @@ use Imagine\Image\ImagineInterface; use Liip\ImagineBundle\Binary\BinaryInterface; +use Liip\ImagineBundle\Binary\FileBinaryInterface; use Liip\ImagineBundle\Binary\MimeTypeGuesserInterface; use Liip\ImagineBundle\Imagine\Filter\PostProcessor\PostProcessorInterface; use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface; @@ -100,7 +101,11 @@ public function apply(BinaryInterface $binary, array $config) $config ); - $image = $this->imagine->load($binary->getContent()); + if ($binary instanceof FileBinaryInterface) { + $image = $this->imagine->open($binary->getPath()); + } else { + $image = $this->imagine->load($binary->getContent()); + } foreach ($config['filters'] as $eachFilter => $eachOptions) { if (!isset($this->loaders[$eachFilter])) { diff --git a/Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php b/Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php index af6a1419d..0f621a06e 100644 --- a/Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php +++ b/Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php @@ -3,7 +3,8 @@ namespace Liip\ImagineBundle\Imagine\Filter\PostProcessor; use Liip\ImagineBundle\Binary\BinaryInterface; -use Liip\ImagineBundle\Model\Binary; +use Liip\ImagineBundle\Binary\FileBinaryInterface; +use Liip\ImagineBundle\Model\FileBinary; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\ProcessBuilder; @@ -112,7 +113,11 @@ public function process(BinaryInterface $binary) } $pb->add($input = tempnam(sys_get_temp_dir(), 'imagine_jpegoptim')); - file_put_contents($input, $binary->getContent()); + if ($binary instanceof FileBinaryInterface) { + copy($binary->getPath(), $input); + } else { + file_put_contents($input, $binary->getContent()); + } $proc = $pb->getProcess(); $proc->run(); @@ -122,7 +127,7 @@ public function process(BinaryInterface $binary) throw new ProcessFailedException($proc); } - $result = new Binary(file_get_contents($input), $binary->getMimeType(), $binary->getFormat()); + $result = new FileBinary($input, $binary->getMimeType(), $binary->getFormat()); unlink($input); diff --git a/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php index 4227f11a0..5ae870dac 100644 --- a/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php +++ b/Imagine/Filter/PostProcessor/OptiPngPostProcessor.php @@ -3,7 +3,8 @@ namespace Liip\ImagineBundle\Imagine\Filter\PostProcessor; use Liip\ImagineBundle\Binary\BinaryInterface; -use Liip\ImagineBundle\Model\Binary; +use Liip\ImagineBundle\Binary\FileBinaryInterface; +use Liip\ImagineBundle\Model\FileBinary; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\ProcessBuilder; @@ -42,7 +43,11 @@ public function process(BinaryInterface $binary) $pb->add('--o7'); $pb->add($input = tempnam(sys_get_temp_dir(), 'imagine_optipng')); - file_put_contents($input, $binary->getContent()); + if ($binary instanceof FileBinaryInterface) { + copy($binary->getPath(), $input); + } else { + file_put_contents($input, $binary->getContent()); + } $proc = $pb->getProcess(); $proc->run(); @@ -52,7 +57,7 @@ public function process(BinaryInterface $binary) throw new ProcessFailedException($proc); } - $result = new Binary(file_get_contents($input), $binary->getMimeType(), $binary->getFormat()); + $result = new FileBinary($input, $binary->getMimeType(), $binary->getFormat()); unlink($input); diff --git a/Model/FileBinary.php b/Model/FileBinary.php new file mode 100644 index 000000000..4e7b820ad --- /dev/null +++ b/Model/FileBinary.php @@ -0,0 +1,67 @@ +path = $path; + $this->mimeType = $mimeType; + $this->format = $format; + } + + /** + * @return string + */ + public function getContent() + { + return file_get_contents($this->path); + } + + /** + * @return string + */ + public function getPath() + { + return $this->path; + } + + /** + * @return string + */ + public function getMimeType() + { + return $this->mimeType; + } + + /** + * @return string + */ + public function getFormat() + { + return $this->format; + } +} diff --git a/Tests/Binary/Loader/FileSystemLoaderTest.php b/Tests/Binary/Loader/FileSystemLoaderTest.php index 715389fa0..57b19046b 100644 --- a/Tests/Binary/Loader/FileSystemLoaderTest.php +++ b/Tests/Binary/Loader/FileSystemLoaderTest.php @@ -102,7 +102,7 @@ public function testLoad($rootDir, $path) $binary = $loader->find($path); - $this->assertInstanceOf('Liip\ImagineBundle\Model\Binary', $binary); + $this->assertInstanceOf('Liip\ImagineBundle\Model\FileBinary', $binary); $this->assertStringStartsWith('text/', $binary->getMimeType()); } }