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

Use tmp_disk for correct EXIF image rotation of remote images. #210

Merged
merged 1 commit into from
Sep 30, 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
9 changes: 9 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
*/
'crops_disk' => 'public',

/*
* The (optional) disk where remote source images are temporarily copied
* to allow correct EXIF image rotation to occur.
* (The Intervention Image library can't correctly orientate remote images -
* see: https://image.intervention.io/v2/api/orientate)
* Set to false/null/empty-string to disable.
*/
'tmp_disk' => 'public',

/*
* Maximum number of sizes to allow for a particular source file. This is to
* limit scripts from filling up your hard drive with images. Set to false or
Expand Down
60 changes: 55 additions & 5 deletions src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ final class Storage
*/
private $srcDisk;

/**
* @var ?FilesystemAdapter
*/
private $tmpDisk;

/**
* @var bool
*/
private $tmpPath = '';

/**
* Inject dependencies.
*/
Expand Down Expand Up @@ -84,6 +94,26 @@ public function getSrcDisk(): FilesystemAdapter
return $this->srcDisk;
}

/**
* Set the tmp disk.
*/
public function setTmpDisk(FilesystemAdapter $disk): void
{
$this->tmpDisk = $disk;
}

/**
* Get the tmp disk or make via the config.
*/
public function getTmpDisk(): FilesystemAdapter
{
if (empty($this->tmpDisk)) {
$this->setTmpDisk($this->makeDisk($this->config['tmp_disk']));
}

return $this->tmpDisk;
}

/**
* "Mount" disks given the config.
*/
Expand Down Expand Up @@ -124,13 +154,21 @@ public function cropExists(string $path): bool
*/
public function path(string $path): string
{
$disk = $this->getSrcDisk();
if ($disk->fileExists($path)) {
if ($disk->getAdapter() instanceof LocalFilesystemAdapter) {
return $disk->path($path);
$srcDisk = $this->getSrcDisk();
if ($srcDisk->fileExists($path)) {
if ($srcDisk->getAdapter() instanceof LocalFilesystemAdapter) {
return $srcDisk->path($path);
}

// If the src_disk is a remote disk, and a tmp_disk has been configured, copy file to tmp_disk - otherwise EXIF auto-rotation won't work)
if ($this->config['tmp_disk']) {
$tmpDisk = $this->getTmpDisk();
$tmpDisk->writeStream($path, $srcDisk->readStream($path));
$this->tmpPath = $path;
return $tmpDisk->path($path);
}

return $disk->url($path);
return $srcDisk->url($path);
}

throw new NotFoundHttpException('Croppa: Src image is missing');
Expand All @@ -148,6 +186,18 @@ public function writeCrop(string $path, string $contents): void
} catch (FilesystemException $e) {
// don't throw exception anymore as mentioned in PR #164
}
$this->cleanup();
}

/**
* Cleanup: delete tmp file if required.
*/
public function cleanup(): void
{
if ($this->tmpPath <> '') {
$this->getTmpDisk()->delete($this->tmpPath);
$this->tmpPath = '';
}
}

/**
Expand Down