first commit

This commit is contained in:
2025-11-06 06:55:15 +00:00
commit 0b603376d2
107 changed files with 17279 additions and 0 deletions

131
app/Traits/ImageHelper.php Normal file
View File

@@ -0,0 +1,131 @@
<?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;
// Direct paths without symlinks
public $uploads_path = "uploads/";
public $public_uploads_path = "public/uploads/";
public function uploadPDF($file, $prefix): string
{
$fileName = '';
if ($file) {
$extension = $file->getClientOriginalExtension();
$fileName = $prefix . '_' . Str::random(30) . '.' . $extension;
// Create directory in public folder if it doesn't exist
$uploadPath = public_path($this->uploads_path);
if (!file_exists($uploadPath)) {
mkdir($uploadPath, 0755, true);
}
// Move file directly to public directory
$file->move($uploadPath, $fileName);
}
return '/' . $this->uploads_path . $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;
// Create directory in public folder if it doesn't exist
$uploadPath = public_path($this->uploads_path);
if (!file_exists($uploadPath)) {
mkdir($uploadPath, 0755, true);
}
// Full path to save the file
$filePath = $uploadPath . $fileName;
// Process with Intervention Image
$img = Image::make($file);
// You can add image manipulation here if needed
// $img->resize(800, null, function ($constraint) {
// $constraint->aspectRatio();
// $constraint->upsize();
// });
// Save directly to public directory
$imagePath = $img->save($filePath) ? '/' . $this->uploads_path . $fileName : '';
}
return $imagePath;
}
public function create_file_from_base64($base64File): UploadedFile
{
$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
$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('/') . $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
{
// Remove leading slash if present
if (str_starts_with($file, '/')) {
$file = substr($file, 1);
}
$filePath = public_path($file);
if (file_exists($filePath)) {
unlink($filePath);
return true;
}
return false;
}
}