我從 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?
}
收到后,我想將影像保存在我的公用檔案夾中,并將路徑保存在我的資料庫中。SO上有幾個我認為可以像使用干預/影像一樣作業的答案,但我無法解決這個問題。我還發現一條評論說這不能完成。有人可以給我一個提示,我該怎么做。或者如果這甚至是可能的?
謝謝你。
uj5u.com熱心網友回復:
如果你使用的是 Laravel,那么你可以使用StorageFacade 來幫助你將內容寫入private存盤或public存盤。如果影像已經是二進制的,只需按原樣放置/寫入,作為二進制。
根據檔案,您可以將影像存盤為示例:
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();
}
您可以使用Log外觀來記錄保存影像的結果。希望對您有所幫助,如果對您有所幫助,請將其設定為可接受的答案。謝謝。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/445448.html
