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

Optimize ImageInterface instance creation. #401

Merged
merged 1 commit into from
Oct 27, 2024
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
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
Loading