PHP code example of fuwasegu / php-base64-image

1. Go to this page and download the library: Download fuwasegu/php-base64-image library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fuwasegu / php-base64-image example snippets




declare(strict_types=1);

namespace App\Http\Controllers;

use Fuwasegu\PhpBase64Image\Image;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Str;

class ImageSaveController
{
    public function __invoke(Request $request): JsonResponse
    {
        // Base64 encoded image data uri
        $dataUri = $request->input('image');
        
        // Create Image object from base64 image data uri
        $image = Image::fromBase64($dataUri);
        
        // Random string for image name based on uuid
        $uuid = Str::uuid();
        
        // Upload to S3 using Storage facade
        Storage::put(
            path: "images/{$uuid}.{$image->extension()->value}",
            contents: $image->data(),
            options: 'private',
        );
        
        return new JsonResponse(['message' => 'success'], 200);
    }
}
shell
composer