Skip to content

Commit

Permalink
Add FileBinary[Interface] to support large files without loading them…
Browse files Browse the repository at this point in the history
… unnecessarily
  • Loading branch information
Seldaek committed Feb 4, 2016
1 parent 417a2d5 commit bb8e410
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 11 deletions.
11 changes: 11 additions & 0 deletions Binary/FileBinaryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Liip\ImagineBundle\Binary;

interface FileBinaryInterface extends BinaryInterface
{
/**
* @return string
*/
public function getPath();
}
6 changes: 3 additions & 3 deletions Binary/Loader/FileSystemLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Liip\ImagineBundle\Binary\Loader;

use Liip\ImagineBundle\Model\Binary;
use Liip\ImagineBundle\Model\FileBinary;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesserInterface;
use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
Expand Down Expand Up @@ -57,8 +57,8 @@ public function find($path)

$mimeType = $this->mimeTypeGuesser->guess($absolutePath);

return new Binary(
file_get_contents($absolutePath),
return new FileBinary(
$absolutePath,
$mimeType,
$this->extensionGuesser->guess($mimeType)
);
Expand Down
7 changes: 6 additions & 1 deletion Imagine/Filter/FilterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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])) {
Expand Down
11 changes: 8 additions & 3 deletions Imagine/Filter/PostProcessor/JpegOptimPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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());

This comment has been minimized.

Copy link
@xavierlacot

xavierlacot Feb 12, 2016

The file gets unlink'ed 2 lines below, so this seems to break the filter :-)

This comment has been minimized.

Copy link
@Seldaek

Seldaek Feb 13, 2016

Author Contributor

Fixed in #707


unlink($input);

Expand Down
11 changes: 8 additions & 3 deletions Imagine/Filter/PostProcessor/OptiPngPostProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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);

Expand Down
67 changes: 67 additions & 0 deletions Model/FileBinary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Liip\ImagineBundle\Model;

use Liip\ImagineBundle\Binary\FileBinaryInterface;

class FileBinary implements FileBinaryInterface
{
/**
* @var string
*/
protected $path;

/**
* @var string
*/
protected $mimeType;

/**
* @var string
*/
protected $format;

/**
* @param string $content
* @param string $mimeType
* @param string $format
*/
public function __construct($path, $mimeType, $format = null)
{
$this->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;
}
}
2 changes: 1 addition & 1 deletion Tests/Binary/Loader/FileSystemLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit bb8e410

Please sign in to comment.