first commit
This commit is contained in:
114
app/Traits/ImageHelper.php
Normal file
114
app/Traits/ImageHelper.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use Illuminate\Http\File;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Intervention\Image\Facades\Image;
|
||||
|
||||
|
||||
trait ImageHelper
|
||||
{
|
||||
use MimeToExtensionHelper;
|
||||
|
||||
public $public_path = "/public/uploads/";
|
||||
public $storage_path = "/storage/uploads/";
|
||||
|
||||
|
||||
public function uploadPDF($file, $prefix): string
|
||||
{
|
||||
$fileName = '';
|
||||
$directory = 'uploads';
|
||||
if ($file) {
|
||||
$extension = $file->getClientOriginalExtension();
|
||||
$fileName = $prefix . '_' . Str::random(30) . '.' . $extension;
|
||||
$file = file_get_contents($file);
|
||||
if (!Storage::disk('public')->exists($directory)) {
|
||||
Storage::disk('public')->makeDirectory($directory);
|
||||
}
|
||||
Storage::disk('public')->put($directory.'/'.$fileName, $file);
|
||||
|
||||
}
|
||||
return '/uploads/'.$fileName;
|
||||
}
|
||||
|
||||
public function uploadFile($file, $prefix, bool $isEncoded): string
|
||||
{
|
||||
if ($isEncoded) {
|
||||
$file = $this->create_file_from_base64($file);
|
||||
}
|
||||
$imagePath = '';
|
||||
if ($file) {
|
||||
$extension = $file->getClientOriginalExtension();
|
||||
$fileName = $prefix . '_' . Str::random(30) . '.' . $extension;
|
||||
$url = $file->storeAs($this->public_path, $fileName);
|
||||
$publicPath = public_path($this->storage_path . $fileName);
|
||||
$img = Image::make($publicPath);
|
||||
$url = preg_replace("/public/", "", $url);
|
||||
$imagePath = $img->save($publicPath) ? $url : '';
|
||||
}
|
||||
return $imagePath;
|
||||
}
|
||||
|
||||
public function create_file_from_base64($base64File): UploadedFile
|
||||
{
|
||||
// $fileData = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $base64File));
|
||||
$fileData = base64_decode(Arr::last(explode(',', $base64File)));
|
||||
|
||||
//Get MimeType
|
||||
$fileInfo = finfo_open();
|
||||
$mimeType = finfo_buffer($fileInfo, $fileData, FILEINFO_MIME_TYPE);
|
||||
|
||||
//Get Extension from MimeType
|
||||
$ext = $this->mime2ext($mimeType);
|
||||
|
||||
// save it to temporary dir first.
|
||||
// save it to temporary dir first.
|
||||
$tempFilePath = sys_get_temp_dir() . '/' . Str::uuid()->toString() . '.' . $ext;
|
||||
|
||||
file_put_contents($tempFilePath, $fileData);
|
||||
|
||||
$tempFileObject = new File($tempFilePath);
|
||||
return new UploadedFile(
|
||||
$tempFileObject->getPathname(),
|
||||
$tempFileObject->getFilename(),
|
||||
$tempFileObject->getMimeType(),
|
||||
0,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
public function getFileFullUrl($file): null|string
|
||||
{
|
||||
if ($file) {
|
||||
return App::make('url')->to('/') . '/storage' . $file;
|
||||
}
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
public function updateFile($file, $existingFile, $prefix, bool $isEncoded): bool|string
|
||||
{
|
||||
if ($existingFile) {
|
||||
$this->deleteFile($existingFile);
|
||||
}
|
||||
return $this->uploadFile($file, $prefix, $isEncoded);
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function deleteFile($file): bool
|
||||
{
|
||||
$file = 'storage' . $file;
|
||||
if (file_exists($file)) {
|
||||
unlink($file);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user