ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

PHP获取目录中的图像尺寸

2019-10-09 00:34:52  阅读:179  来源: 互联网

标签:dir readdir php image-processing


我有很多需要整理的照片.我需要知道每张照片的尺寸才能知道或需要重新调整尺寸.作为程序员,我确信必须有更快的方法来做到这一点.

我走得很远.以下代码读取目录和所有子目录.但是当我尝试提取尺寸时,循环停止在所有需要检查的图片的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 />";

}

解决方法:

以下内容与您的操作类似,只是使用了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();

标签:dir,readdir,php,image-processing
来源: https://codeday.me/bug/20191008/1875295.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有