Making a small map, from a huge ammount of smaller images

Discussion in 'PHP' started by hip_hop_x, Mar 15, 2008.

  1. #1
    Hello,
    I would like to know what's the solution to make a small image, from pieces?
    In a database, i've stored from 1 to 600 pieces of smaller images, and i would like to put them all in a smaller image.
    Each small piece has 53 width, 38 height.
    What should I do to put each image one near other, and after every 50 images to continue showing the images on the next line.
    Sort of what i want to have in 1 smaller image
    <img src="img1"><img src="img2">...<img src="img50"><br>
    Code (markup):
    Thank you in advance!
     
    hip_hop_x, Mar 15, 2008 IP
  2. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #2
    There's probably a better way to do this, but this is an easy way with nested loops:

    
    
    $numrows = 12;  // number of rows
    $numcols = 50;  // number of images per row
    // 12 rows x 50 images each = 600 images
    
    echo "<table>";
    for ($row = 1, $row < $numrows, $row++) {
      echo "<tr>";
      for ($column = 1, $column < $numcols, $column++) {
        $imagefile = $row*$column;
        echo "<td><img src=\"img$imagefile.gif\"></td>";
      }
      echo "</tr>";
    }
    echo "</table>";
    
    PHP:
    You have to do it in a table because most browsers will force the images to wrap themselves without it.

    There's probably some syntax errors in the above code, but it will do exactly what you want.
     
    Christian Little, Mar 16, 2008 IP
  3. stoli

    stoli Peon

    Messages:
    69
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    There are syntax errors, plus you have the wrong equation to calculate the $imagefile value.

    Here is a fixed version:
    <?php
    
    $numrows = 12;  // number of rows
    $numcols = 50;  // number of images per row
    // 12 rows x 50 images each = 600 images
    
    echo "<table>";
    for ($row = 1; $row <= $numrows; $row++) {
      echo "<tr>";
      for ($column = 1; $column <= $numcols; $column++) {
        $imagefile = (($row-1)*$numcols)+$column;
        echo "<td><img src=\"img$imagefile.gif\"></td>";
      }
      echo "</tr>";
    }
    echo "</table>";
    
    ?>
    PHP:
     
    stoli, Mar 16, 2008 IP
  4. hip_hop_x

    hip_hop_x Active Member

    Messages:
    522
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #4
    hmm, i want to generate a new image using gd library or other module, standalone, not adding piece by piece in a table.
     
    hip_hop_x, Mar 16, 2008 IP