Skip to content

Commit

Permalink
streamline passing of media data to Transform and Optimize functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mszulik committed Feb 17, 2025
1 parent d8e150a commit 15066ac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 38 deletions.
41 changes: 17 additions & 24 deletions app/Classes/Intervention/Transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion app/Classes/MediaHandler/PdfHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
39 changes: 26 additions & 13 deletions app/Classes/Optimizer/Optimize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
}

0 comments on commit 15066ac

Please sign in to comment.