0
votes

I have a php script that takes a jpeg received within a String that is a byte array encoded to base64. I don't know what is the correct header options to send to azure and tell it to decode and write the result to a valid jpg file,instead of writing the string to the file, which it does correctly at this moment. I have tried several content types. (image/jpeg). Even sending the raw byte array data. Nothing. Azure writes the file but not correctly as a jpeg.

$contentType = "text/plain; charset=UTF-8";

$curl = curl_init($base_url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
        'Content-Type: '.$contentType,
        'Content-Encoding : BASE64',
        'x-ms-version: 2014-02-14',
        'x-ms-date: '.$currentTime,
        'x-ms-blob-type: BlockBlob',
        'x-ms-blob-content-type: '.$contentType,
        'Authorization: SharedKey zzzzzstorage:'.$signature
    )); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $encodedImage);

$response = curl_exec($curl);
1
Are you sure that the program receiving your file is coded to do this conversion? Or is it expecting to be sent a proper jpgRiggsFolly
msdn.microsoft.com/en-us/library/azure/dd179451.aspx . There are php sdks and .net and js and many libraries that have methods like uploadBlobFromText that allow a string to be written to a jpg. But I can't use the php sdk in this. I'm looking for the right combination of headers to make the api perform a decode on the base64 string.nvasilescu
I dont see anything in that documentation that leads me to believe that API will do this conversion of you. Why dont you do it and then send an actual .jpg $the_jpg = base64_decode($encodedImage);RiggsFolly

1 Answers

1
votes

You can combine the base64 encoded image array your get to a base64 encoded string, and decode the string to the image content, then upload to Azure Storage via Azure PHP SDK.

Here is the code snippet:

require_once 'vendor/autoload.php';
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Common\ServiceException;
use WindowsAzure\Blob\Models\CreateBlobOptions;
$image_string = 'base64 encoded string, not begin with `data:image/jpeg;base64,...`';
$imgdata = base64_decode($image_string);
//get file type from decoded content
$f = finfo_open();
$mime_type = finfo_buffer($f, $imgdata, FILEINFO_MIME_TYPE);
$connectionString = "your_storage_account_connection_string";
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
$blob_name = 'base64image';
$container_name = 'container_name';
$option = new CreateBlobOptions();
$option->setContentType($mime_type);
try{
    $blobRestProxy->createBlockBlob($container_name, $blob_name, $imgdata,$option);
}
catch(Exception $e){
    echo "Error <br />";
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br />";
    echo "content " . $imgdata."<br />";
}