SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get IMAGE by Size, NOT Folder...

    I've been working on this script for several days now. It's designed to display images regardless of size and file extensions. It was working fine except for a quirk that apparently recognizes folders as images. In other words, if I have an image named usa.jpg and a folder named "usa," it throws a fit.

    Someone gave me the following script, which solved that problem:

    PHP Code:
    foreach($img as $path){
        if(
    preg_match("[\.gif$|\.jpg$|\.png$]",$path)){
            
    $image=$path;
            break;
        }

    Unfortunately, it created a new problem - pages that use this script won't load in Internet Explorer.

    So while I search for another workaround, I thought another solution would be to go back to square one and replace the above script with something completely different. Any suggestions?

    I pasted the entire script below. Thanks.

    PHP Code:
    $imgDir '/images/plants/flowers/world/'.$PicLink.''.$Parent.'/';
    $imgPath $_SERVER['DOCUMENT_ROOT'] . $imgDir;
    $img glob($imgPath $mycode2.'*');

    // MISSING IMAGE SCRIPT
    if (empty($img))
    {
    echo (
    '<div style="padding: 10px; background: #000; color: #fff; float: right; margin-left: 15px;">Sorry, no pictures yet.</div>');
    }
    else
    {
    // (continued)

    // IGNORE MS FOLDERS SCRIPT
    foreach($img as $path){
        if(
    preg_match("[\.gif$|\.jpg$|\.png$]",$path)){
            
    $image=$path;
            break;
        }
    }
    // END IGNORE FOLDERS SCRIPT

     
    if (file_exists($image)) {
        list(
    $width$height$type$attr) = getimagesize($image);
        
    $image=str_replace($_SERVER['DOCUMENT_ROOT'],"",$image);
        
    $widthpx $width 'px';
        echo 
    "<div style=\"width:$widthpx; margin-right: -40px; $BorderDivFlower\"".     
            
    " class=\"$ImgClass\">";

        echo 
    "<img src=\"$image\" $attr alt=\"$myname\" style=\"".
            
    "$StyleImg\" />$CreditFlower $BigFlower</div>";
    }
    // END MISSING IMAGE SCRIPT...


  2. #2
    SitePoint Zealot
    Join Date
    Jan 2005
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are 2 functions in PHP that might help you.

    is_file() and is_dir()

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chowmein858
    There are 2 functions in PHP that might help you.

    is_file() and is_dir()
    Thanks; that sounds like the right direction. This is the example I copied from the manual:

    PHP Code:
    <?
    var_dump
    (is_file('a_file.txt')) . "\n";
    var_dump(is_file('/usr/bin/')) . "\n";
    ?>
    And this is the script I need to modify:

    PHP Code:
    foreach($img as $path){
        if(
    preg_match("[\.gif$|\.jpg$|\.png$]",$path)){
            
    $image=$path;
            break;
        }

    So I presume I need to perhaps put my script inside an IF function...something like this:


    if { the object is a file, include my script; }

    else {}

    I assume I could just leave else blank - else ()

    Can anyone show me how to combine my script and the is_file() function?

    Thanks.

  4. #4
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($img as $path){ 
        if(
    is_file($path))
       { 
            
    $image=$path;
        }

    Or whatever-- that's how'd you use it.

  5. #5
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, you're using in-line CSS, that defeats the purpose of using CSS.

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dylan B
    PHP Code:
    foreach($img as $path){ 
        if(
    is_file($path))
       { 
            
    $image=$path;
        }

    Or whatever-- that's how'd you use it.
    Wow, I just can't beat IE! My revised script works just like the last one - it works great in Firefox, but my page doesn't load in IE.

    Actually, could someone do me a favor and view http://www.geosymbols.org/world/na/usa/wy/flower/ in your Internet Explorer browser, then tell me if it loads for you? Maybe there's something else going on here.

    Thanks for the script, though. That gives me more ideas to play with.

    Regarding CSS, most of my stuff is in external files, but I like to put specific CSS styles with the stuff I want to modify. That has a couple advantages.

    First, if I want to modify div#unique, I don't have to go searching through an external file sheet for it. I just go straight to div#unique and fix it right there. Also, it ensures that that style will be used only on pages that feature that unique element, rather than bogging pages that don't even use it down.

    A third advantage is that it helps keep my external style sheets a little leaner. They're bloated enough as it is. When I finally get all my PHP and JavaScript squared away, then I'll have time to review and condense my CSS.


    PHP Code:
    $imgDir '/images/plants/flowers/world/'.$PicLink.''.$Parent.'/';
    $imgPath $_SERVER['DOCUMENT_ROOT'] . $imgDir;
    $img glob($imgPath $mycode2.'*');

    // MISSING IMAGE SCRIPT
    if (empty($img))
    {
    echo (
    '<div style="padding: 10px; background: #000; color: #fff; float: right; margin-left: 15px;">Sorry, no pictures yet.</div>');
    }
    else
    {
    // (continued)




    foreach($img as $path){
        if(
    is_file($path))
       {
            
    $image=$path;
        }
    }





     if (
    file_exists($image)) {
        list(
    $width$height$type$attr) = getimagesize($image);
        
    $image=str_replace($_SERVER['DOCUMENT_ROOT'],"",$image);
        
    $widthpx $width 'px';
        echo 
    "<div style=\"width:$widthpx; margin-right: -40px; $BorderDivFlower\"".     
            
    " class=\"$ImgClass\">";

        echo 
    "<img src=\"$image\" $attr alt=\"$myname\" style=\"".
            
    "$StyleImg\" />$CreditFlower $BigFlower</div>";
    }
    // END MISSING IMAGE SCRIPT...


  7. #7
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It crashed my IE when I tried..

  8. #8
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It sounds like an display problem.

    Post the HTML source that you get.

  9. #9
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by klik
    It crashed my IE when I tried..
    Oops, I should havd said it may freeze (i.e. "crash") IE, not just fail to load the page. Sorry.

  10. #10
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No apology needed. I wasn't annoyed, I was simply informing you


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
  •