Skip to content

Instantly share code, notes, and snippets.

@Edofre
Last active August 13, 2019 19:22
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 Edofre/49403f6c38f8ec6c4f91ab8a79b157a4 to your computer and use it in GitHub Desktop.
Save Edofre/49403f6c38f8ec6c4f91ab8a79b157a4 to your computer and use it in GitHub Desktop.
Create thumbnails from images
<?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