Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

php - 如何从数据库中定义以二进制格式存储为div背景图像的图像?(How to define an image from the database stored in binary as a div background image?)

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 .

(我已经(选择了)挽救了这张图片,并希望将其作为div的背景图片插入。)

I can only enter it as src of an img.

(我只能将其作为img的src输入。)

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.

(第一部分输入为img的src。)

( 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背景图像中插入:(问题))

 <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.

    (我试图回显包含url()中图像的变量,但是没有成功。)

  • Generated Page HTML Code: Fiddle

    (生成的页面HTML代码: 小提琴)

  • I am brazilian.

    (我是巴西人。)

    I don't speak english.

    (我不会说英语。)

    I used the google translate.

    (我用谷歌翻译。)

  ask by Thiaguinho Embasamento translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use

(使用)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...