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 .

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教