首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP -包含二进制数据的Pack数组

PHP -包含二进制数据的Pack数组
EN

Stack Overflow用户
提问于 2019-05-27 23:17:55
回答 2查看 335关注 0票数 0

有一个服务(php)可以将图像发送到客户端(php)。

代码语言:javascript
复制
header('Content-Type: image/png');
readfile($image);

如果不仅需要发送图像,还需要发送一些数据,该怎么办?

代码语言:javascript
复制
$arrayToSend = [
    'image' => file_get_contents($image),
    'some_data' => [
        'a' => 1,
        'b' => 2
    ]
];

服务如何打包$arrayToSend以便客户端可以将其解包?

而不将图像转换为base64 (因为尺寸太大)。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-05-28 02:00:07

如果你通过HTTP通信,@Danon的报头方法可能是可行的,但其他传输可能不支持发送额外的报头,所以你需要用二进制数据打包它们,然后在接收端解包。

代码语言:javascript
复制
<?php
class Codec
{
    /**
     * Pack metadata along with binary data
     *
     * @param $meta
     * @param $data
     * @return false|string
     */
    public static function encode($meta, $data)
    {
        $meta = base64_encode($meta);

        //determine length of metadata
        $metaLength = strlen($meta);

        //The first part of the message is the metadata length
        $output = pack('VA*', $metaLength, $meta);

        //Length and metadata are set, now include the binary data
        $output .= $data;

        return $output;
    }

    /**
     * Unpack data encoded via the encode function.
     * Returns an array with "meta" and "data" elaments
     *
     * @param $content
     * @return array
     */
    public static function decode($content)
    {
        //Get the length of the metadata content
        $metaLength = unpack('V', $content)[1];

        //Slice out the metatdata, offset 4 to account for the length bytes
        $metaPacked = substr($content, 4, $metaLength);

        //Unpack and base64 decode the metadata
        $meta = unpack('A*', $metaPacked)[1];
        $meta = base64_decode($meta);

        //The binary data is everything after the metadata
        $data = substr($content, $metaLength+4);

        return [
            'meta' => $meta,
            'data' => $data
        ];
    }
}

//Load contents of a binary file
$imageFilePath = 'path_to_image.png';
$data = file_get_contents($imageFilePath);

//Arbitrary metadata - could be anything, let's use JSON
$meta = [
    'filename' => 'foo.png',
    'uid' => 12345,
    'md5' => md5_file($imageFilePath)
];

$metaJson = json_encode($meta);

//Encode the message, you can then send this to the receiver
$payload = Codec::encode($metaJson, $data);

//Receiver decodes the message
$result = Codec::decode($payload);

//Decode our JSON string
$resultMeta = json_decode($result['meta'], true);


echo 'Filename: '.$resultMeta['filename'].PHP_EOL;
echo 'UID: '.$resultMeta['uid'].PHP_EOL;

//We included an MD5 hash of the file, so we can verify here
if($resultMeta['md5'] != md5($result['data']))
{
    echo 'MD5 mismatch!';
}
票数 1
EN

Stack Overflow用户

发布于 2019-05-27 23:56:04

也许您可以将some_data-asome_data-b作为头文件进行传递?例如,作为cookie。

您可以在此文档中自助:https://developer.mozilla.org/pl/docs/Web/HTTP/Headers,以了解有关头部的更多信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56328719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档