0

Is there anyway to convert image url to base64. my current code looks like this.

$path = 'https://projectstaging.s3.ap-southeast-2.amazonaws.com/2ade1776f74aa967de6578bbbceca692c274aced.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

I don't want to use file_get_contents as it blocks when there is too many request and this fails. Any better alternative. Thanks

saj sta
  • 95
  • 7

1 Answers1

1

Can you try this

function convertImagetoBase64($url)
{
  $urlParts = pathinfo($url);
    $extension = $urlParts['extension'];
  
  $base64 = 'data:image/' . $extension . ';base64,' . base64_encode(\Illuminate\Support\Facades\Http::get($url)->body());
    return $base64;
}

$url = 'snapformsstaging.s3.ap-southeast-2.amazonaws.com/80f1d508b80a16f7b114009c62a2794ff45a84b6.png';

$base64Txt = (convertImagetoBase64($url));

Live Demo here

ManojKiran A
  • 5,896
  • 4
  • 30
  • 43