【问题标题】:Display Image in Php From Byte String从字节字符串以 PHP 显示图像
【发布时间】:2018-08-06 15:24:32
【问题描述】:

因为我的英语能力不够,

请事先确认

使用 MongoDB (GridFS),带来图像。

$bucket = $this->mDB->selectGridFSBucket([
               'bucketName' => $collection,
               'readPreference' => new MongoDB\Driver\ReadPreference(MongoDB\Driver\ReadPreference::RP_SECONDARY),
            ]);

$stream = $bucket->openDownloadStream($id);
$im = stream_get_contents ( $stream );
header('Content-Type: image/png');
echo $im;

例如:https://docs.mongodb.com/php-library/v1.2/tutorial/gridfs/

但是,很难将其输出为图像。

我该怎么办?

remove header('Content-Type: image/png')

added header('Content-Type: image/png')

【问题讨论】:

  • 你可以试试imagecreatefromstring( $im );
  • @RamRaider 结果是 i.stack.imgur.com/H9o0v.png , url : 13.124.109.93/httpapi/get.cover.thumbnail.image.php?query=2

标签: php mongodb


【解决方案1】:

完全未经测试,但鉴于图像中显示的数据的性质,您可以尝试使用imagecreatefromstring - 类似以下内容:

$bucket = $this->mDB->selectGridFSBucket([
               'bucketName' => $collection,
               'readPreference' => new MongoDB\Driver\ReadPreference( MongoDB\Driver\ReadPreference::RP_SECONDARY ),
            ]);

$stream = $bucket->openDownloadStream( $id );
$im = imagecreatefromstring( stream_get_contents ( $stream ) ) ;
if( $im ){
    header( 'Content-Type: image/png' );
    imagepng( $im );
    imagedestroy( $im );
}

你可以在流数据上尝试base64_decode(我真的不知道流的内容是什么)

$im = imagecreatefromstring( base64_decode( stream_get_contents ( $stream ) )  );
if( $im ){
    header( 'Content-Type: image/png' );
    imagepng( $im );
    imagedestroy( $im );
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-09-13
  • 2022-01-13
  • 1970-01-01
  • 2021-03-13
  • 1970-01-01
  • 2021-08-06
  • 2017-01-13
相关资源
最近更新 更多