Last active
August 13, 2019 19:22
-
-
Save Edofre/49403f6c38f8ec6c4f91ab8a79b157a4 to your computer and use it in GitHub Desktop.
Create thumbnails from images
This file contains 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 | |
// Create instance | |
$img = \Image::make(storage_path("app/{$uploadPath}" . $fileName)); | |
// Make sure | |
$img->orientate(); | |
// Resize only the width of the image | |
$img->resize(250, null, function ($constraint) { | |
$constraint->aspectRatio(); | |
}); | |
// Create a thumbnail from the image | |
$imageThumbnailPath = "app/{$uploadPath}" . get_thumbnail($fileName); | |
// Finally we save the image as a new file | |
$img->save(storage_path($imageThumbnailPath)); | |
if (!function_exists('get_thumbnail')) { | |
/** | |
* Adds the image suffix to get the thumbnail of the image instead | |
* @param string $imageName | |
* @return string | |
*/ | |
function get_thumbnail(string $imageName) | |
{ | |
// Thumbnail suffix, generated by hand for now // TODO | |
$suffix = '_tn'; | |
$imageExtension = pathinfo($imageName, PATHINFO_EXTENSION); | |
$thumbailName = (str_replace('.' . $imageExtension, "", $imageName)); | |
return "{$thumbailName}{$suffix}.{$imageExtension}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment