John Davidson

php - Binary image from API, convert and save path


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.

My Answer:
>getStatusCode() == 200) {
$imageData = $response->getBody();

// Convert binary image to base64
$base64Image = base64_encode($imageData);

// Save base64 image to file
$filePath = 'path/to/save/image.jpg';
file_put_contents($filePath, base64_decode($base64Image));

echo 'Image saved successfully at ' . $filePath;
} else {
echo 'Failed to retrieve image from API';

Rate this post

3 of 5 based on 8672 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us