This commit is contained in:
glovetleong
2021-06-17 09:53:04 +08:00
commit 8fa80e4a1a
2382 changed files with 196486 additions and 0 deletions
+139
View File
@@ -0,0 +1,139 @@
<?php
namespace App\Http\Helpers;
use Illuminate\Support\Str;
use Image;
class ImageHandel
{
function __construct()
{
}
public static function getImgFullPath($img_info = [], $type = 'object')
{
$default_file = asset('share/no-image.png');
if ($type == 'object') {
$default_file = asset('share/non-image.png');
}
else if ($type == 'person') {
$default_file = asset('share/non-person.png');
}
else if ($type == 'logo') {
$default_file = asset('share/non-logo.png');
}
$img_info = json_decode($img_info);
if (!empty($img_info)) {
foreach ($img_info as $key_img => $row_img) {
$img_info[$key_img]->file_info->original->file = asset('storage/' . $row_img->file_info->original->file);
}
}
else {
$img_info = [(object)[
'empty' => true,
'file_info' => (object)[
'original' => (object) [
'file' => $default_file
],
'large' => (object) [
'file' => $default_file
],
'medium' => (object) [
'file' => $default_file
],
'small' => (object) [
'file' => $default_file
],
]
]];
}
return $img_info;
}
public static function extractFullPath($file = '')
{
$file = explode(asset('/storage'), $file);
return $file[1];
}
public static function generateFolder($path = '')
{
$folder = \Storage::disk('public')->makeDirectory($path);
return $folder;
}
public static function insertImage($path = '', $file = '')
{
$image = ImageHandel::base64ToImage($path, $file);
return $image;
}
public static function base64ToImage($path = '', $base64Set = [])
{
$img_info = [];
$folder_path = ImageHandel::generateFolder('images/' . $path);
foreach ($base64Set as $key => $row) {
$imgdata = $row;
$filename = (string) Str::uuid();
$f = finfo_open();
$mime_type = finfo_file($f, $imgdata, FILEINFO_MIME_TYPE);
$extension = '';
switch ($mime_type) {
case 'image/gif':
$extension = 'gif';
break;
case 'image/png':
$extension = 'png';
break;
case 'image/jpeg':
$extension = 'jpg';
break;
}
if (empty($extension)) {
return false;
}
$file_info = [];
$filename = $filename . '.' . $extension;
$img = Image::make($row)->save(storage_path('app/public/images/' . $path) . '/' . $filename);
$file_info['original']['file'] = 'images/' . $path . '/' . $filename;
$file_info['original']['width'] = $img->width();
$file_info['original']['height'] = $img->height();
$img_info[] = [
'on_cloud' => false,
'path' => $path,
'filename' => $filename,
'mime_type' => $mime_type,
'extension' => $extension,
'file_info' => empty($file_info) ? [] : $file_info,
];
}
return $img_info;
}
public static function removeImage($img_info = [])
{
foreach ($img_info as $key_img => $row_img) {
if (!empty($row_img->path) && $row_img->path != 'seeder') {
$img_original = ImageHandel::extractFullPath($row_img->file_info->original->file);
\Storage::disk('public')->delete([$img_original]);
}
}
return true;
}
}