Skip to content

Commit

Permalink
Optimize ImageInterface instance creation.
Browse files Browse the repository at this point in the history
- Remove unneeded temp image creation
- Specify only the required binary image decoder class for ImageManager::read()
  • Loading branch information
ADmad committed Oct 27, 2024
1 parent 8d51c57 commit 01924c8
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace League\Glide\Api;

use Intervention\Image\Decoders\BinaryImageDecoder;
use Intervention\Image\ImageManager;
use Intervention\Image\Interfaces\ImageInterface;
use League\Glide\Manipulators\ManipulatorInterface;
Expand Down Expand Up @@ -88,7 +89,7 @@ public function getManipulators(): array
*/
public function run(string $source, array $params): string
{
$image = $this->imageManager->read($source);
$image = $this->imageManager->read($source, BinaryImageDecoder::class);

foreach ($this->manipulators as $manipulator) {
$manipulator->setParams($params);
Expand Down
13 changes: 1 addition & 12 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -620,24 +620,13 @@ public function makeImage(string $path, array $params): string
throw new FilesystemException('Could not read the image `'.$sourcePath.'`.');
}

// We need to write the image to the local disk before
// doing any manipulations. This is because EXIF data
// can only be read from an actual file.
$tmp = tempnam($this->tempDir, 'Glide');

if (false === file_put_contents($tmp, $source)) {
throw new FilesystemException('Unable to write temp file for `'.$sourcePath.'`.');
}

try {
$this->cache->write(
$cachedPath,
$this->api->run($tmp, $this->getAllParams($params))
$this->api->run($source, $this->getAllParams($params))
);
} catch (FilesystemV2Exception $exception) {
throw new FilesystemException('Could not write the image `'.$cachedPath.'`.');
} finally {
unlink($tmp);
}

return $cachedPath;
Expand Down
5 changes: 4 additions & 1 deletion tests/Api/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public function testRun()

$api = new Api($manager, [$manipulator]);

$this->assertEquals('encoded', $api->run(dirname(__FILE__, 2).'/files/red-pixel.png', []));
$this->assertEquals('encoded', $api->run(
file_get_contents(dirname(__FILE__, 2).'/files/red-pixel.png'),
[]
));
}
}
7 changes: 2 additions & 5 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace League\Glide;

use Hamcrest\Matchers;
use League\Glide\Filesystem\FileNotFoundException;
use League\Glide\Filesystem\FilesystemException;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -522,8 +521,7 @@ public function testMakeImageFromSource()
}));

$this->server->setApi(\Mockery::mock('League\Glide\Api\ApiInterface', function ($mock) {
$tmpDirPattern = Matchers::matchesPattern('~\/?'.sys_get_temp_dir().'.*~');
$mock->shouldReceive('run')->withArgs([$tmpDirPattern, []])->andReturn('content')->once();
$mock->shouldReceive('run')->withArgs(['content', []])->andReturn('content')->once();
}));

$this->assertEquals(
Expand All @@ -546,8 +544,7 @@ public function testMakeImageFromSourceWithCustomTmpDir()

$this->server->setTempDir(__DIR__);
$this->server->setApi(\Mockery::mock('League\Glide\Api\ApiInterface', function ($mock) {
$tmpDirPattern = Matchers::matchesPattern('~^'.__DIR__.'.*~');
$mock->shouldReceive('run')->with($tmpDirPattern, [])->andReturn('content')->once();
$mock->shouldReceive('run')->with('content', [])->andReturn('content')->once();
}));

$this->assertEquals(
Expand Down

0 comments on commit 01924c8

Please sign in to comment.