Last active
February 17, 2023 09:13
-
-
Save cybernerdie/a984e95fa39fc8f1fc1f7e274b5f7dec to your computer and use it in GitHub Desktop.
Upload image to cloudinary in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Http; | |
class CloudinaryController extends Controller | |
public function uploadImageToCloudinary(Request $request) | |
{ | |
$file = $request->file('image'); | |
$cloudName = 'your_cloud_name'; | |
$uploadPreset = 'your_upload_preset'; | |
$url = 'http://api.cloudinary.com/v1_1/' . $cloudName . '/image/upload?upload_preset=' . $uploadPreset; | |
$filePath = $file->getRealPath(); | |
$base64 = base64_encode(file_get_contents($filePath)); | |
$response = Http::post($url, [ | |
'file' => "data:{$file->getClientMimeType()};base64,{$base64}", | |
'multiple' => true, | |
]); | |
return $response->body(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment