Sample code for 30+ languages & platforms
PHP

Download Image (JPG, GIF, etc.) to Base64

See more HTTP Examples

Demonstrates how to download an image, or any type of file, to get the data in base64 encoding format.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\BinData;
use Chilkat2\Http;

$success = false;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$http = new Http();
$http->KeepResponseBody = true;

$bd = new BinData();

try {
    $http->DownloadBd('https://www.chilkatsoft.com/images/starfish.jpg', $bd);
    $success = true;
} catch (\Chilkat2\Exception $e) {
    $success = false;
}
$statusCode = $http->LastStatus;
if (!$success) {
    if ($statusCode == 0) {
        // Unable to either send the request or get the response.
        echo $http->LastErrorText, "\n";
    } else {
        // We got a response, but the status code was not in the 200s
        echo 'Response status code: ', $statusCode, "\n";
        // Examine the response body.
        echo "Response body:\n";
        echo $http->LastResponseBody, "\n";
    }

    echo "Download failed.\n";
} else {
    echo 'Download success, response status = ', $statusCode, "\n";

    $base64_image_data = $bd->GetEncoded('base64');
    echo "image data in base64 format:\n";
    echo $base64_image_data, "\n";
}