Location: PHPKode > scripts > file_finder > file_finder/file_finder_class.php
<?PHP // CLASS TO FIND AN IMAGE GIVEN THE NAME (no ext. needed)

class file_find {

  VAR $fname ;
  VAR $found ;
  VAR $size ;
  VAR $width ;
  VAR $height ;
  VAR $maxwidth ;
  VAR $maxheight ;
  VAR $ratio ;
  VAR $maxratio ;
  VAR $rootdir ;
  VAR $imgdir ;
  VAR $alttext ;
  VAR $extensions ;
  
  function file_find($imgdir="images") {
    // Constructor
    $this->extensions = Array (".gif",".jpg",".png") ;
    $this->fname = "" ;
    $this->rootdir = "./" ;
    $parts = explode('/',$GLOBALS["PHP_SELF"]) ;
    for ($i=1;$i<count($parts)-1;$i++) {
      $this->rootdir = $this->rootdir . "../" ;
      }
    if ($imgdir=="") {
      $this->imgdir = "./" ;
    } else {    
      $this->imgdir = $imgdir ;    
      }
    $this->maxwidth = 0 ;
    $this->maxheight = 0 ;
    $this->alttext = "" ;
    $this->ratio = 1 ;
    $this->maxratio = 1 ;
    }
    
  function get_file ($fname,$alttext,$maxw = 0,$maxh = 0,$altimg = "") {
    // Find the file in the dir
    $this->found = false ;
    $this->fname = $fname ;
    if (strrchr($this->fname,".")) {
      if (file_exists($this->rootdir . $this->imgdir . "/" . $this->fname)) {
        $this->found = true ;
        }
    } else {
      reset($this->extensions) ;
      $ext = current($this->extensions) ;
      while ($ext!=false && $this->found!=true) {
        if (file_exists($this->rootdir . $this->imgdir . "/" . $this->fname . $ext)) {
          $this->found = true ;
          $this->fname = $this->fname . $ext ;
        } else {
          $ext = next($this->extensions) ;
          }
        }
      }
    if ($this->found) {
      $this->size = GetImageSize($this->rootdir . $this->imgdir . "/" . $this->fname) ;
      $this->width = $this->size[0] ;
      $this->height = $this->size[1] ;
      $this->alttext = $alttext ;
      $this->ratio = $this->size[0]/$this->size[1] ;
      if ($maxw != 0 || $maxh != 0) {
        if ($maxw==0) $maxw = $this->size[0] ;
        if ($maxh==0) $maxh = $this->size[1] ;
        $this->maxratio = $maxw/$maxh ;
        $this->maxwidth = $maxw ;
        $this->maxheight = $maxh ;
        // We work always width first for consistency
        if ($this->ratio >= 1 && $this->maxratio >= 1) {
          // width always >= height on original and scaled image
          if ($this->maxratio <= $this->ratio) {
            // WIDTH SCALED
            $this->scale_width() ;
          } else {
            // HEIGHT SCALED
            $this->scale_height() ;
            }
          }
        if ($this->ratio < 1 && $this->maxratio < 1) {
          // width always < height on original and scaled image
          if ($this->maxratio <= $this->ratio) {
            // WIDTH SCALED
            $this->scale_width() ;
          } else {
            // HEIGHT SCALED
            $this->scale_height() ;
            }
          }
        if ($this->ratio >=1 && $this->maxratio < 1) {
          // scaled width always < : sure height will be smaller
          $this->scale_width() ;
          }
        if ($this->ratio < 1 && $this->maxratio >=1) {
          // scaled height will be <: sure width will be smaller
          $this->scale_height() ;
          }        
        }
      $this->put_image() ;
      }
    if (!$this->found && $altimg != "") {
      $this->get_file($altimg,$alttext,$maxw,$maxh) ;
      }
    }
    
  function scale_width() {
    if ($this->size[0] > $this->maxwidth) {
      $this->width = $this->maxwidth ;
      $this->height = ($this->maxwidth * $this->size[1])/$this->size[0] ;
      }
    }
    
  function scale_height() {
    if ($this->size[1] > $this->maxheight) {
      $this->height = $this->maxheight ;
      $this->width = ($this->maxheight * $this->size[0])/$this->size[1] ;
      }
    }
    
  function put_image() {
    echo '<img src="/'. $this->imgdir . '/' . $this->fname ;
    echo '" width="' . $this->width . '" height="' . $this->height . '" alt="' ;
    echo $this->alttext . '" border="0">' ;
    }
  }
?>
Return current item: file_finder