dongxindu8753 2016-04-11 20:09
浏览 81

将base64字符串解码为azure存储上的jpeg

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条回答 默认 最新

  • dousao6260 2016-04-12 05:55
    关注

    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 />";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?