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


php gallery





snowboardalliance
I'm making a simple image gallery for a friend and I think I'm going to make it dynamic so I don't have to hardcode each image.

I have an example here.
http://waldbauer.us/valtemp/nature.html

Basically, each page should look for a specific sub directory of gallery for it's page, then for each sub directory within that, it would be a section with a thumb.jpg and images labeled "01 - name _thumb.jpg" "01 - name _normal.jpg" and "01 - name _large.jpg" etc.
Each image would be placed like the example above and I know what html formatting I need.

My question is, being rusty with php, how do I get an array of these images from a directory?
Also, is it easier to have php resize the images (thumbnails and mid-size preview from a large one) or would this be slower than having all sizes already made?

So if someone could give me a quick example of reading all the images in directories based on the current page, that would be great.

Thanks.
sonam
Last few years I am using this peace of code for grabing images from some folder.

Code:
<?php
$dir = "images/somefolder/"; // folder path
$openDir  = opendir($dir);
    while (false !== ($fileNames = readdir($openDir))) {
   $check = $dir . "/" . $fileNames;
   $size = @getimagesize($check); // this check mime of file
   if($fileNames == "." || $fileNames == ".." || strpos($size[mime], "image") === FALSE) {
                continue; // exclude everything wha is not image
           }  else {
                $imagesAll[] = $fileNames; // create an array of images
           }       
    }
   print_r($imagesAll);
?>


If is your images good optimized (max 100Kb) then you can resize directly without using thumbs. You can see how this is work on http://www.bodo.garage100.net/galerija.php?dir=austrija (this is gallery what I am start to create for one customer).

Sonam
snowboardalliance
sonam wrote:
Last few years I am using this peace of code for grabing images from some folder.

Code:
<?php
$dir = "images/somefolder/"; // folder path
$openDir  = opendir($dir);
    while (false !== ($fileNames = readdir($openDir))) {
   $check = $dir . "/" . $fileNames;
   $size = @getimagesize($check); // this check mime of file
   if($fileNames == "." || $fileNames == ".." || strpos($size[mime], "image") === FALSE) {
                continue; // exclude everything wha is not image
           }  else {
                $imagesAll[] = $fileNames; // create an array of images
           }       
    }
   print_r($imagesAll);
?>


If is your images good optimized (max 100Kb) then you can resize directly without using thumbs. You can see how this is work on http://www.bodo.garage100.net/galerija.php?dir=austrija (this is gallery what I am start to create for one customer).

Sonam


Ok, thanks I think I can build off of that for what I want.

One quick question, how would I modify this to first find all sub directories and their names?

i.e.
Code:

gallery/
    spring/
        01.jpg
        02.jpg
        thumb.jpg
    fall/
        01.jpg
        02.jpg
        03.jpg
        04.jpg
        thumb.jpg


So I can keep the images organized. I'll have to see if readdir() finds directories and how to determine if what it reads is a directory.
sonam
This will print out all directories from your images folder and exclude other files.

Code:
$mainDir = "images";
$openMainDir = opendir($mainDir);
while (false !== ($dirs = readdir($openMainDir))) {
    if(is_file($dirs) === false && $dirs != "." && $dirs != "..") {
       $allDirs[] = $dirs;
    }
}
print_r($allDirs);


Sonam
snowboardalliance
sonam wrote:
This will print out all directories from your images folder and exclude other files.

Code:
$mainDir = "images";
$openMainDir = opendir($mainDir);
while (false !== ($dirs = readdir($openMainDir))) {
    if(is_file($dirs) === false && $dirs != "." && $dirs != "..") {
       $allDirs[] = $dirs;
    }
}
print_r($allDirs);


Sonam


How about this?

Code:

      $dir = 'Gallery/' . PAGE . '/'; // folder path
      $dir_handle = opendir($dir);
      $categories = array(); // hold all sub directories which are categories
      while (false !== ($subdir = readdir($dir_handle)))
      {
         if(is_dir($subdir)  && $subdir != "." && $subdir != "..")
         {
            $cat = array();
            $cat['title'] = $subdir;
            $cat['thumb'] = $dir . '/' . $subdir . '/thumb.jpg';
            $cat['images'] = array();
            
            // read the subdir
            while (false !== ($file = readdir($dir_handle)))
            {
               if(is_file($file)  && $file != "." && $file != ".." && $file != "thumb.jpg")
               {
                  // ?
               }
            }
            $categories[] = $cat;
         }
      }


Now how would I go about reading each sub directory for images when I want
$categories[]['images'][]['thumb']
$categories[]['images'][]['normal']
$categories[]['images'][]['large']

with files like

01_somename_thumb.jpg
01_somename_preview.jpg
01_somename_large.jpg

I'm think I would need to read the number (i.e. 01) and use that subscript in my array and determine if it has the "_thumb" or "_large" name.
Should I use like explode() with '_'?
sonam
Could you please print_r($categories); and paste here how I can see structure of array, and of course, you can explode jpg name.
snowboardalliance
sonam wrote:
Could you please print_r($categories); and paste here how I can see structure of array, and of course, you can explode jpg name.


Nevermind I finally had some time to play around with it and got it working.

Thanks a lot for your help getting me started.
Related topics
flash gallery
PHP gallery probleem
Putting something on every page...
my website
Good standalone image gallery?
hi
Script php about gallery
PhP Image gallery?
Random image for your gallery
Photo Gallery with Thumbnails
how u get perfaction in php and myqsl programing
What do you think about the Coopermine Gallery?
image gallery: suggesions reqd
PHP problem...
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.