Generate multiple avatar sizes

This commit is contained in:
Čarodej
2021-11-02 16:11:53 +01:00
parent 9b2dbe06c7
commit dc8ec5f20b
18 changed files with 197 additions and 75 deletions

View File

@@ -320,40 +320,42 @@ if (! function_exists('is_visitor')) {
if (! function_exists('store_avatar')) {
/**
* Store user avatar to storage
*
* @param $request
* @param $name
* @return string|null
* Generate multiple avatar sizes and store it in app storage
*/
function store_avatar($request, $name)
function store_avatar($request, $name): ?string
{
if (! $request->hasFile($name)) {
// Check if file exist in http request
if (!$request->hasFile($name)) {
return null;
}
// Get file
$image = $request->file($name);
// Store avatar
$image_path = Str::random(16) . '-' . $image->getClientOriginalName();
if (in_array($image->getClientMimeType(), ['image/gif', 'image/jpeg', 'image/jpg', 'image/png', 'image/webp'])) {
// Create intervention image
$img = Image::make($image->getRealPath());
// Generate thumbnail
$img->fit('150', '150')->stream();
// Store thumbnail to disk
Storage::put("avatars/$image_path", $img);
// Check supported file extension
if (! in_array($image->getClientMimeType(), ['image/gif', 'image/jpeg', 'image/jpg', 'image/png', 'image/webp'])) {
return null;
}
if ($image->getClientMimeType() === 'image/svg+xml') {
Storage::putFileAs('avatars', $image, $image_path);
}
// Generate avatar name
$avatar_name = Str::uuid() . '.' . $image->getClientOriginalExtension();
// Create intervention image
$intervention = Image::make($image->getRealPath());
// Generate avatar sizes
collect(config('vuefilemanager.avatar_sizes'))
->each(function ($size) use ($intervention, $avatar_name) {
// fit thumbnail
$intervention->fit($size['size'], $size['size'])->stream();
// Store thumbnail to disk
Storage::put("avatars/{$size['name']}-{$avatar_name}", $intervention);
});
// Return path to image
return "avatars/$image_path";
return $avatar_name;
}
}
@@ -374,7 +376,7 @@ if (! function_exists('store_system_image')) {
$image = $request->file($name);
// Store avatar
$filename = Str::random(8) . '-' . str_replace(' ', '', $image->getClientOriginalName());
$filename = Str::uuid() . $image->getClientOriginalExtension();
// Store image to disk
Storage::putFileAs('system', $image, $filename);