Slideshow with Mysql BLOB Images

Asked

Viewed 3,433 times

6

I am mounting an image manager and I need to display these images on a TV, through the browser by a slideshow.

I can assemble a slide show setting each image address, but I have several images in DB, and would need to create an array of images to make the slideshow take these images from the image array I have in Mysql.

Follow my codes:

php (displays the image on the screen, is displaying one underneath the other)

<?
//CONECTA AO MYSQL                                               
$conn = mysqli_connect("localhost", "root", "", "tv"); 

//EXECUTA A QUERY                                                
$sql = mysqli_query($conn, "SELECT id_msg, img, departamento FROM mensagem");   

echo "<h2>Exibe imagens cadastradas no BD</h2>";
while ($row = mysqli_fetch_row($sql)) {   
$id    = $row[0];                         
$bytes = $row[1];                         
$tipo  = $row[2];                         

echo "<img src='gera.php?id_msg=".$id."' width='200' height='300' border='1'>";

echo "<br><br></div>";
}


?>

gera.php (generate all database images to be displayed by exibe.php)

<?php

//RECEBE PARÂMETRO  
$id = $_GET["id_msg"];  

//CONECTA AO MYSQL                                               
$conn = mysqli_connect("localhost", "root", "Sat3t3ll", "tv"); 

//EXIBE IMAGEM                                                                        
$sql = mysqli_query($conn, "SELECT img FROM mensagem WHERE id_msg = ".$id."");         

$row = mysqli_fetch_array($sql, MYSQLI_ASSOC);    
//$tipo   = $row["tipo"];                        
$bytes  = $row["img"];                        
//EXIBE IMAGEM                                 
header( "Content-type: image/gif");              
echo $bytes;

?>

My idea would be to use this slideshow to display the array of images generated by exibe.php

<html>
<head>
<script src="jquery/jquery.js" type="text/javascript"></script>
<script src="jquery/jcycle.js" type="text/javascript"></script>

<script type="text/javascript">
<!--
$(function() {
$('#slideShow').cycle({ fx: 'fade' });
});
// -->
</script>


</head>
<body>

<div id="slideShow">
<img src="img/foto1.jpg" alt="Primeira Foto" width="300" height="200" />
<img src="img/foto2.jpg" alt="Segunda Foto" width="300" height="200" />
<img src="img/foto3.jpg" alt="Terceira Foto" width="300" height="200" />
</div>

</body>
</html>
  • The problem is to display the images(do not appear)? or only an image is shown in your example?

  • The images appear, the problem is that it takes the first image and applies the effect, in the others it is one on top of the other. I will post the code on the web. 1 min.

  • I’m having trouble getting online. See an example image: http://uploaddeimagens.com.br/imagens/capturr-jpg--663 In image 1 the effect is applied, but the other images are below. The idea would be to put all these images on top of each other and apply the slideshow effect. I’m using Cycle to make the slide.

1 answer

5


You can display the image directly from the database(stream) in the tag <img> using the attribute srcthus: data:image/jpg;base64,stream_codificado....

In the.php view, make an array of all images, then use it on the slide page.

php.

$imagens = array();
while ($row = mysqli_fetch_row($sql)) {   
   $imagens[] = $row;
}

In the file that has the slides, you may need to include.php displays:

<div id="slideShow">
<?php
    foreach($imagens as $item){
       $img_template = '<img src="data:image/jpg;base64,'. $item[1]. '" alt="Primeira Foto" width="300" height="200" />';
       echo $img_template;
    }
?>
</div>

Based on :

PHP show image as BLOB mysqli

Displaying a Base64 image from database via php

How to Decode a Base64 string (gif) into image in PHP / HTML

  • 1

    Only avoid using this method if printing too many images. base64_encode will print a very large amount of text which can give a "choke" in the browser at the time of loading.

  • @Andréribeiro, correct, if there are many images it is better to take the stream turn into a file and load the name on src.

  • People worked. I only had to change from base64_enconde to base64_decode. The enconde was generating several strange characters. I just have one question right now. It takes the 3 images I have, but at the end it shows the icone of a corrupted image, see: http://uploaddeimagens.com.br/imagens/capturr-jpg-665

  • Find out, just take echo $img_template; out of the slide.php code, because it’s already set in displays.php. Thanks again. the following complete code is available for anyone, link: https://drive.google.com/folderview?id=0Bx7OPztNLuy4d0djT1QyUHh3Vlk&usp=sharing

  • Guys, I’m having a hard time. I am making the following query in the database: SELECT id_msg, img FROM message WHERE '$hoje' BETWEEN data_inicio and data_termino, where $hoje picks the current date, I will only display on the screen what corresponds to the initial date and final date. The query is right, but the array is being mounted with a completely blank image, which is the image that is getting out of select. Does anyone know how I can fix this?

  • @Renato_souza_delphi, maybe it is better to ask another question describing this problem and its details.

  • Since this one is being used as a canonical of several duplicates, I think it deserves an Edit to add the possibility of header ( image.... + blob with native data, without this inflating thing with Base64), so it would be very complete

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.