Convert image to base 64 string with Laravel
Asked Answered
A

4

21

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=

Antipathetic answered 13/9, 2017 at 21:18 Comment(12)
$request->file() doesn't return the actual file content but an instance of UploadedFile. You need to load the actual file to convert it. Try: $image = base64_encode(file_get_contents($request->file('image')->path()));Unjust
please to make the inverse operation? base64 to imageAntipathetic
base64_decode($image)? Did the first comment help you?Unjust
for the first i think this work base64_encode(file_get_contents($request->file('image')));Antipathetic
now i want to decode and save but this dont work file(base64_decode($image))->move("images", $name);Antipathetic
I've added my comment as an answer so you can accept it, since it solved your main question.Unjust
I'm not sure what you're trying to do. Your variable $image contains the image data base64 encoded. If you decode it, it's just binary data, not a PHP-class.Unjust
this workerd file_put_contents("images/".$name, base64_decode($image));Antipathetic
Yes, you're simply storing the binary data as a file. Not sure why you needed to base64 encode it first, though. You could just have used the ->move()-method on the original UploadedFile-class to store it as a file.Unjust
i an sending it to an external API with guzzleAntipathetic
Ah. ok. But since my answer actually did solve your initial question, please accept the answer below so other SO members knows that this question has been answered.Unjust
In Laravel base64_encode(file_get_contents($request->file('image')->getRealPath())) this works too.Monopetalous
U
45

Laravel's $request->file() doesn't return the actual file content. It returns an instance of the UploadedFile-class.

You need to load the actual file to be able to convert it:

$image = base64_encode(file_get_contents($request->file('image')->pat‌​h()));
Unjust answered 13/9, 2017 at 21:41 Comment(2)
what is the purpose of this one? I just want to know I am little bit confused why you need to convert it?Pasteboard
@Pasteboard they might want to store it in the database as a string, just as one exampleBiogen
C
26

It worked for me in the following way:

$image = base64_encode(file_get_contents($request->file('image')));

I eliminated this part ->pat‌​h();

Carcinoma answered 10/10, 2018 at 23:28 Comment(1)
When I tried this, I got this error : "file_get_contents(): Filename cannot be empty".Belicia
C
3

Don't forget to append file type.

Example for pdf :

$file = "data:@file/pdf;base64,".base64_encode(file_get_contents($request->file('image')));

Example for image :

$file = "data:image/png;base64,".base64_encode(file_get_contents($request->file('image')));
Chancellorsville answered 8/1, 2023 at 5:47 Comment(0)
G
0
//html code
<input type="file" name="EmployeeImage">

//laravel controller or PHP code
$file = $request->file('EmployeeImage');
$image = base64_encode(file_get_contents($file)); 

// display base64 image
echo '<img src="data:image/png;base64,' . $image . '" />'; exit();
Gallicism answered 6/7, 2023 at 7:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.