php - 使用 PHP 不显示图像
问题描述
我正在尝试display
从database
. 以下query
工作正常,但问题image
不再显示。
<?php
session_start();
// Include config file
require_once "../auth/dbconnection.php";
$output='';
$sql='select `image`,`title`,`sub_title`,`username`,`blog_id`,`body`,`published` from `blog` WHERE user_id=? ';
$stmt=$conn->prepare( $sql );
$stmt->bind_param( 's',$_SESSION['user_id'] );
$res=$stmt->execute();
if( $res ){
$stmt->store_result();
$stmt->bind_result($image,$title,$sub_title, $username, $blog_id,$body,$published);
while( $stmt->fetch() ){
$filepath="../assets/img/blog_images/";
$title= substr($title,0,30);
$body= substr($body,0,500);
$date= date('dS F Y', strtotime($published));
$output .= '
<div class="col-sm-6 col-md-6 col-lg-4">
<div class="blog grid-blog">
<div class="blog-image">
<a href="#">';
$output .= ' <img style="height:190px; width:330px;" class="img-fluid" src="data:image/png;base64, %s" alt="" />'; base64_encode(file_get_contents($filepath.$image) ) ;
$output .= ' </a>
</div>
<div class="blog-content">
<h3 class="blog-title"><a href="blog-details.html"> '.$title.' </a></h3>
<p> <code> '.$body.' </code> </p> <br>
<a href="blog-details.html" class="read-more"><i class="fa fa-long-arrow-right"></i> Read More</a>
<div class="blog-info clearfix">
<div class="post-left">
<ul>
<li><a href="#."><i class="fa fa-calendar"></i> <span>'.$date.'</span></a></li>
</ul>
</div>
<div class="post-right"><a href="#."><i class="fa fa-heart-o"></i>21</a> <a href="#."><i class="fa fa-eye"></i>8</a> <a href="#."><i class="fa fa-comment-o"></i>17</a></div>
</div>
</div>
</div>
</div>';
}
echo $output;
}else{
echo 'No any post found';
}
?>
谁能指导我如何解决这个问题,如果有人指导我解决这个问题,我将不胜感激。谢谢你。
解决方案
我假设您省略了sprintf
功能。这应该有效。
sprintf('<img src="data:image/png;base64, %s" alt="" />', base64_encode(file_get_contents($filepath.$image));
推荐阅读
- json - 在 SAPUI5 上工作时的 JSON 数据问题
- python - 如果一个条目为空,则删除行/列
- html - 为什么我的样式表适用于我的所有 ejs 文件?
- spring-boot - 完成 gradle build 后,在单个项目的多个模块中编写测试用例不会构建
- php - 在 for 循环中执行一次值
- html - 做什么
我知道这个标签指定了你链接到的文件类型(通常是“text/css”),但为什么我们需要添加它?
在依赖注入中使用相同接口在容器中注册两个类
- dependency-injection - 在依赖注入中使用相同接口在容器中注册两个类
- javascript - 为什么我不能使用模块?
- android - Apache POI 的 XSSFWorkbook 单元格的样式属性显示编译错误
- c# - 调试器要求 datetime.cs 源,尽管它不应基于解决方案属性