2012-03-21 127 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