【问题标题】:How to define an image from the database stored in binary as a div background image?如何将存储在二进制中的数据库中的图像定义为 div 背景图像?
【发布时间】:2020-03-25 22:30:24
【问题描述】:

我有一张以二进制形式存储在数据库中的图像。我已经拯救了这张图片(通过选择),并希望将其作为 div 的背景图片插入。我只能将它作为 img 的 src 输入。

如何在数据库中存储:

<?php

$foto = $_FILES['image']['name'];

if($this->__get('image') != "")
{
		$binary = file_get_contents($this->__get('foto')); 
		$query = "UPDATE usuarios SET image = :image, name = :name" ;
		$query .= "WHERE id = :id";
				
		$stmt = $this->db->prepare($query);
		$stmt->bindValue(':image', $binary);
		$stmt->bindValue(':name', $this->__get('name'));	
		$stmt->bindValue(':id', $this->__get('id'));
		$stmt->execute();		
}

?>

我作为img 的src 输入的部分。 ( 它正在工作)

<?php
						
function dataURI($bin)
{
		return 'data: image/gif;base64,'.base64_encode( $bin );
}				
					
?>

  <div class="row mt-2">
    <div class="col-lg-12 text-center" id="div-foto-usuario">
      <?php								
      if(!empty($this->view->info_usuario['image']))
      {										

			  $image = dataURI($this->view->info_usuario['image']);
			
			  echo "<img id='img-image-user' class='mr-2 border border-secondary' src='$image' style='border-radius:50%;width:120px; height:120px;'>";				
		  }
		  else
		  {
			  echo "<img id='img-image-user' class='mr-2 border border-secondary' src='/img/user.jpg' style='border-radius:50%;width:120px; height:120px;'>";
		  }
    ?>
    </div>
  </div>

我想在下面的div背景图片中插入:(问题)

<div class="row">
  <div class="col-lg-12" style="background-image: url(<?php echo $foto;?>);">
  </div>
</div>
  • 我尝试在url()中回显包含图片的变量,但没有成功。

  • 生成的页面 HTML 代码:Fiddle

  • 我是巴西人。我不会说英语。我用的是谷歌翻译。

【问题讨论】:

  • 注意还有pt.stackoverflow.com
  • @miken32 ,我知道。我在网站上没有找到葡萄牙语的答案。
  • 好的,我只是想确保您了解它,以防您更熟悉母语。很高兴您在这里找到了答案!

标签: php html mysql


【解决方案1】:

使用

<div id="postimave" style="background-image: url('<?php echo $image; ?>')">Some text
</div>

【讨论】:

  • 哎呀;p postimave
  • @nbk,没用。我想我已经在 dataURI ($ bin) 函数中做到了。如问题中所述。正确吗?
  • 是的,您使用 dataURI 函数生成了 $image 并且您可以在 url() 中使用此 $image。如第一个代码所示。剩下的就是 mor 作为解释,如果其他人搜索的话。
  • @nbk,我设法解决了这个问题。我忘了在问题中添加一些信息。我现在加了。查看最后一个问题代码,您将看到错误以编辑您的答案。
  • 你发布的代码没有显示,变量包含什么.. $foto = dataURI ($ bin) 或类似的东西。如果您无法解释,也请用葡萄牙语写(se tu nao sabes explicar o que tu queres eminges, escreve tudo em protugues e eminges。
猜你喜欢
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 2011-05-06
  • 1970-01-01
  • 2014-10-28
相关资源
最近更新 更多