|
@@ -2,20 +2,21 @@
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
namespace App\Traits;
|
|
|
|
|
|
|
|
|
|
+use Exception;
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
trait UploadsBase64Image
|
|
trait UploadsBase64Image
|
|
|
{
|
|
{
|
|
|
/**
|
|
/**
|
|
|
- * Upload a base64 encoded image to S3 and return the URL.
|
|
|
|
|
|
|
+ * Upload a base64 encoded image and return the URL.
|
|
|
*
|
|
*
|
|
|
* @param string $base64Image
|
|
* @param string $base64Image
|
|
|
- * @param string $s3Folder
|
|
|
|
|
|
|
+ * @param string $folder
|
|
|
* @return string
|
|
* @return string
|
|
|
* @throws \Exception
|
|
* @throws \Exception
|
|
|
*/
|
|
*/
|
|
|
- public function uploadBase64Image(string $base64Image, string $s3Folder): string
|
|
|
|
|
|
|
+ public function uploadBase64Image(string $base64Image, string $folder): string
|
|
|
{
|
|
{
|
|
|
// Extract the file extension and base64 data
|
|
// Extract the file extension and base64 data
|
|
|
if (preg_match('/^data:image\/(\w+);base64,/', $base64Image, $type)) {
|
|
if (preg_match('/^data:image\/(\w+);base64,/', $base64Image, $type)) {
|
|
@@ -23,28 +24,28 @@ public function uploadBase64Image(string $base64Image, string $s3Folder): string
|
|
|
$type = strtolower($type[1]); // jpg, png, gif
|
|
$type = strtolower($type[1]); // jpg, png, gif
|
|
|
|
|
|
|
|
if (!in_array($type, ['jpg', 'jpeg', 'png', 'gif'])) {
|
|
if (!in_array($type, ['jpg', 'jpeg', 'png', 'gif'])) {
|
|
|
- throw new \Exception(__('validation.extensions', ['attribute' => __('general.image'), 'values' => 'jpg, jpeg, png, gif']));
|
|
|
|
|
|
|
+ throw new Exception(__('validation.extensions', ['attribute' => __('general.image'), 'values' => 'jpg, jpeg, png, gif']));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$image = base64_decode($base64Image);
|
|
$image = base64_decode($base64Image);
|
|
|
|
|
|
|
|
if ($image === false) {
|
|
if ($image === false) {
|
|
|
- throw new \Exception('Base64 decode failed');
|
|
|
|
|
|
|
+ throw new Exception('Base64 decode failed');
|
|
|
}
|
|
}
|
|
|
} else {
|
|
} else {
|
|
|
- throw new \Exception('Invalid base64 image format');
|
|
|
|
|
|
|
+ throw new Exception('Invalid base64 image format');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Generate a unique filename
|
|
// Generate a unique filename
|
|
|
$filename = Str::random(20) . '.' . $type;
|
|
$filename = Str::random(20) . '.' . $type;
|
|
|
|
|
|
|
|
// Define the full path where the file will be stored
|
|
// Define the full path where the file will be stored
|
|
|
- $filePath = $s3Folder . '/' . $filename;
|
|
|
|
|
|
|
+ $filePath = $folder . '/' . $filename;
|
|
|
|
|
|
|
|
- // Save the file to S3
|
|
|
|
|
- Storage::disk('s3')->put($filePath, $image);
|
|
|
|
|
|
|
+ // Save the file
|
|
|
|
|
+ Storage::put($filePath, $image);
|
|
|
|
|
|
|
|
// Return the URL of the uploaded image
|
|
// Return the URL of the uploaded image
|
|
|
- return Storage::disk('s3')->url($filePath);
|
|
|
|
|
|
|
+ return Storage::url($filePath);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|