Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts

    OO Gallery based on a directory

    class_lib.php
    PHP Code:
    <?php
    class createGallery {
        var 
    $result;
        function 
    __construct($dir) {
             
    $search = array($dir."*.jpg"$dir."*.png"$dir."*.gif");
            
    $path '{' implode(','$search) . '}';
            
    $this->result glob($path,GLOB_BRACE);  
        } 

        function 
    display($maxSize$containerSize$marginBoth)
        {

            
    $maxSize2 $maxSize $marginBoth;
            
    $number floor($containerSize $maxSize2);
            
    $galleryContSize $maxSize2 $number $contmargin;  
            echo 
    "<div style=\"width:".$containerSize."px;margin:0 auto;background-color:#999;\">\n\n<div id='galleryCont' style='width:".$galleryContSize."px;margin:0 auto;'>\n\n";
            foreach (
    $this->result as $file)
            {
                
    $file str_replace($_SERVER['DOCUMENT_ROOT'],'',$file); 
                
    $dimensions1 getimagesize($file); 
                  
    $xoldsize $dimensions1['0'];
                 
    $yoldsize $dimensions1['1'];
                  if (
    $xoldsize $yoldsize) {
                    
    $biggest $xoldsize;
                    
    $class "l";
                  }
                  if (
    $xoldsize $yoldsize) {
                     
    $biggest$yoldsize;
                    
    $class "p";
                  }
                if (
    $biggest $maxSize) {
                    
    $ratio "Not Changed";
                    
    $xnew $xoldsize;
                    
    $ynew $yoldsize;
                }else {
                    
    $ratio $maxSize $biggest;
                    
    $xnew $xoldsize $ratio;
                    
    $ynew $yoldsize $ratio;
                }
                if (
    $class == "l") {
                    
    $ynew2 $maxSize $ynew;
                    
    $margin $ynew2 "2";
                    
    $margin "margin:".$margin."px 0px";
                }
                if (
    $class == "p") {
                    
    $xnew2 $maxSize $xnew;
                    
    $margin $xnew2 "2";
                    
    $margin "margin:0px ".$margin."px";
                }
                echo 
    "<div class=\"imgCont\" style='width:".$maxSize."px;height:".$maxSize."px;'>\n<img src=\"".$file."\" style=\"".$margin."\" width=\"".$xnew."\" height=\"".$ynew."\" /></div>\n";
                
    $a++;
                if (
    $a $number == && $a 0){
                    echo 
    "\n<div style='clear:both'></div>";
                }  
                
    //unset($margin);
            
    }
            echo 
    "<div style='clear:both;'></div></div></div>";
        }
    }
    ?>
    index.php
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Gallery</title>
    <?php include("gallery.php"); ?>

    <style type="text/css">>
    body
    {
        margin:0;
        padding:0;
    }
    .imgCont {
        
        background-color:#333;
        float:left;
        margin:5px 5px;
    }
    </style>
    </head>
    <body>
    <?php

    $gallery 
    = new createGallery("photos2/");  //createGallery("path/to/photos");
    echo "<center><h1>Tom's OOP Gallery</h1></center>";
    $gallery->displayGallery("250""800""10"); //displayGallery("max image size", "containerSize", "margin(both sides)");
    ?>
    </body>
    </html>
    EDIT: Added oesxyl's improvements.

    Just something i made, feel free to use/change/post any improvments/questions you want . This is my first attempt at an OOP. From the max image width and container size it will determine how many columns it can have (including room for the margin). Also it will find the largest side of an image, and proportionally shrink it until the longer side is "max image width"
    Last edited by tomharto; 04-24-2011 at 05:18 PM.

  2. Users who have thanked tomharto for this post:

    oesxyl (04-23-2011)

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    few problems:

    1. $file is the absolute path to the image, will not allways work when you use it in the src attribute of the img tag( server settings), so you need a relative path to the root document for this:

    PHP Code:
    $path str_replace($_SERVER['DOCUMENT_ROOT'],'',$file); 
    2. clearing floats:
    move this after you output the image inside the foreach
    PHP Code:
                            $a++;
                if (
    $a $number == && $a 0){
                    echo 
    "\n<div style='clear:both' id=\"cf".$a."\"></div>";
                } 
    and add this just after foreach
    PHP Code:
                    echo "\n<div style='clear:both' id=\"cf".$a."\"></div>"
    id can't start with a number, you need a prefix, i used 'cf' (abbr for clear float).

    3. move last block:

    Code:
    </div></div>
    from html page inside the method displayGallery since you start divs there.

    4. you need to add a offset for left and right margin to this else last img will apear on the next line in some situation. I used a $contmargin of 10px.
    PHP Code:
            $galleryContSize $maxSize2 $number $contmargin
    some personal opinions:
    - usualy a class is a model of something which is a noun( when the class doesn't model a relation between objects), and probably the class name should be Gallery not createGallery.
    - the suffix Gallery in method name, displayGallery is redundat because the method is of the class Gallery, should be only display.

    best regards

  • Users who have thanked oesxyl for this post:

    tomharto (04-24-2011)

  • #3
    Regular Coder
    Join Date
    Jul 2010
    Location
    Sheffield
    Posts
    827
    Thanks
    93
    Thanked 18 Times in 18 Posts
    Thanks for your advice/changes . As i said its my first attempt at an OOP so any advice i can get now to help me prevent bad habits later on are always helpful

    EDIT: Also the id inside the float was to check the value of $number i dont need it in at all so ill just take it out :P. Also changed the posted code, hopefully thats better than before
    Last edited by tomharto; 04-24-2011 at 05:13 PM.

  • #4
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Sorry, my question is a little out of topic, but still.....
    I'm having trouble finding something. If I can't find it, I could
    bumble my way through writing something, but I thought I'd check around
    first.

    I am looking for a php script or some other software that will build a
    dynamic photo gallery without adding or changing my photos, or creating
    any static content.

    I use Gallery2 for my favorite photos...but I'd like the rest of the
    thousands to be available online, and to be available just becuase
    they're in the photos folder. I keep my photos in something like
    /home/mdg/Photos/2006/whatever ../2006/whateverelse
    .../2006/whateverthethird and I add them all the time.

    I want a script that I could keep at
    /home/mdg/public_html/somescript.php or whatever...and point it at my
    photo root and have them all be there when I hit the script with my
    browser.

    Any advice?

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by britney12 View Post
    Any advice?
    Did you... try the class out? It seems to be almost exactly what you're looking for.

  • Users who have thanked Inigoesdr for this post:

    britney12 (08-05-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •