将base64图像字符串转换为IE兼容图像,仅限PHP

时间:2014-10-09 10:25:06

标签: php image base64

我正在一个网站上工作,我会在其中提供内容,并需要将其工作到一个有效的网站。

由于严格要求,我不允许依赖javascript,并且需要让网站在所有最常用的浏览器(和版本)中运行。这包括IE8(也可能更低)。

所传递的内容包括base64编码图像。这些图像随着嵌入其前面的图像信息一起传送。 (数据:image / png; base64,[图像数据])

然而,由于图像非常大,< IE8无法处理图像。我一直在与tutorial from this site(以及其他人)一起工作,但是,我到目前为止还没有。

TL; DR

如何仅使用PHP将base64编码的图像转换为工作格式?最好不必将图像存储在某处的服务器上。

1 个答案:

答案 0 :(得分:2)

您需要提取图像类型,以便将其用作内容类型,然后简单地解码图像数据:

一个简单的实现如下:

  $image = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAAEqCAMAAACxwopAAAABOFBMVEX///8AAAAAAACBgYUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACBgYUAAAAAAAAAAAAAAAAAAACBgYWmim6mim6mim7Siin2ih/AlFLSiinSiinSiinSiinSiin2ih/2ih/SiinAlFLAlFLAlFL2ih/AlFLAlFLAlFKmim7AlFL2ih/0eiD2ih/Siimmim72ih/AlFLAlFL2ih/AlFL0eiDAlFL2ih/AlFLSiin0eiDAlFLAlFLSiimmim6mim7Siin0eiCmim6BgYWmim7Siimmim72ih+mim7Siimmim70eiCmim6mim7Siin0eiDSiin0eiD2ih/0eiD0eiD0eiD2ih/2ih/2ih/0eiD0eiCmim6BgYX2ih/0eiD0eiCBgYX0eiDSiikAAACBgYWmim7AlFL0eiD2ih/atez0AAAAYXRSTlMAv0CAgO8Qn88wIGC/31CvcI9AQIC/7xDvEECAvyBAgN+/UCCvn2AQIN/fv49Q7zBAcM+PcIC/MJ9Az69g31CvgJ8gMM+v72BwcFDPj48QMI8gMM+vYHCf798QYFAgYBCfEQM3FQAAFuFJREFUeF7s3C1uxEAMx9HeJ3ylYUELgpoPVY3UjBY5979BWUFNlo713g1+9C/LH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UtU8r2cC/V9h5mnQPzrwP59vG/oGpjPUjGsYkvn5ul6paspYmZt3MvQgaGN+3UaneL0H/VFA/mpPGf5F8cHryw3oK8iUFd83C/9uBw4IAAAAEAT1f3VDhJUBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH1zxFFGhajFcAAAAASUVORK5CYII=";

  $data = explode(";", $image);
  $type = str_replace("data:", "", $data[0]);

  header("Content-type: ".$type);
  echo base64_decode(str_replace("base64,", "", $data[1]));

将输出stackoverflow的徽标。

然而,一些javascript会让事情变得更容易,因为如果你检测到Internet Explorer,你可以使用php脚本更改图像源“动态”。但是既然你说这不是一个选项,你应该在服务器端进行匹配。