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()));base64_decode($image)? Did the first comment help you?