通过 PHP 显示数据库中的 Base64 图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16262098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:48:05  来源:igfitidea点击:

Displaying a Base64 images from a database via PHP

php

提问by Kosmas Papadatos

I have this database that contains images as strings. Those strings look something like this:

我有这个包含图像作为字符串的数据库。这些字符串看起来像这样:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD...

I need to create a link that will display this image. Like something.com/?id=27is an image. All the images are in jpeg format. Here's what I've tried but didn't work:

我需要创建一个链接来显示此图像。就像something.com/?id=27是一个图像。所有图片均为jpeg格式。这是我尝试过但没有奏效的方法:

<?php
  $host = "smth";
  $user = "smth";
  $pass = "smth";
  $db_name = "smth";
  $dbh = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass);
  $dbh->exec("SET NAMES utf8");
  $q = $dbh->prepare("select content from img where id = :id");
  $q->execute(array(':id'=>$_GET['id']));
  $row = $q->fetch(PDO::FETCH_BOTH);
  header("Content-type: image/jpeg");
  echo $row['content'];
?>

The data is being fetched correctly but the image is not displayed.

正在正确获取数据,但未显示图像。

I need to be able to use this link like this <img src="mysite.com?id=21" />and I do NOT want this solution: <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABA..." />

我需要能够像这样使用这个链接<img src="mysite.com?id=21" />,我不想要这个解决方案:<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABA..." />

Thanks!

谢谢!

回答by Menios

The solution to your problem is here:

您的问题的解决方案在这里:

How to decode a base64 string (gif) into image in PHP / HTML

如何在 PHP/HTML 中将 base64 字符串 (gif) 解码为图像

Quoting that source but modifying:

引用该来源但修改:

In the case you strip out the first case and choose to decode the string, you should add this before echoing the decoded image data:

如果您去掉第一种情况并选择解码字符串,您应该在回显解码的图像数据之前添加它:

header("Content-type: image/gif");
$data = "/9j/4AAQSkZJRgABAQEAYABgAAD........";
echo base64_decode($data);

In the second case, use this instead:

在第二种情况下,请改用它:

echo '<img src="data:image/gif;base64,' . $data . '" />';

The second case is bad because the browser does not perform caching if the same image is shown on multiple pages.

第二种情况很糟糕,因为如果在多个页面上显示相同的图像,浏览器不会执行缓存。

回答by Ashkan Arefi

Use this:

用这个:

$code_base64 = $row['content'];
$code_base64 = str_replace('data:image/jpeg;base64,','',$code_base64);
$code_binary = base64_decode($code_base64);
$image= imagecreatefromstring($code_binary);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);

回答by Awais Ahmed Khan

try this

尝试这个

//your image data

$logodata = "/9j/4AAQSkZJRgABAQEAYABgAAD........";
echo '<img src="data:image/gif;base64,' . $logodata . '" />';

回答by Mahbub Alam

Try this:

尝试这个:

echo '<img src="data:image/png;base64,' . $base64encodedString . '" />

回答by Ryan Horace

/**
* @param $base64_image_content 
* @param $path 
* @return bool|string
*/
function base64_image_content($base64_image_content,$path){
  if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
    $type = $result[2];
    $new_file = $path."/".date('Ymd',time())."/";
    $basePutUrl = C('UPLOAD_IMG_BASE64_URL').$new_file;

    if(!file_exists($basePutUrl)){
        //Check if there is a folder, if not, create it and grant the highest authority.
       mkdir($basePutUrl, 0700);
    }
       $ping_url = genRandomString(8).time().".{$type}";
       $ftp_image_upload_url = $new_file.$ping_url;
       $local_file_url = $basePutUrl.$ping_url;

   if (file_put_contents($local_file_url, base64_decode(str_replace($result[1], '', $base64_image_content)))){
     ftp_upload(C('REMOTE_ROOT').$ftp_image_upload_url,$local_file_url);
         return $ftp_image_upload_url;
    }else{
         return false;
    }
  }else{
     return false;
 }
}