I want to convert an image to base 64 with Laravel. I get the image from a form . I tried this in my controller:
public function newEvent(Request $request){
$parametre =$request->all();
if ($request->hasFile('image')) {
if($request->file('image')->isValid()) {
try {
$file = $request->file('image');
$image = base64_encode($file);
echo $image;
} catch (FileNotFoundException $e) {
echo "catch";
}
}
}
I get this only:
L3RtcC9waHBya0NqQlQ=
$request->file()
doesn't return the actual file content but an instance ofUploadedFile
. You need to load the actual file to convert it. Try:$image = base64_encode(file_get_contents($request->file('image')->path()));
– Unjustbase64_decode($image)
? Did the first comment help you? – Unjust$image
contains the image data base64 encoded. If you decode it, it's just binary data, not a PHP-class. – Unjust->move()
-method on the original UploadedFile-class to store it as a file. – Unjustbase64_encode(file_get_contents($request->file('image')->getRealPath()))
this works too. – Monopetalous