Skip to content

Instantly share code, notes, and snippets.

@GeniJaho
Created November 9, 2021 20:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeniJaho/dd1839fd10fd8651d1ec0a7f46f86e43 to your computer and use it in GitHub Desktop.
Save GeniJaho/dd1839fd10fd8651d1ec0a7f46f86e43 to your computer and use it in GitHub Desktop.
5 - Image convertion
<?php
...
private const TEMP_HEIC_STORAGE_DIR = 'app/heic_images/';
protected function convertHeic(UploadedFile $file): \Intervention\Image\Image
{
$extension = $file->getClientOriginalExtension();
// Generating a random filename, and not using the image's
// original filename to handle cases
// that contain spaces or other weird characters
$randomFilename = bin2hex(random_bytes(8));
// Path for a temporary file from the upload
// storage/app/heic_images/sample1.heic
$tmpFilepath = storage_path(
self::TEMP_HEIC_STORAGE_DIR .
$randomFilename . ".$extension"
);
// Path for a converted temporary file
// storage/app/heic_images/sample1.jpg
$convertedFilepath = storage_path(
self::TEMP_HEIC_STORAGE_DIR .
$randomFilename . '.jpg'
);
// Store the uploaded HEIC file on the server
File::put($tmpFilepath, $file->getContent());
// Run a shell command to execute ImageMagick conversion
exec('magick convert ' . $tmpFilepath . ' ' . $convertedFilepath);
// Make the image from the new converted file
$image = Image::make($convertedFilepath);
// Remove the temporary files from storage
unlink($tmpFilepath);
unlink($convertedFilepath);
return $image;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment