PHP 获取目录中图像的尺寸

2023-12-09

我有大量照片需要整理。我需要知道每张照片的尺寸才能知道,否则需要重新调整大小。作为一名程序员,我相信必须有一种更快的方法来做到这一点。

我已经走了很远了。以下代码读取目录和所有子目录。但当我尝试提取尺寸时,循环在需要检查的所有图片的 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();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PHP 获取目录中图像的尺寸 的相关文章

  • php curl 使用 GET 发送变量 奇怪的结果

    我正在尝试调用远程站点上页面中的网址 决定使用curl 在远程站点上 url 变量显示为 REQUEST Array var1 gt val1 amp var2 gt val2 amp var3 gt val3 被调用的url是 http
  • php 数组中出现意外的 json 输出结构

    我正在尝试转换动态数据 如何从 PHP 获取此 JSON JSON 122240cb 253c 4046 adcd ae81266709a6 item 0 3 这就是我所做的 但它不起作用 PHP json array 122240cb 2
  • CodeIgniter 自定义库未加载

    我是 CodeIgniter 的新手 并尝试用它开发一个相当简单的应用程序 只是一个用于处理想要娱乐中心通行证的员工的注册的表单 我正在尝试将事物分开以使它们更清晰 这是代码 应用程序 控制器 reccenter php class Rec
  • 解决相关代码的低 FPS 问题以计算图像中的偏移

    我正在尝试使用相关性来跟踪对象 我在较大的图像中逐帧找到较小的补丁 为此 我发现补丁中的变化 并且相关性最大的地方 用新补丁更新补丁 我的代码是 cv Mat im float 2 imagePart out cv Mat im float
  • jQuery ajax 调用包含重音字符的 url 将错误的 Uri 从 IE 发送到服务器

    我在使用 IE 发送包含重音字符的 url 时遇到问题 这是一个简单的函数 function runjQueryTest var url test Beyonc get url function 在服务器 PHP 上我记录了请求uri的值
  • 您的要求无法解析为 laravel 的一组可安装软件包

    我使用 5 7v Laravel 和 7 2 1v PHP 和 Composer 最新版本 但是当我想创建新项目时出现这些错误 Your requirements could not be resolved to an installabl
  • 图像处理 - 使用 opencv 进行服装分割

    我正在使用 opencv 进行服装特征识别 第一步 我需要通过从图像中移除脸部和手来分割 T 恤 任何建议表示赞赏 我建议采用以下方法 Use 阿德里安 罗斯布鲁克的用于检测皮肤的皮肤检测算法 谢谢罗莎 格隆奇以获得他的评论 在方差图上使用
  • 在哪里可以获得 PHP 5.3+ 的 runkit DLL 扩展?

    这是一个简单的问题 我在哪里可以获得 PHP 5 3 版本的 runkit 扩展 它的手册 http php net manual en book runkit php http php net manual en book runkit
  • PHP 中标头的使用

    非常简单的问题 这两个 PHP 版本 5 标头调用中哪一个是 最好的 header Not Modified true 304 header HTTP 1 1 304 Not Modified 我很确定第一个是最多价的 但只是好奇如果在 H
  • 使用值填充的 Symfony2 自定义字段类型

    这是先前问题的后续问题Symfony2 自定义表单类型或扩展 https stackoverflow com questions 24079288 symfony2 custom form type or extension 我正在尝试为订
  • 在 foreach 中使用 QueryPath 的多个查找

    我正在使用 QueryPath 和 PHP 这发现 eventdate 没问题 但不会为 dtstart 返回任何内容 qp htmlqp url foreach qp gt find table schedule gt find tr a
  • 如何在 Windows 上安装 Zend 框架

    安装 Zend Framework 就是这么简单 是的 对 好吧 我正在写一本初学者的书 有一件不太详细的事情是最重要的部分 安装该死的东西 浏览了几个小时的快速入门指南后 它只说 下载 Zend 添加包含目录 bla bla 然后就完成了
  • 带倒计时的php循环

    假设我从 400 开始计数器 我将如何执行一个向后运行直到 0 的 foreach 循环 伪代码 i 400 foreach SOMETHING do stuff i for i 400 i gt 0 i do stuff 其他方法 i 4
  • 如何使用更新资源控制器 laravel 4?

    我有带有索引 编辑 更新方法的客户控制器 Route resource customer CustomerController 控制器方法更新 public function update id echo id 我的 HTML 表单
  • 从 Laravel 4 输入生成新数组

    我使用 Input all 从动态生成的表单中获取一些输入 我使用 jQuery 来允许用户添加字段 字段名称为 first names last names 和 emails input 变量现在看起来像这样 array size 4 t
  • PHP 中的 -> 和 :: 有什么区别?

    这个东西困扰我好久了 一直找不到 在 php 中使用 和 gt 之间的类有什么区别 让我举个例子 想象一个名为 MyClass 的类 该类中有一个函数 myFunction 使用有什么区别 MyClass myclass new MyCla
  • 雄辩的第一个 where 子句

    我想知道 Laravel 如何实现雄辩的语法 以便可以静态调用第一个 where 子句User where User where id 23 gt where email email gt first 他们有吗public static f
  • Jquery一键提交多个同名表单

    我有动态创建的循环表单 我需要一键提交所有表单 我正在遵循下面的代码 你能建议我怎么做吗 谢谢
  • PHP 表单 - 带验证蜜罐

    我有以下内容 效果很好 但对垃圾邮件机器人开放 我想放入蜜罐 而不是验证码 下面的代码适用于验证姓名 电子邮件 消息 但我无法让它与蜜罐一起工作 任何人都可以查看 蜜罐 代码并告诉我如何修复它吗 我希望表单给出 success2 不允许垃圾
  • PHPUnit - 模拟 S3Client 无法正常工作

    库 aws aws sdk php 2 PHP 版本 PHP 5 4 24 cli 作曲家 json require php gt 5 3 1 aws aws sdk php 2 require dev phpunit phpunit 4

随机推荐