FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Index page for images, putting them all on one page 50fri$!!





The-Master
If anyone can help me with this then I will give them 50fri$!!!

What I would like is a php code which can take all the images in a directory and lay them out, resized, into a neet row with Size, Name Of File etc.

Roughly like how Dailymotion lays thier files out.

If anyone can help me I will be very happy and wil give you 50fri$!!!
alalex
the best way to do that, is with javascript. and the easiest to do it with the less code is naming your images 1.gif , 2.gif... (it doesnt matter the format)
and you would do a function like this:
Code:
<script language="javascript" type="text/javascript">
<!--
   //create the image html code by pieces
   var root = "images/directory/";
   var end = ".gif";
   var img_1 = '<img src="';
   //custom height                           var img_2 = '" width="120" height="80" border="1" class="image_list" alt="Image">';
   for (i = 1; i <= 25; i++){
      document.write('<a href="imagenes/fotos/campamento/' + i + '.JPG" target="_blank">' + img_1 + camp_root + i + end + img_2 + '</a>');
      i++;
      document.write('<a href="imagenes/fotos/campamento/' + i + '.JPG" target="_blank">' + img_1 + camp_root + i + end + img_2 + '</a>' + "<br>");
   }
-->
</script>
alalex
hope it helps! (when it works, 50 $frihs Wink )
hexkid
The-Master wrote:
What I would like is a php code which can take all the images in a directory and lay them out, resized, into a neet row with Size, Name Of File etc.


If you have lots of images (more than ... er ... 12) it really would be tremendously helpful to have a database.
Anyway, make an array with the image names and use it to display the imegaes. The array can be populated from data in the database or from readdir() and friends; I'll just use constants for the example code below.

Code:
<?php
$images = array(
  array(name=>'photo42.jpg', size=>42000, directory=>'photos/', URL=>'http://example.com/images/photo/photo42.jpg'),
  array(name=>'avatar42.gif', size=>4200, directory=>'avatars/', URL=>'http://example.com/images/avatars/avatar42.gif'),
  // ...
);

for ($row = 0; $row < 4; ++$row) {
  for ($col = 0; $col < 4; ++$col) {
    $index = $row * 4 + $col;
    echo '<img src="', $images[$index]['URL'], '"><br>', $images[$index]['size'], ' bytes';
  }
}
?>
The-Master
hexkid wrote:
The-Master wrote:
What I would like is a php code which can take all the images in a directory and lay them out, resized, into a neet row with Size, Name Of File etc.


If you have lots of images (more than ... er ... 12) it really would be tremendously helpful to have a database.
So how would I go about making a database? The php script worked alright but the size of the file did not line up next to the image which was annoying.

I tried the javascript method and it did not work. I just got a blank page. Please give me a bit of help with the javascript as I am not too good with javascript. I am slightly better with php but still not great.

[EDIT]Well based on one of the php codes on the PHP: readdir manual, I managed to edit the code to work like I wanted.

Thanks for everyones help and if they think they can help by giving me a better code then feel free and I will give them the 50fri$.

Sorry that I didn't understand the javascript alalex but if you can explain it and get a better result than I already have then I will still give you the points.

I think I will donate 10fri$ to hexkid because he has helped me several times and the link helped so thanks. Anyway here is what I did:
Code:

<?php
   function file_type($file){
       $path_chunks = explode("/", $file);
       $thefile = $path_chunks[count($path_chunks) - 1];
       $dotpos = strrpos($thefile, ".");
       return strtolower(substr($thefile, $dotpos + 1));
   }
   $path = "photos/";
   $file_types = array('jpeg', 'jpg', 'ico', 'png', 'gif', 'bmp', 'html');
   $p = opendir($path);
       while (false !== ($filename = readdir($p))) {
           $files[] = $filename;
       }
   sort($files);
       foreach ($files as $file) {
           $extension = file_type($file);
           if($file != '.' && $file != '..' && array_search($extension, $file_types)){
               $file_count++;
               echo '<a href="'.$path.$file.'"><img src="'.$path.$file.'" width=500 height=500/>'.$file.'</a> <br/>';
           }
       }
?>
hexkid
Thank you for the FRIH$ Smile

The-Master wrote:
So how would I go about making a database?

If you're on FriHost, MySQL is already setup. You just go to the administration area (cPanel or DirectAdmin) and create a personal database. Then, probably with PHPmyAdmin, create the tables and populate them. To use the data in your PHP scripts you need to use MySQL functions. It appears to be a lot more work than using readdir() and friends, but once the initial setup is done, it's much more powerful: you can sort by size, page through the images, keep a viewed count, ..., ...

The-Master wrote:
The php script worked alright but the size of the file did not line up next to the image which was annoying.

Well ... I don't do (more than basic) HTML.

The-Master wrote:
if they think they can help by giving me a better code then feel free

Code:
<?php
   #### Hmmmm, PHP already has a function that does what
   #### your file_type() function does.
   #function file_type($file){
   #    $path_chunks = explode("/", $file);
   #    $thefile = $path_chunks[count($path_chunks) - 1];
   #    $dotpos = strrpos($thefile, ".");
   #    return strtolower(substr($thefile, $dotpos + 1));
   #}
   function file_type($file) {
     return strtolower(pathinfo($file, PATHINFO_EXTENSION));
   }

   $path = "photos/";
   $file_types = array('jpeg', 'jpg', 'ico', 'png', 'gif', 'bmp', 'html');
   $p = opendir($path);
       while (false !== ($filename = readdir($p))) {
           #### Why not put the extension test here?
           if ($file != '.' && $file != '..' && array_search($extension, $file_types)) {
              #### You could also get the file size now
              #$files[] = $filename;
              $files[] = array($filename, filesize($path . $filename));
           }
       }
   sort($files);
       foreach ($files as $file) {
           #### Moved the test outside the loop
           #$extension = file_type($file);
           #if($file != '.' && $file != '..' && array_search($extension, $file_types)){
               $file_count++;
               #### As each $file is now an array (with name and size)
               #### the $file below needs to specify an index.
               #### For the filename the index is 0, it is 1 for the size
               echo '<a href="'.$path.$file[0].'"><img src="'.$path.$file[0].'" width=500 height=500/>'.$file[0].'</a> <br/>';
               echo 'File size: ', $file[1], ' bytes<br/>';
           #}
       }
?>
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.