1

I'm trying to use kreait/firebase-php for storing base64 images on Firebase. Here I found a solution to upload base64 to Firebase but unfortunately I couldn't figure out how to this with the firebase-php library. How can I use it with the library?

Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48
she hates me
  • 1,212
  • 5
  • 25
  • 44

1 Answers1

1

I found how to do it. It seems just replacing "'data:image/jpeg;base64," and turning spaces to "+" and writing to a file directly is working fine.

Here is an example code.

$firebase = (new Firebase\Factory)
            ->withServiceAccount($serviceAccount)
            ->create();


$storage = $firebase->getStorage();
$bucket = $storage->getBucket();

$base64Img = $base64Img->encoded;  // your base64 encoded string
$base64Img = str_replace('data:image/jpeg;base64,', '', $base64Img);
$base64Img = str_replace(' ', '+', $base64Img);

$object = $bucket->upload(base64_decode($base64Img), [
   'name' => $imageName
]);
she hates me
  • 1,212
  • 5
  • 25
  • 44