Reputation: 199
I have an image that is stored in the database in binary form. I have rescued this image (with a select) and would like to insert it as a div's background image. I can only enter it as src of an img.
How to Store in Database:
<?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();
}
?>
Part I enter as src of an img. ( it's working)
<?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>
I would like to insert in the div background-image below: (The problem)
<div class="row">
<div class="col-lg-12" style="background-image: url(<?php echo $foto;?>);">
</div>
</div>
I tried to echo the variable that contains the image inside the url (), but it didn't work out.
Generated Page HTML Code:Fiddle
I am brazilian. I don't speak english. I used the google translate.
Upvotes: 1
Views: 88
Reputation: 49385
Use
<div id="postimave" style="background-image: url('<?php echo $image; ?>')">Some text
</div>
Upvotes: 1