fuwasegu / php-base64-image
PHP中处理base64编码图片的库
1.1.0
2023-03-15 00:42 UTC
Requires
- php: ^8.1
- ext-fileinfo: *
Requires (Dev)
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.5
- yumemi-inc/php-cs-fixer-config: ^8.1
This package is auto-updated.
Last update: 2024-09-15 04:00:27 UTC
README
PHP中处理base64编码图片的库
📦 安装
composer require fuwasegu/php-base64-image
✅ 使用
假设你在一个Laravel项目中使用此包,你可以这样使用它
💡提示
当然,这个库不依赖于Laravel,因此也可以用于纯PHP项目。
<?php 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); } }