Trying to reduce functionality, what should I comment?

Discussion in 'PHP' started by thewindmaster, Aug 16, 2007.

  1. #1
    I have the following script that automatically creates a thumbnail image gallery. All you have to do is upload images to the directory or any subdirectories and the script will create a thumbnail and link it to the image.

    Well my host uses a security setting that prevents excessive use of the GD2 library to prevent dos attacks and the script does not work properly.

    I don't care about resizing the image. I just want to be able to upload an image and get it to display without editing the html. What to I need to get rid of here to get that functionality.

    */
    
    // GALLERY SETTINGS
    
    Define('DIR_CACHE', './tmp/'); // directory to store modified images (shoul have right to write files)
    Define('THUMBNAIL_WIDTH', '0'); // thumbnails width
    Define('THUMBNAIL_HEIGHT', '0'); // thumbnails height
    Define('IMAGES_ROW', 1); // images on row
    
    
    
    
    // ----------------------------------------------------------------------
    
    
    
    
    
    $img=$_GET['img'];
    
    if (!Is_Dir(DIR_CACHE)) {
     mkdir(DIR_CACHE, 0777);
    }
    
    // IMAGE RESIZE AND SAVE TO FIT IN $new_width x $new_height
    if ($img!='' && file_exists($img)) {
    
     $w=$_GET['w'];
     $h=$_GET['h'];
    
     $thumb=strtolower(preg_replace('/\W/is', "_", "$img $w $h"));
    
      $changed=0;
      if (file_exists($img) && file_exists(DIR_CACHE.$thumb)) {
       $mtime1=filemtime(DIR_CACHE.$thumb);
       $mtime2=filemtime($img);
       if ($mtime2>$mtime1) $changed=1;
      } elseif (!file_exists(DIR_CACHE.$thumb)) {
       $changed=1;
      }
    
     if ($changed) {
    
     $filename=$img;
     $new_width=(int)@$w;
     $new_height=(int)@$h;
     $lst=GetImageSize($filename);
     $image_width=$lst[0];
     $image_height=$lst[1];
     $image_format=$lst[2];
    
     if ($image_format==1) {
       Header("Content-Type:image/gif");
       readfile($filename);
       exit;
     } elseif ($image_format==2) {
      $old_image=imagecreatefromjpeg($filename);
     } elseif ($image_format==3) {
      $old_image=imagecreatefrompng($filename);
     } else {
      exit;
     }
    
     if (($new_width!=0) && ($new_width<$image_width)) {
      $image_height=(int)($image_height*($new_width/$image_width));
      $image_width=$new_width;
    //  $image_height=$new_height;
     }
    
     if (($new_height!=0) && ($new_height<$image_height)) {
      $image_width=(int)($image_width*($new_height/$image_height));
      $image_height=$new_height;
    //  $image_width=$new_width;
     }
    
    
    //  $new_image=imageCreate($image_width, $image_height); // for old GDlib
     $new_image=imageCreateTrueColor($image_width, $image_height);
    
     $white = ImageColorAllocate($new_image, 255, 255, 255);
     ImageFill($new_image, 0, 0, $white);
    
    // imagecopyresampled
    // imageCopyResized (for old GDlib)
     imageCopyResampled( $new_image, $old_image, 0, 0, 0, 0, $image_width, $image_height, imageSX($old_image), imageSY($old_image));
    
     imageJpeg($new_image, DIR_CACHE.$thumb);
     }
    
    
     Header("Content-type:image/jpeg");
     readfile(DIR_CACHE.$thumb);
     exit;
    }
    
    // ----------------------------------------------------------------------
    
      $init_dir="./";
    
      if (IsSet($HTTP_GET_VARS['dir'])) {
       $dir=$HTTP_GET_VARS['dir'];
       $dir=preg_replace("/\.+/", ".", $dir);
      }
    
      if (IsSet($dir) && ($dir!=$init_dir)) {
       $directory=$dir;
       $tmp=array_reverse(explode('/', $dir));
       array_shift($tmp);
       array_shift($tmp);
       $out['UP']=urlencode(implode('/', array_reverse($tmp))."/");
      } else {
       $directory=$init_dir;
      }
    
      $dir=opendir($directory);
      $k=0;
      $on_row=IMAGES_ROW;
    
    
      if (file_exists($directory."files.txt")) {
       $data=LoadFile($directory."files.txt");
       $lines=explode("\n", $data);
       for($i=0;$i<count($lines);$i++) {
        $ar=explode(' ', trim($lines[$i]));
        $tmp=array_shift($ar);
        $descriptions[$tmp]=implode(' ', $ar);
       }
      }
    
      while ($file=readdir($dir)) { 
       if (Is_Dir($directory."/$file") && ($file!='tmp') && ($file!='.') && ($file!='..')) {
        $directories[]=array('FILENAME'=>$file, 'DIR'=>$directory, 'DESCRIPTION'=>$descriptions[$file], 'URL'=>urlencode($directory."$file/"));
       }
       
       if (!Is_file($directory."/$file")) continue;
    
       $ext=strtolower(substr($file, -3));
    
       if ($ext!='gif' && $ext!='jpg') continue;
    
       $rec=array();
       $rec["FILENAME"]=$file;
    
       $size=(int)(filesize($directory."/".$file)/1024);
       if ($size>1024) {
        $rec["SIZE"]=(((int)($size*100/1024))/100)." Mb";
       } else {
        $rec["SIZE"]=$size." Kb";
       }
       if (IsSet($descriptions[$file])) {
        $rec['DESCRIPTION']=$descriptions[$file];
       }
       $rec["DIR"]=$directory;
       if (($k+1)%$on_row==0) {
        $rec['NEXT_ROW']=1;
       }
       $k++;
       $files[]=$rec;
      }
    
      include('./template.php');
    
    // ----------------------------------------------------------------------
    
    function LoadFile($filename) {
     // loading file
     $f=fopen("$filename", "r");
     $data="";
     if ($f) {
      $data=fread($f, filesize($filename));
      fclose($f);
     }
     return $data;
    }
    
    
    ?>
    Code (markup):
     
    thewindmaster, Aug 16, 2007 IP