SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Nov 2004
    Location
    Sweden
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    do not show images with thumb_ infront of the name

    I have a script that get images from a folder and then shows them on my site. But i have edited the script and now i have thumbnail-images in the same folder as the ordinary. Now i have a problem with the script, it shows all the images both ordinary and thumbnail-images on the site. All thumbnails have "thumb_" infront of the ordinary name, like thumb_hello.jpg, thumb_2.jpg, thumb_car.png etx etc.

    How do i show only the ordinary images from the folder? Like hello.jpg, 2.jpg, car.png etc

    The script im am using for the moment.
    PHP Code:
    echo "<table  class='reservdelar' style='width: 100%; padding: 4px;'>";
                echo 
    "<tr>";
                echo 
    "<td style='width: 70%;'>";
              
                
    $kh = @opendir("bilder/trimdelar/$_GET[rID]");
                 while(
    $bild = @readdir($kh)){
                              
                     if(!(
    $bild=="."||$bild=="..") && preg_match("#\.(gif|jpeg|jpg)$#i"$bild)){
                     
    $flerabilder[] = $bild;
                      }
                }
                             
                @
    closedir($kh);
                 
                @
    $enbild array_slice($flerabilder01);
                            
                      if (!empty(
    $enbild)) {
                        echo 
    "<img id='imgBig' src='bilder/trimdelar/$_GET[rID]/$enbild[0]' alt='' border='1' width='570px' height='445px'>";
                      }
                  
                  echo 
    "</td>";
                  echo 
    "<td style='width: 30%; vertical-align: top;'>";
                            
                      if (!empty(
    $flerabilder)) {
                      foreach (
    $flerabilder as $bild) {
                         echo 
    "<img src='bilder/trimdelar/$_GET[rID]/$bild' vspace='2' width='75' height='56' border='1' onclick=\"document.images['imgBig'].src='bilder/trimdelar/$_GET[rID]/$bild'\"><br />";
                        }
                      }  
                
                  echo 
    "</td>";
                  echo 
    "</tr>";
                  echo 
    "</table>"
    Is there anyone that has an idea? Maybe another preg_match or something? but i dont know how :/

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,367
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    You could use strpos to see if the string contains thumb_

    PHP Code:
    $thumbnail strpos($bild,'thumb_');

    if(
    $pos === false) {
    echo 
    "<img src='bilder/trimdelar/$_GET[rID]/$bild' vspace='2' width='75' height='56' border='1' onclick=\"document.images['imgBig'].src='bilder/trimdelar/$_GET[rID]/$bild'\"><br />"
    Also I would look at glob() for getting your directory contents as I think it is a bit easier/neater; from memory you may be able to weed out the thumb_ images within the glog function.

    PHP Code:
    // Directory to read images from
    $dir "./";

    // Read the directory and sellect jpg and png
    $filenames glob("$dir*.{jpg, JPG, png, PNG}"GLOB_BRACE); 
    Last edited by Rubble; Apr 22, 2012 at 02:01. Reason: Added the glob code

  3. #3
    SitePoint Member
    Join Date
    Nov 2004
    Location
    Sweden
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for a quick respons. But after some "google'ing" i got an solution for my problem

    I changed
    PHP Code:
    if(!($bild=="."||$bild=="..") && preg_match("#\.(gif|jpeg|jpg)$#i"$bild)){ 
    to
    PHP Code:
    if(!($bild=="."||$bild=="..") && preg_match("#\.(gif|jpeg|jpg)$#i"$bild) && preg_match("#^thumb_#i"$bild) == false){ 
    And it works fine.

    Why should i use
    PHP Code:
    $filenames glob("$dir*.{jpg, JPG, png, PNG}"GLOB_BRACE); 
    instead of the code im using for the moment?

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,367
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    You could replace all this:
    PHP Code:
    if(!($bild=="."||$bild=="..") && preg_match("#\.(gif|jpeg|jpg)$#i"$bild)){ 
                     
    $flerabilder[] = $bild
    with
    PHP Code:
    $filenames glob("$dir*.{jpg, JPG, png, PNG}"GLOB_BRACE); 
    If you had an index file or other file in your folder you would need to add that to your ignore code; but with glob you do not.
    Your code is selecting everything then weeding out what you do not want; whereas glob is just selecting what you want.

    But as I say it is personal preferance.

    You can also use patterns and I am sure you could have added something to the glob line to avoid loading any files starting with thumb_ into the array at thet point as well.

  5. #5
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Using glob could streamline your code somewhat, which it uses just a single statement. However, it does require an additional function to filter for the thumb images. Here is how your script would look like:
    PHP Code:
    <?php

       
    function thumb($var)
       {
        
    // returns whether the input is a thumbnail image
        
    if(strstr($var,"thumb_"))return false;   
        return 
    true;
       }

       
    $dir "bilder/trimdelar/$_GET[rID]";
       
    $filenames glob("$dir*.{gif,jpeg,jpg}"GLOB_BRACE);
       
    $filenames array_filter($filenames,"thumb"); // get rid of thumb images

       
    echo "<table  class='reservdelar' style='width: 100%; padding: 4px;'>";
       echo 
    "<tr>";
       echo 
    "<td style='width: 70%;'>";
                 
       @
    $enbild array_slice($filenames01);
                            
       if (!empty(
    $enbild)) {
           echo 
    "<img id='imgBig' src='$enbild[0]' alt='' border='1' width='570px' height='445px'>";
       }
       echo 
    "</td>";
       echo 
    "<td style='width: 30%; vertical-align: top;'>";

       foreach (
    $filenames as $bild)
       {
           echo 
    "<img src='$bild' vspace='2' width='75' height='56' border='1' onclick=\"document.images['imgBig'].src='$bild'\"><br />";
       }   
       echo 
    "</td>";
       echo 
    "</tr>";
       echo 
    "</table>";  
    ?>
    I don't see any advantage in either approach.

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,367
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    I am not an expert on this but something like this may work?
    PHP Code:
    $filenames glob("$dir.!thumb_*.{jpg, JPG, png, PNG}"GLOB_BRACE); 

  7. #7
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I tried that and it didn't work.

  8. #8
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,367
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    That was only a guess tom8; this seems to work:
    PHP Code:
    <?php

    $filenames 
    glob("photos/[!thumb_]*.{jpg,gif,png}"GLOB_BRACE);

    foreach ( 
    $filenames as $value ){ echo $value."<br>"; }

    ?>
    Need to give credit to: http://cowburn.info/2010/04/30/glob-patterns/

  9. #9
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, it works!

    Here is the revised version of the script:
    PHP Code:
    <?php

       $dir 
    "bilder/trimdelar/$_GET[rID]";
       
    $filenames glob($dir."[thumb_]*.{gif,jpeg,jpg}"GLOB_BRACE);

       echo 
    "<table  class='reservdelar' style='width: 100%; padding: 4px;'>";
       echo 
    "<tr>";
       echo 
    "<td style='width: 70%;'>";
                 
       @
    $enbild array_slice($filenames01);
                            
       if (!empty(
    $enbild)) {
           echo 
    "<img id='imgBig' src='$enbild[0]' alt='' border='1' width='570px' height='445px'>";
       }
       echo 
    "</td>";
       echo 
    "<td style='width: 30%; vertical-align: top;'>";

       foreach (
    $filenames as $bild)
       {
           echo 
    "<img src='$bild' vspace='2' width='75' height='56' border='1' onclick=\"document.images['imgBig'].src='$bild'\"><br />";
       }   
       echo 
    "</td>";
       echo 
    "</tr>";
       echo 
    "</table>";  
    ?>
    Now definitely this is a better approach. Did the whole thing with one single statement

  10. #10
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,367
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    Yes looks a lot better; I would put the start of the table and the end that do not need php outside the php tags and call it done.

    PHP Code:

       <table  class='reservdelar' style='width: 100%; padding: 4px;'>
       <tr>
       <td style='width: 70%;'>

    <?php 

       $dir 
    "bilder/trimdelar/$_GET[rID]"
       
    $filenames glob($dir.[!thumb_]*.{gif,jpeg,jpg}", GLOB_BRACE); 
                  
       @
    $enbild = array_slice($filenames, 0, 1); 
                             
       if (!empty(
    $enbild)) { 
           echo "
    <img id='imgBig' src='$enbild[0]' alt='' border='1' width='570px' height='445px'>"; 
       } 
       echo "
    </td>"; 
       echo "
    <td style='width: 30%; vertical-align: top;'>"; 

       foreach (
    $filenames as $bild
       { 
           echo "
    <img src='$bild' vspace='2' width='75' height='56' border='1' onclick=\"document.images['imgBig'].src='$bild'\"><br />"
       }   
    ?>
     
       </td>
       </tr>
       </table>

  11. #11
    SitePoint Addict tom8's Avatar
    Join Date
    Mar 2012
    Location
    New Jersey
    Posts
    310
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes, that looks even better but I try not to alter the original script too much.

    BTW, there is a " (quote) after dir.

    $filenames = glob($dir."[!thumb_]*.{gif,jpeg,jpg}", GLOB_BRACE);

  12. #12
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,367
    Mentioned
    80 Post(s)
    Tagged
    3 Thread(s)
    No there should be a quote before $dir - it seems to have been lost along the way somewhere and there needs to be a / in the $dir variable or on the glob after $dir.
    PHP Code:
    $dir "bilder/trimdelar/$_GET[rID]/";  
    $filenames glob("$dir.[!thumb_]*.{gif,jpeg,jpg}"GLOB_BRACE); 


Bookmarks

Posting Permissions

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