2012-03-21 87 views
1

我有一大堆需要排序的照片。我需要知道每幅照片的尺寸才能知道或需要重新調整尺寸。作爲一名程序員,我深信必須有更快捷的方式來做到這一點。PHP獲取圖像的尺寸

我走得很遠。以下代碼讀取目錄和所有子目錄。但是,當我嘗試提取尺寸時,循環停止在需要檢查的所有圖片的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

你也可以給我們錯誤嗎? – 2012-03-21 17:45:55

+0

你的'php.ini'中的'max_execution_time'看起來像什麼? – 2012-03-21 17:47:52

回答

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 2012-03-21 18:17:33

+0

該腳本正在工作。感謝您的輸入。停止我的腳本的事情是因爲如果沒有足夠的可用內存,圖片太大而imagecreatefromjpeg($ fullPath)會失敗。它通過增加這個來解決:ini_set('memory_limit','500M'); – SKuijers 2012-03-28 17:17:05

1

您可以簡單地使用getimagesize()

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

謝謝你。這是一個更輕的解決方案。但我最初的問題是, '發生什麼事?'。你會碰巧知道我的腳本爲什麼停止? – SKuijers 2012-03-21 18:17:12

+1

你說你在目錄中有大量的圖片,所以一段時間後,php可能會因爲'max_execution_time'停止, – safarov 2012-03-21 18:20:12

+0

我已經將max_execution_time設置爲20分鐘,但那不是阻止我的腳本的東西。這是因爲如果沒有足夠的內存可用,圖片太大而且imagecreatefromjpeg($ fullPath)失敗。它通過增加這個來解決:ini_set('memory_limit','500M'); – SKuijers 2012-03-28 17:15:33