diff --git a/app/Classes/Intervention/Transform.php b/app/Classes/Intervention/Transform.php index 4bc2a85..9448a17 100644 --- a/app/Classes/Intervention/Transform.php +++ b/app/Classes/Intervention/Transform.php @@ -12,7 +12,6 @@ use ImagickException; use Intervention\Image\Encoders\AutoEncoder; use Intervention\Image\Laravel\Facades\Image as ImageManager; -use InvalidArgumentException; class Transform implements TransformInterface { @@ -23,58 +22,52 @@ class Transform implements TransformInterface * @param array|null $transformations * * @return string Binary string of the image. - * @throws FileNotFoundException */ public function transform(string $pathToOriginalImage, ?array $transformations = null): string { $fileHandle = $this->getOriginalFileStream($pathToOriginalImage); $mimeType = mime_content_type($fileHandle); + $fileData = stream_get_contents($fileHandle); return match ($mimeType) { - 'application/pdf' => $this->pdfToImage($fileHandle, $transformations), - default => $this->applyTransformations(stream_get_contents($fileHandle), $transformations), + 'application/pdf' => $this->pdfToImage($fileData, $transformations), + default => $this->applyTransformations($fileData, $transformations), }; } /** - * @param $fileHandle + * @param string $fileData * @param array|null $transformations * @return string * @throws PdfPageDoesNotExistException */ - protected function pdfToImage($fileHandle, ?array $transformations = null): string + protected function pdfToImage(string $fileData, ?array $transformations = null): string { - // There is no type hint for resource. - if (!is_resource($fileHandle)) { - throw new InvalidArgumentException(sprintf('Argument must be a valid resource type, %s given.', gettype($fileHandle))); - } - - // We need a temporary file to speed up the PDF to image transformation by accessing only a single page. + // We need a local file for Imagick to be able to access only the requested page. $tempFile = tempnam(sys_get_temp_dir(), 'transmorpher'); - file_put_contents($tempFile, $fileHandle); - - $imagick = new Imagick(); - - // 300 DPI, might make this a transformation - $imagick->setResolution(300, 300); + file_put_contents($tempFile, $fileData); try { + $imagick = new Imagick(); + + // 300 DPI, might make this a transformation + $imagick->setResolution(300, 300); $imagick->readImage(sprintf('%s[%d]', $tempFile, ($transformations[Transformation::PAGE->value] ?? 1) - 1)); } catch (ImagickException) { // Assuming an error happened because the requested page does not exist, we throw a custom exception. throw new PdfPageDoesNotExistException($transformations[Transformation::PAGE->value]); + } finally { + unlink($tempFile); } - unlink($tempFile); - $imagick->setImageFormat(ImageFormat::from(config('transmorpher.pdf_default_image_format'))->value); - return $this->applyTransformations($imagick->getImage(), $transformations); + return $this->applyTransformations($imagick->getImageBlob(), $transformations); } /** * @param string $path - * @return resource + * @return resource|null * @throws FileNotFoundException */ protected function getOriginalFileStream(string $path) @@ -89,11 +82,11 @@ protected function getOriginalFileStream(string $path) } /** - * @param $imageData + * @param string $imageData * @param array|null $transformations * @return string */ - protected function applyTransformations($imageData, ?array $transformations = null): string + protected function applyTransformations(string $imageData, ?array $transformations = null): string { if (!$transformations) { return $imageData; diff --git a/app/Classes/MediaHandler/PdfHandler.php b/app/Classes/MediaHandler/PdfHandler.php index 5b1bc51..d69e073 100644 --- a/app/Classes/MediaHandler/PdfHandler.php +++ b/app/Classes/MediaHandler/PdfHandler.php @@ -53,6 +53,6 @@ public function applyTransformations(Version $version, ?array $transformationsAr return Optimize::optimize($derivative, $transformationsArray[Transformation::QUALITY->value] ?? null); } - return Optimize::removePdfMetadata(MediaStorage::ORIGINALS->getDisk()->readStream($version->originalFilePath())); + return Optimize::removePdfMetadata(MediaStorage::ORIGINALS->getDisk()->get($version->originalFilePath())); } } diff --git a/app/Classes/Optimizer/Optimize.php b/app/Classes/Optimizer/Optimize.php index 9ae266d..ee61997 100644 --- a/app/Classes/Optimizer/Optimize.php +++ b/app/Classes/Optimizer/Optimize.php @@ -12,16 +12,14 @@ class Optimize * Optimize an image derivative. * Creates a temporary file since image optimizers only work locally. * - * @param $derivative + * @param string $derivative * @param int|null $quality * @return string * @throws Exception */ - public static function optimize($derivative, int $quality = null): string + public static function optimize(string $derivative, int $quality = null): string { - // Temporary file is needed since optimizers only work locally. - $tempFile = tempnam(sys_get_temp_dir(), 'transmorpher'); - file_put_contents($tempFile, $derivative); + $tempFile = self::getTemporaryFile($derivative); // Optimizes the image based on optimizers configured in 'config/image-optimizer.php'. ImageFormat::fromMimeType(mime_content_type($tempFile))->getOptimizer()->optimize($tempFile, $quality); @@ -38,21 +36,36 @@ public static function optimize($derivative, int $quality = null): string } /** - * @param resource|string|array $derivative + * @param string $derivative * @return string + * @throws Exception */ - public static function removePdfMetadata(mixed $derivative): string + public static function removePdfMetadata(string $derivative): string { - $tempFile = tempnam(sys_get_temp_dir(), 'transmorpher'); - file_put_contents($tempFile, $derivative); - + $tempFile = self::getTemporaryFile($derivative); $pdfMerge = new PdfMerge(); - $pdfMerge->add($tempFile); - $pdfData = $pdfMerge->merge('', 'S'); + try { + $pdfMerge->add($tempFile); + $pdfData = $pdfMerge->merge('', 'S'); + } catch (Exception $exception) { + unlink($tempFile); - unlink($tempFile); + throw $exception; + } return $pdfData; } + + /** + * @param string $derivative + * @return false|string + */ + protected static function getTemporaryFile(string $derivative): string|false + { + $tempFile = tempnam(sys_get_temp_dir(), 'transmorpher'); + file_put_contents($tempFile, $derivative); + + return $tempFile; + } }