【问题标题】:PHP View Picture From Base64 StringPHP从Base64字符串查看图片
【发布时间】:2011-12-01 02:50:16
【问题描述】:

我正在连接到 Active-Directory 并成功获取 thumbnailPhoto 属性。

我已使用 Base64 编码将文件存储在数据库中,结果如下所示:

/9j/4AAQSkZJRgABAQEAYABgAAD/4RHoRXhpZgAATU0AKgAAAAgABQEyAAIAAAAUAA ...

(完整的 Base64 编码字符串:http://pastebin.com/zn2wDEmd

使用a simple Base64 Decoder 并将字符串解码为二进制文件并将其重命名为 jpeg 并使用图像查看器打开(此处:Irfan View)我得到正确的图片 - 看看你自己:

如何通过 PHP 实现这一点 - 我尝试过使用:

<?php 

$data = '/9j/4A...'; //The entire base64 string - gives an error in dreamweaver

$data = base64_decode($data);

$fileTmp = imagecreatefromstring($data);

$newImage = imagecreatefromjpeg($fileTmp);

if (!$newImage) {
    echo("<img src=".$newImage."/>");
} 

?>

我只是得到一个空白页!

【问题讨论】:

标签: php base64 gd image


【解决方案1】:

您的问题是 imagecreatefromstring() 不返回文件,而是返回内存中的图像,应该使用正确的标题输出。

$data = base64_decode($data);

// Create image resource from your data string
$imgdata = imagecreatefromstring($data);

if ($imgdata) {
  // Send JPEG headers
  header("Content-type: image/jpeg");
  // Output the image data
  imagejpeg($imgdata);

  // Clean up the resource
  imagedestroy($imgdata);
  exit();
}

【讨论】:

  • 这里 $imgdata 是资源,我们可以应用 base64_decode 来获取我想要的资源。
  • @yourkishore 不清楚你在问什么。你有$imgdata 作为GD 图像资源?您不能直接对其进行 base64 编码 - 您需要从中返回二进制字符串进行编码。针对您的具体问题发布问题。
  • 它的明确伴侣。我的问题是 imagecopymerge(stuff) 什么时候会返回资源?我们如何将该资源转换为 base64_decode。
  • 啊,我认为you want this then 使用imagejpeg()(或任何格式)结合PHP 的输出缓冲来捕获二进制字符串数据,然后base64_encode() 它。 base64_decode() 没有任何内容,因为它没有被编码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 2019-06-11
  • 1970-01-01
  • 2014-02-14
  • 2011-08-29
  • 2014-10-18
相关资源
最近更新 更多