PHP获取dir中的图像尺寸 [英] PHP Get dimensions of images in dir
问题描述
我有相当的远。以下代码读取目录和所有子目录。但是当我尝试提取尺寸时,循环停止在所有需要检查的图片的8%。是不是PHP不允许做更多的计算?这是怎么回事?
这是我有多远:
checkDir('dir2Check');
function checkDir($ dir,$ level = 0){
if($ handle = opendir($ dir)){
while(false!==($ entry = readdir($ handle))){
if(!preg_match('/ \。 ',$ entry)){
echo echoEntry(DIR\\,$ entry,$ level);
checkDir($ dir。'/'。$ entry,$ level + 1);
} else {
if($ entry!=。&& $ entry!=..&& $ entry!=.DS_Store){
//如果我对下一行发表评论。它循环访问目录
checkFile($ entry,$ dir。'/'。$ entry,$ level)中的所有文件;
//这行回显,所以我可以检查,或者真正读取所有的文件,以防止我注释前一行
// echo echoEntry(FILE,$ entry,$ level) ;
}
}
}
$ level--;
closedir($ handle);
}
}
//检查文件类型,让我知道发生了什么
函数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 {
echoXXX____file不是一个图像[$ fileName]< br />;
}
if($ info){
echo echoEntry(FILE,$ fileName,$ level,$ info);
}
}
//获取图像中需要的信息并释放缓存
函数getImgInfo($ srcImg){
$ width = imagesx($ srcImg);
$ height = imagesy($ srcImg);
$ info =Dimensions:。$ width。X。$ height;
imagingestroy($ srcImg);
return $ info;
}
//这个文件以可读的方式格式化我的目录读取器的结果
function echoEntry($ type,$ entry,$ level,$ info = false){
$ output = $类型;
$ i = -1;
while($ i <$ level){
$ output。=____;
$ i ++;
}
$ output。= $ entry;
if($ info){
$ output。=IMG_INFO [。$ info。];
}
return $ output。< br />;
}
以下内容与您所做的相似,只是使用php的DirectoryIterator,这在我的拙见更清晰,更多OOP-y
<?php
函数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()。 尺寸:。 $ w。 。 $ h。 \\\
;
} elseif($ f-> isDir()&&$ f-> getFilename()!='。&& $ f-> getFilename()!='..' ){
walkDir($ f-> getPathname());
}
}
}
walkDir();
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 />";
}
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();
这篇关于PHP获取dir中的图像尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!