Skip to content

Commit

Permalink
Merge branch 'pr/210'
Browse files Browse the repository at this point in the history
  • Loading branch information
jordanade committed Sep 29, 2024
2 parents 09dc88f + 9d3df2f commit 849d3ce
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
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

0 comments on commit 849d3ce

Please sign in to comment.