Home > Mobile >  Binary image from API, convert and save path
Binary image from API, convert and save path

Time:03-12

Im receiving a binary image from an API (Line API)

// if status 200
if ($response->isSucceeded()) {
    // get binary image from api
    $binary = $response->getRawBody();
    // convert to base64?
    $imageData = base64_encode($binary);
    // what do i do here?
}

After receiving it, I want to save the image in my public folder and save the path in my database. There were several answers on SO that I thought could work like using intervention/image but I could not manage to fix this. Ive also found a comment saying this cant be done. Can someone give me a hint how I could do this. Or if this is even possible?

Thank you.

CodePudding user response:

If you are using Laravel, then you can use Stroge facade to help you write the content into private storage or public storage. If the image already on binary, just put/write as is, as binary.

Based on the docs, you can store the image as example :

use Illuminate\Support\Facades\Storage;
// ..... controller
// if status 200
if ($response->isSucceeded()) {
    // get binary image from api
    $binary = $response->getRawBody();
    Storage::disk("public")->put("/img/some-image.jpg",$binary);
    // you can echo the URL, and return it to your API or show on web
    echo url("storage/img/some-image.jpg"); 
    // or you can write to DB
    // Example Model, change to anything
    $imgTable =  new TableImage();
    $imgTable->url = url("storage/img/some-image.jpg");
    $imgTable->save();
}

You can use Log facade to log the result of the saving image. Hope this help, and if it help you, please set it as acceptable answer. thanks.

  • Related