Tutorials Infos - Anleitungen - Hilfe - Dreamcodes
 

Image Resizer

Dieses Script resized alle Grafiken aus einem Verzeichnis und speichert die neuen Bilder in einem vorgegebenen Verzeichnis

Script:
<html>
<head>
<title>Image-Resizer</title>
</head>
<body>
<?
   
if ( ! isset($_POST["submit"]))
   {
?>
  <div align='center'>
    <center>
  <form action='<? echo $_SERVER["PHP_SELF"]; ?>' method='POST'>
  <table border='0' width='50%'>
    <tr>
      <td>&nbsp;</td>
      <td><font size='5'><b>Image-Resizer</b></font></td>
    </tr>
    <tr>
      <td height='20'>&nbsp;</td>
      <td height='20'>&nbsp;</td>
    </tr>
    <tr>
      <td>Quellverzeichnis:</td>
      <td><input type='text' name='source' size='30'></td>
    </tr>
    <tr>
      <td>Zielverzeichnis:</td>
      <td><input type='text' name='destination' size='30'></td>
    </tr>
    <tr>
      <td>Breite in Pixel:</td>
      <td><input type='text' name='width' size='4'></td>
    </tr>
    <tr>
      <td>Höhe in Pixel:</td>
      <td><input type='text' name='height' size='4'></td>
    </tr>
    <tr>
    <td>Qualität:</td>
    <td><input type='text' name='quality' size='4' value='100'></td>
    </tr>
    <tr>
      <td>Proportional:</td>
      <td><input type='checkbox' name='fixed' checked></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type='submit' name='submit' value='Abschicken'</td>
    </tr>
  </table>
  </form>
    </center>
  </div>
<?
   
}
   else
   {
      
// Bei Timeout-Problemen den Wert erhöhen
      
ini_set("max_execution_time", "3000");

      
$source = $_POST["source"];
      
$dest   = $_POST["destination"];
      
$width  = $_POST["width"];
      
$height = $_POST["height"];
      
$quality = $_POST["quality"];

      if (isset(
$_POST["fixed"]))
        
$fixed   = true;
      else
        
$fixed   = false;

      if (!
strlen ($source))
      {
         echo
"Kein Quellverzeichnis";
         exit;
      }

      if (!
strlen ($dest))
      {
         echo
"Kein Zielverzeichnis";
         exit;
      }

      if (!
strlen ($width) || ! strlen ($height))
      {
         echo
"Keine Größen angegeben";
         exit;
      }

      if (
$fixed)
      {
         if (
strlen ($width))
            
$height = 0;
          else if (
strlen ($height))
            
$widht = 0;
      }

      include_once(
"./functions.inc.php");

      
$data = getImagesFromPath($source);

      if (!
file_exists($dest))
      {
         if ( !
mkdir ($dest, "0777"))
         {
           echo
"Verzeichnis $dest konnte nicht angelegt werden";
           exit;
         }
      }
      foreach(
$data as $name)
      {
          
$in  = $source . "/" . $name;
          
$out = $dest . "/" . $name;
          
$ret = resizeImage($in, $out, $width, $height, $quality);
          if (
$ret)
            echo
"Grafik $out erzeugt<br>\n";
          else
            echo
"<font color='#FF0000'>Grafik $out konnte nicht erzeugt werden</font><br>\n";
      }

   }
?>
</body>
</html>

<?
  
//
  //   functions.inc.php
  //
  
function getImagesFromPath($path)
  {
    
$result = array();

    
$handle=opendir($path);

    while (
$file = readdir ($handle))
    {
       if (
$file != "." && $file != "..")
       {
          if (!
is_dir($file))
          {
            
$tmp = strtolower($file);
            
$sub = substr($tmp, -4);
            if (
$sub == ".png" || $sub == ".jpg" || $sub == ".gif" || $sub == ".bmp")
               
$result[] = $file;
          }
       }
    }

    return
$result;
  }

  function
resizeImage($file_name_src, $file_name_dest, $width=0, $height = 0, $quality=100)
  {

   if (
file_exists($file_name_src)  && isset($file_name_dest))
   {
       if (
$width == 0 && $height == 0)
         return
false;
       
       
$est_src = pathinfo(strtolower($file_name_src));
       
$est_dest = pathinfo(strtolower($file_name_dest));

       
$size = getimagesize($file_name_src);

       if (
$height && ! $width)
       {
         
$h = number_format($height, 0, ',', '');
         
$w = number_format(($size[0]/$size[1])*$height,0,',','');
       }
       else if (!
$height && $width)
       {
         
$w = number_format($width, 0, ',', '');
         
$h = number_format(($size[1]/$size[0])*$width,0,',','');
       }
       else
       {
         
$h = number_format($height, 0, ',', '');
         
$w = number_format($width, 0, ',', '');
       }

       if (
$est_dest['extension'] == "gif" || $est_dest['extension'] == "jpg")
       {
           
$file_name_dest = substr_replace($file_name_dest, 'jpg', -3);
           
$dest = imagecreatetruecolor($w, $h);
           
imageantialias($dest, TRUE);
       } elseif (
$est_dest['extension'] == "png")
       {
           
$dest = imagecreatetruecolor($w, $h);
           
imageantialias($dest, TRUE);
       } else
       {
           return
FALSE;
       }

       switch(
$size[2])
       {
       case
1:      //GIF
           
$src = imagecreatefromgif($file_name_src);
           break;
       case
2:      //JPEG
           
$src = imagecreatefromjpeg($file_name_src);
           break;
       case
3:      //PNG
           
$src = imagecreatefrompng($file_name_src);
           break;
       default:
           return
FALSE;
           break;
       }

       
imagecopyresampled($dest, $src, 0, 0, 0, 0, $w, $h, $size[0], $size[1]);
       
       switch(
$size[2])
       {
       case
1:
       case
2:
           
imagejpeg($dest,$file_name_dest, $quality);
           break;
       case
3:
           
imagepng($dest,$file_name_dest);
       }
       return
TRUE;
   }
   return
FALSE;
}

 
ID: 767
eingestellt am: 25.03.2005
Autor: na
Status zum lesen: Gast
gelesen: 4644
Webseite: www.dreamcodes.com
[Drucken]