Converting images to Base64 encoding can be useful for various applications, such as storing images in databases or sending them over HTTP requests. To convert an image from a URL to Base64 encoding, follow these steps:
Example:
$url = 'https://example.com/myimage.png'; $path = 'myfolder/myimage.png'; file_put_contents($path, file_get_contents($url)); $type = pathinfo($path, PATHINFO_EXTENSION); $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data); echo $base64;
This code will download the image from the URL, save it to a local file, extract the image type, encode the image data to Base64, and output the data URI.
The above is the detailed content of How Can I Convert an Image URL to a Base64 String in PHP?. For more information, please follow other related articles on the PHP Chinese website!