SitePoint Sponsor

User Tag List

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

    If file_exists/getimagesize

    Some time ago, I developed a fairly elaborate script for displaying images. It displays them only if they exist, automatically adding the correct extension and image size. I'm now trying to modify this script for another website, but I'm having problems.

    This is the modified script:

    PHP Code:
    // This first line will eventually consist of variables; for now, I'm trying to display a map of France, located at the folder /images/maps/cia/eur/fra.gif...
    $imgDir '/images/maps/cia/2007/eur/';

    $imgPath $_SERVER['DOCUMENT_ROOT'] . $imgDir;

    // $MyID2 = each image's name; in this case 'fra'...
    $img glob($imgPath $MyID2.'*');

    $ImgX = array(234);
    $ImgY = array('''''');
    $img str_replace($ImgX$ImgY$img);

    if (empty(
    $img))
    {
     
    // At first, it displayed "NO IMAGE." However, I modified my path, and it no longer displays "NO IMAGE," so everything should be OK up to here...
     
    echo '<strong>NO IMAGE</strong>';
    }
    else
    {
    foreach(
    $img as $path)
    {
     if(
    preg_match("[\.gif$|\.jpg$|\.png$]",$path)){
       
    $image=$path;

    // I inserted the following line as a test...it displays "TEST" but no value for $image...
    echo 'TEST<img src=\"$image\">';

      }
    }

    if (
    file_exists($image)) {
     list (
    $width$height$type$attr) = getimagesize($image);
     
    $image=str_replace($_SERVER['DOCUMENT_ROOT'],"",$image);

    // Here's where it falls off the map; it doesn't even echo "TEST2"...
     
    echo "TEST2<img src=\"$image\" $attr>";

    This is what I'm trying to do...

    Imagine a folder named eur with maps of various Eurasian nations, as follows:

    fra.gif (France)
    esp.jpg (Spain)
    rus.gif (Russia)

    As long as my page ID (e.g. fra, esp, rus) matches a nation's ID, it should display the appropriate image, regardless of its extension (.gif, .jpg, .png), also displaying the image's size. Eventually, I'm going to modify the path so that the image displays correctly whether I'm working on my PC, Mac or online.

    Thanks.

  2. #2
    SitePoint Enthusiast SecondV's Avatar
    Join Date
    Jan 2006
    Location
    Kentucky
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure I understand what you're asking, but anyway.. I modified what you posted:
    Code PHP:
    /**
    * This first line will eventually consist of variables; for now, 
    * I'm trying to display a map of France, located at the folder 
    *     /images/maps/cia/eur/fra.gif
    */
    $imgDir = '/images/maps/cia/2007/eur/';
     
    $imgPath = "$_SERVER[DOCUMENT_ROOT]/$imgDir";
     
    // $MyID2 = each image's name; in this case 'fra'...
    $img = glob("$imgDir*");
     
    $ImgX = array(2, 3, 4);
    $ImgY = array('', '', '');
    $img = str_replace($ImgX, $ImgY, $img);
     
    if (empty($img))
    {
        /**
        * At first, it displayed "NO IMAGE." However, I modified my path, 
        * and it no longer displays "NO IMAGE," so everything should be OK up to here...
        */
        echo '<strong>NO IMAGE</strong>';
    }
    else
    {
        foreach ($img AS $path)
        {
            if (preg_match("#\.(gif|jpg|png)$#", $path) AND file_exists($path))
            {
                list ($width, $height, $type, $attr) = getimagesize($path);
                $image = str_replace($_SERVER['DOCUMENT_ROOT'], "", $path);
                echo "TEST2<img src=\"$image\" $attr /><br />\n";
            } 
        }
    }

    For my test, I changed $imgDir to 'img/' which outputs:

    Code:
    TEST2<img src="img/apachefriends.gif" width="68" height="22" /><br />
    TEST2<img src="img/blank.gif" width="1" height="1" /><br />
    TEST2<img src="img/filezilla1.gif" width="523" height="301" /><br />
    TEST2<img src="img/head-for.gif" width="47" height="35" /><br />
    TEST2<img src="img/head-fuer.gif" width="47" height="35" /><br />
    TEST2<img src="img/head-windows.gif" width="166" height="35" /><br />
    TEST2<img src="img/head-xampp.gif" width="134" height="34" /><br />
    TEST2<img src="img/logo-small.gif" width="51" height="51" /><br />
    TEST2<img src="img/mercury1.gif" width="467" height="436" /><br />
    TEST2<img src="img/strichel.gif" width="8" height="1" /><br />
    TEST2<img src="img/xampp-logo-new.gif" width="200" height="59" /><br />
    TEST2<img src="img/xampp-logo.jpg" width="383" height="117" /><br />


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
  •