简体   繁体   English

PHP获取目录中图像的尺寸

[英]PHP Get dimensions of images in dir

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. 但是,当我尝试提取尺寸时,循环停止在需要检查的所有图片的8%。 Could it be PHP is not allowed to do more calculations? 难道不允许PHP做更多的计算吗? 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 />"; 

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

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

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

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

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

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

}

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的DirectoryIterator,以我的拙见,它更干净,更糟糕。

<?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();

您可以简单地使用getimagesize()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM