简体   繁体   English

PHP如何从Base64生成图像?

[英]PHP How to generate image from Base64?

Hello I used to store my images as data. 您好我曾经将我的图像存储为数据。 It looked something like "‰PNG IHDRÂæÉ0†" . 它看起来像"‰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. 该代码位于一个名为generateThumbnailXL.php的文件中,我将它放在图像的src中,图像将完美生成。 I switched to using Base64 image data recently and I just put the base64 data into the src and the image is generated perfectly. 我最近切换到使用Base64图像数据,我只是将base64数据放入src中,图像生成完美。 The problem is when someone shares my article on Facebook, the thumbnail doesn't show up because Facebook doesn't like using Base64. 问题是当有人在Facebook上分享我的文章时,缩略图没有显示,因为Facebook不喜欢使用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. 我需要一些方法将base64转换成类似"‰PNG IHDRÂæÉ0†" ,然后像上面的代码一样回应它。 How can I do this? 我怎样才能做到这一点? I researched on Google and found little help, nothing that will work for me. 我在Google上进行了研究并找不到任何帮助,没有任何对我有用的东西。

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 . 你会看到图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM