duankan6894 2017-04-21 10:10
浏览 81

PHP如何从Base64生成图像?

Hello I used to store my images as data. It looked something like "‰PNG IHDRÂæÉ0†". I was able to generate images from this data using the following code:

include_once "mysql_connect.php";
if(isset($_GET['id'])){
$id = mysql_real_escape_string($_GET['id']);
$query = mysql_query("SELECT * FROM `entree` WHERE `realID` = '$id'");

while($row = mysql_fetch_assoc($query)){
    $imageData = $row["thumbXL"];
    $imageType = $row["imageext"];
}

if($imageType == "image/png"){
    header("content-type: image/png");
}else if($imageType == "image/jpeg"){
    header("content-type: image/jpeg");
}else{}

if(!($imageData == null)){
echo $imageData;
}else{
}


}else{

}

That code was in a file called generateThumbnailXL.php and I would put that in the src of the images and the image would be generated perfectly. I switched to using Base64 image data recently and I just put the base64 data into the src and the image is generated perfectly. The problem is when someone shares my article on Facebook, the thumbnail doesn't show up because Facebook doesn't like using Base64. I need some way of converting base64 into something like "‰PNG IHDRÂæÉ0†" and then echo it out like the way the above code did. How can I do this? I researched on Google and found little help, nothing that will work for me.

  • 写回答

1条回答 默认 最新

  • doumi2106 2017-04-21 10:18
    关注

    Let say you have something like this :-

    <?php
    
    // $imgData contains base64 encoded data of image 
    $imgData = <<< EOFILE
    R0lGODlhogBxAPcAA......0zEREAA7
    EOFILE;
    
    header("Content-type: image/png");
    echo base64_decode($imgData);
    exit();
    

    and you will see image .

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名