2

I have a huge ammount of photos that need sorting through. I need to know the dimensions of each photo in order to know or it needs re-sizing. As a programmer I'm convinced there must be a quicker way of doing this.

I got quite far. The following code reads the dir and all the sub dirs. But the moment I try to extract the dimensions the loop halts at 8% of all the pictures that need checking. Could it be PHP is not allowed to do more calculations? What is going on!?

This is how far I got:

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 />";

}

SKuijers
  • 406
  • 7
  • 18

2 Answers2

4

The following does similar to what you do, only it's using php's DirectoryIterator which in my humble opinion is cleaner and more 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();
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
  • :D I love the solution. Nice job! But I still wonder why my script stopped after a few lines. Do you know? – SKuijers Mar 21 '12 at 18:17
  • The script is working now. Thanks for your input. The thing stopping my script was because the pictures where too big and imagecreatefromjpeg($fullPath) fails if not enough memory available. It was solved by increasing this: ini_set('memory_limit', '500M'); – SKuijers Mar 28 '12 at 17:17
1

You can simply use getimagesize()

  list($width, $height) = getimagesize($imgFile);
Josh
  • 8,082
  • 5
  • 43
  • 41
safarov
  • 7,793
  • 2
  • 36
  • 52
  • Thanks man. This is a lighter solutions. But my original question was; 'what is happening?'. Would you happened to know why my script stops? – SKuijers Mar 21 '12 at 18:17
  • 1
    You said u have huge amount of images in directory, so after time php may stop due to `max_execution_time` – safarov Mar 21 '12 at 18:20
  • I have set the max_execution_time to 20 minutes but that was not what was stopping my script. It was because the pictures where too big and imagecreatefromjpeg($fullPath) fails if not enough memory available. It was solved by increasing this: ini_set('memory_limit', '500M'); – SKuijers Mar 28 '12 at 17:15