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

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型