2012-03-21 7 views
1

私は並べ替える必要がある巨大な写真があります。私は知るために各写真の寸法を知る必要があり、サイズを変更する必要があります。プログラマとして、私はこれを行うより速い方法が必要であると確信しています。PHP dirの画像の寸法を取得

私はかなり遠くになりました。次のコードは、dirおよびすべてのサブディレクトリを読み取ります。しかし、私が寸法を抽出しようとすると、ループはチェックする必要があるすべての画像の8%で停止します。それはPHPは、より多くの計算を行うことはできませんできますか?何が起こっている!?

これは私が得たどのくらいです:

checkDir('dir2Check');

function checkDir($dir, $level = 0) { 
if ($handle = opendir($dir)) { 
    while (false !== ($entry = readdir($handle))) { 
     if (!preg_match('/\./i', $entry)) { 
      echo echoEntry("DIR\\", $entry, $level); 
      checkDir($dir.'/'.$entry, $level+1); 
     } else { 
      if ($entry != "." && $entry != ".." && $entry != ".DS_Store") { 
       // if I comment the next line. It loops through all the files in the directory 
       checkFile($entry, $dir.'/'.$entry, $level); 

       // this line echoes so I can check or it really read all the files in case I comment the proceeding line 
       //echo echoEntry("FILE", $entry, $level); 
      } 
     } 
    } 
    $level--; 
    closedir($handle); 
} 

}

// Checks the file type and lets me know what is happening 
function checkFile($fileName, $fullPath, $level) { 
if (preg_match('/\.gif$/i', $fullPath)) { 
    $info = getImgInfo(imagecreatefromgif($fullPath)); 
} else if (preg_match('/\.png$/i', $fullPath)) { 
    $info = getImgInfo(imagecreatefrompng($fullPath)); 
} else if (preg_match('/\.jpe?g$/i', $fullPath)){ 
    $info = getImgInfo(imagecreatefromjpeg($fullPath)); 
} else { 
    echo "XXX____file is not an image [$fileName]<br />"; 
} 

if ($info) { 
    echo echoEntry("FILE", $fileName, $level, $info); 
} 

}

// get's the info I need from the image and frees up the cache 
function getImgInfo($srcImg) { 
$width = imagesx($srcImg); 
$height = imagesy($srcImg); 
$info = "Dimensions:".$width."X".$height; 

imagedestroy($srcImg); 
return $info; 

}

// this file formats the findings of my dir-reader in a readable way 
function echoEntry($type, $entry, $level, $info = false) { 
$output = $type; 

$i = -1; 
while ($i < $level) { 
    $output .= "____"; 
    $i++; 
} 

$output .= $entry; 

if ($info) { 
    $output .= "IMG_INFO[".$info."]"; 
} 

return $output."<br />"; 

}

+0

あなたは私たちにエラーをもらえますか? –

+0

'php.ini'の' max_execution_time'はどのように見えますか? –

答えて

3

は、あなたが何に似てい使用することができ、それだけではPHPののDirectoryIteratorを使用していた私の謙虚で意見はより洗練されたものですOOP-y

<?php 

function walkDir($path = null) { 
    if(empty($path)) { 
     $d = new DirectoryIterator(dirname(__FILE__)); 
    } else { 
     $d = new DirectoryIterator($path); 
    } 

    foreach($d as $f) { 
     if(
      $f->isFile() && 
      preg_match("/(\.gif|\.png|\.jpe?g)$/", $f->getFilename()) 
     ) { 
      list($w, $h) = getimagesize($f->getPathname()); 
      echo $f->getFilename() . " Dimensions: " . $w . ' ' . $h . "\n"; 
     } elseif($f->isDir() && $f->getFilename() != '.' && $f->getFilename() != '..') { 
      walkDir($f->getPathname()); 
     } 
    } 
} 

walkDir(); 
+0

:D私は解決策が大好きです。良くやった!しかし、私はまだ私のスクリプトがいくつかの行の後に止まったのか疑問に思う。あなたは知っていますか? – SKuijers

+0

スクリプトは現在動作しています。あなたのご意見ありがとうございます。私のスクリプトを止めるのは、大きすぎる場所やimagecreatefromjpeg($ fullPath)の画像が十分なメモリがない場合に失敗するためです。これを増やすことで解決しました:ini_set( 'memory_limit'、 '500M'); – SKuijers

1

あなたは、単に以下のgetimagesize()

list($width, $height) = getimagesize($imgFile); 
+0

ありがとうございます。これはより軽い解決策です。しかし、私の元々の質問は、 '何が起こっている?'。私のスクリプトがなぜ止まるのか知りましたか? – SKuijers

+1

あなたはディレクトリに大量の画像を持っていると言っていますので、phpが 'max_execution_time 'のために停止する可能性があります。 – safarov

+0

max_execution_timeを20分に設定しましたが、大きすぎる画像やimagecreatefromjpeg($ fullPath)が利用可能なメモリが不足すると失敗するためです。これを増やすことで解決しました:ini_set( 'memory_limit'、 '500M'); – SKuijers

関連する問題