Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Get dimensions of images in dir
    primarykey
    data
    text
    <p>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.</p> <p>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!?</p> <p>This is how far I got:</p> <p><code> checkDir('dir2Check');</p> <pre><code>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 != "." &amp;&amp; $entry != ".." &amp;&amp; $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); } </code></pre> <p>}</p> <pre><code>// 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]&lt;br /&gt;"; } if ($info) { echo echoEntry("FILE", $fileName, $level, $info); } </code></pre> <p>}</p> <pre><code>// 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; </code></pre> <p>}</p> <pre><code>// 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 &lt; $level) { $output .= "____"; $i++; } $output .= $entry; if ($info) { $output .= "IMG_INFO[".$info."]"; } return $output."&lt;br /&gt;"; </code></pre> <p>} </code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload