diff --git a/lib/Helper/TempImage.php b/lib/Helper/TempImage.php index cb8a1d44..52e8aebe 100644 --- a/lib/Helper/TempImage.php +++ b/lib/Helper/TempImage.php @@ -33,10 +33,10 @@ class TempImage extends Image { /** @var string */ private $imagePath; - /** @var string */ + /** @var string|null */ private $orientedImagePath; - /** @var string */ + /** @var string|null */ private $resizedImagePath; /** @var string */ @@ -84,11 +84,13 @@ public function getImagePath(): string { } /** - * Get the path of orig image + * Get the path of oriented or original image * * @return string */ public function getOrientedImagePath(): string { + $this->prepareOrientedImage(); + return $this->orientedImagePath; } @@ -142,17 +144,10 @@ private function prepareImage() { return; } - if ($this->getOrientation() > 1) { - $this->fixOrientation(); - $this->orientedImagePath = $this->tempManager->getTemporaryFile(); - $this->save($this->orientedImagePath, $this->preferredMimeType); - } else { - $this->orientedImagePath = $this->imagePath; - } - + $this->fixOrientation(); $this->ratio = $this->resizeImage(); - $this->resizedImagePath = $this->tempManager->getTemporaryFile(); + $this->resizedImagePath = $this->tempManager->getTemporaryFile(); $this->save($this->resizedImagePath, $this->preferredMimeType); } @@ -184,4 +179,29 @@ private function resizeImage(): float { return 1 / $scaleFactor; } + /** + * Obtain a temporary image according to the imposed restrictions. + * + */ + private function prepareOrientedImage() { + if (!is_null($this->orientedImagePath)) { + return; + } + + $this->loadFromFile($this->imagePath); + + if (!$this->valid()) { + throw new \RuntimeException("Image is not valid, probably cannot be loaded"); + } + + if ($this->getOrientation() > 1) { + $this->fixOrientation(); + + $this->orientedImagePath = $this->tempManager->getTemporaryFile(); + $this->save($this->orientedImagePath, $this->preferredMimeType); + } else { + $this->orientedImagePath = $this->imagePath; + } + } + } \ No newline at end of file