Created
November 9, 2021 20:30
-
-
Save GeniJaho/dd1839fd10fd8651d1ec0a7f46f86e43 to your computer and use it in GitHub Desktop.
5 - Image convertion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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