Undermine2k
Undermine2k

Reputation: 1491

Display multiple images with PHP

I have this PHP script that returns pictures from the database. I am using a loop now to return 5 pictures at once, but I think they are all overlapping so I am only seeing one. How can I shift each picture a couple of pixels over so I can see the other pictures?

<?php
    $mysqli=mysqli_connect('localhost','root','','draftdb');
    if (!$mysqli)
    die("Can't connect to MySQL: ".mysqli_connect_error());

    $param = isset($_GET['rarity']) ? $_GET['loopcount'] :null;

    $stmt = $mysqli->prepare("SELECT display.PICTURE_ID 
    FROM cards  
    INNER JOIN display ON cards.DISPLAY_ID = display.DISPLAY_ID 
    WHERE display.DISPLAY_ID=? AND cards.CARD_TYPE =?" );

    $cardtype='Rare';


    for ($i=0; $i<=5; $i++) 
    {
      $num[$i] = rand(16,30);
       for ($j=0; $j<$i; $j++) 
       {            
          while ($num[$j] == $num[$i])
          {              
             $num[$i] = rand(16,30);   
          }          

                   $displayid= array_shift($num);   

       }   



    $stmt->bind_param("si", $displayid, $cardtype);
    $stmt->execute();
    $stmt->bind_result($image);
    $stmt->fetch();
    header("Content-Type: image/jpeg");
    echo $image; 
    }


    ?>

Upvotes: 0

Views: 4278

Answers (2)

Jonathan
Jonathan

Reputation: 3034

You can't "display" an image with php. You do that with HTML using the <img> tag. What you do is printing the raw image data and then telling the browser It's a picture. I'm not sure what's gonna happen if you do this and print multiple images, but It's up to the browser how It should handle this probably.

This is sometimes done with one image if you for example store them as blobs in a database. But not for this purpose of displaying them.

If you want to merge images you can do this with the GD library in PHP.

Upvotes: 3

Vatev
Vatev

Reputation: 7590

You can only send one picture to the browser with Content-Type: image/jpeg. I can think of two ways to send multiple pictures.

1.Use GD to make a new picture containing all of the other pictures and send it.

2.Send an HTML page instead of the picture:

<?php 
//some code which gets the images and puts them in an array - $images
?>
<!DOCTYPE HTML>
<html>
<body>
<?php foreach($images as $image):?>
    <img src="data:image/jpeg;base64,<?php echo base64_encode($image);?>"/><br/>
<?php endforeach;?>
</body>
</html>

edit: It seems browsers have difficulty parsing very large HTML documents fast, so if you have large or lots of images it might be better to load them in separate HTTP requests (src="image.php?image=xxxx").

Upvotes: 2

Related Questions