Sample code for 30+ languages & platforms
PHP Extension

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 Extension Downloads

PHP Extension
<?php

include("chilkat.php");

$success = false;

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

$http = new CkHttp();
$http->put_KeepResponseBody(true);

$bd = new CkBinData();

$success = $http->DownloadBd('https://www.chilkatsoft.com/images/starfish.jpg',$bd);
$statusCode = $http->get_LastStatus();
if ($success == false) {
    if ($statusCode == 0) {
        // Unable to either send the request or get the response.
        print $http->lastErrorText() . "\n";
    }
    else {
        // We got a response, but the status code was not in the 200s
        print 'Response status code: ' . $statusCode . "\n";
        // Examine the response body.
        print 'Response body:' . "\n";
        print $http->lastResponseBody() . "\n";
    }

    print 'Download failed.' . "\n";

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

    $base64_image_data = $bd->getEncoded('base64');
    print 'image data in base64 format:' . "\n";
    print $base64_image_data . "\n";
}


?>