0

我在网上找到了以下 PHP 脚本,它正在获取图像目录并创建缩略图。这部分效果很好,希望我注意到排序已关闭。我能说的最好的是它正在使用文件名,我想使用创建日期,因为我正在对从安全摄像头创建的图像目录进行排序。

任何输入和一点(好的,很多)方向都会很棒,因为我没有很多 PHP 经验,但可以绕过一些。

再次感谢您的帮助!

<html><head><title>Snapshots</title></head>
<body bgcolor='white'>

<?php
$folder = '.';
$dir = getcwd();
DirStat($folder, 0);
chdir($dir);
$FolderSize = ByteSize($FolderSize);
$FileCount=$FileCount-2;
?>

<h2 align=center><?php echo date('m/d/Y H:i:s') ." - $FileCount Snapshots - $FolderSize";?></h4>

<?php

$imagens='';
$dn = opendir('.');
while (false !== ($file = readdir($dn))) {
 if ($file == '.' || $file =='..' || $file =='index.php' || $file =='Thumbs.db'){
        //print "<a href=$file>$file</a><br>";
 }else{
        if (is_dir($file)){
                print "<img src='/imagens/diretorio.png'>&nbsp;<a href='$file?dir=dirname(__FILE__)'>$file</a><br>";
        }else{
                $tamanho = filesize($file);
                $m = 'bytes'; // M�ltiplo
                if ($tamanho>1024) {
                        $tamanho=round($tamanho/1024,2);
                        $m = 'KB';
                } elseif($tamanho > 1024*1024){
                        $tamanho = round(($tamanho/1024)/1024,2);
                        $m = 'MB';
                }

                $imagens .=OutputThumbnail($file, $tamanho, $m);
        }
 }
}

closedir($dn);

print '<br>'.$imagens;

function OutputThumbnail($image_file, $tamanho, $m)
{
        if (file_exists($image_file))
        {
                $size = GetImageSize($image_file);

                if ($size[0] <=64) {
                        $larg=$size[0];
                }elseif ($size[0] > 64 && $size[0] <= 200) {
                        $larg=64;
                }elseif ($size[0] > 201 && $size[0] < 400) {
                        $larg=128;
                }elseif ($size[0] > 401) {
                        $larg=256;
                }

                if ($size[0] == 0) $size[0]=1;

                $alt= ($larg * $size[1])/$size[0];

                return "<a href=$image_file><img width=$larg height=$alt        src=$image_file border=0
                        TITLE='$image_file - $larg x $alt - $tamanho $m'></a>&nbsp;&nbsp;";
        }
}

?>

<?php
function DirStat($directory) {
        global $FolderCount, $FileCount, $FolderSize;

        chdir($directory);
        $directory = getcwd();
        if($open = opendir($directory)) {
                while($file = readdir($open)) {
                        if($file == '..' || $file == '.') continue;
                                if(is_file($file)) {
                                        $FileCount++;
                                        $FolderSize += filesize($file);
                                } elseif(is_dir($file)) {
                                        $FolderCount++;
                                }
                }
                if($FolderCount > 0) {
                        $open2 = opendir($directory);
                        while($folders = readdir($open2)) {
                                $folder = $directory.'/'.$folders;
                                if($folders == '..' || $folders == '.') continue;
                                        if(is_dir($folder)) {
                                                DirStat($folder);
                                        }
                                }
                                closedir($open2);
                        }
                        closedir($open);
                }
}

function ByteSize($bytes) {
        $size = $bytes / 1024;
        if($size < 1024){
                $size = number_format($size, 2);
                $size .= 'KB';
        } else {
                if($size / 1024 < 1024) {
                        $size = number_format($size / 1024, 2);
                        $size .= 'MB';
                } elseif($size / 1024 / 1024 < 1024) {
                        $size = number_format($size / 1024 / 1024, 2);
                        $size .= 'GB';
                } else {
                        $size = number_format($size / 1024 / 1024 / 1024,2);
                        $size .= 'TB';
                }
        }
        return $size;
}

?>
4

1 回答 1

1

我添加了一种非常肮脏的排序方式(效率不高)。在代码中将下面的行更改为 SORT_DESC 或 SORT_ASC 以降序或升序排序。

arraySortByColumn($exifData, 'time', SORT_DESC);

您的脚本添加了排序功能:

<html><head><title>Snapshots</title></head>
<body bgcolor='white'>

<?php
$folder = '.';
$dir = getcwd();
DirStat($folder, 0);
chdir($dir);
$FolderSize = ByteSize($FolderSize);
$FileCount=$FileCount-2;
?>

<h2 align=center><?php echo date('m/d/Y H:i:s') ." - $FileCount Snapshots - $FolderSize";?></h4>

<?php

$imagens='';
$exifData = array();
$dn = opendir('.');
while (false !== ($file = readdir($dn))) {
 if ($file == '.' || $file =='..' || $file =='index.php' || $file =='Thumbs.db'){
        //print "<a href=$file>$file</a><br>";
 }else{
        if (is_dir($file)){
                print "<img src='/imagens/diretorio.png'>&nbsp;<a href='$file?dir=dirname(__FILE__)'>$file</a><br>";
        }else{
                $tamanho = filesize($file);
                $datetime = @exif_read_data($file);
                $m = 'bytes'; //
                if ($tamanho>1024) {
                        $tamanho=round($tamanho/1024,2);
                        $m = 'KB';
                } elseif($tamanho > 1024*1024){
                        $tamanho = round(($tamanho/1024)/1024,2);
                        $m = 'MB';
                }
                $exifData[] = array('time' => $datetime['FileDateTime'], 'file' => $file, 'tamanho' => $tamanho,  'm' => $m );

        }
 }
}

closedir($dn);
//change to SORT_DESC or SORT_ASC
arraySortByColumn($exifData, 'time', SORT_DESC);

foreach ($exifData as $value) {
     $imagens .= OutputThumbnail($value['file'], $value['tamanho'], $value['m']);
}

print '<br>'.$imagens;

function arraySortByColumn(&$arr, $col, $dir = SORT_DESC) {
        $sort_col = array();
        foreach ($arr as $key => $row) {
            $sort_col[$key] = $row[$col];
        }
        array_multisort($sort_col, $dir, $arr);
    }

function OutputThumbnail($image_file, $tamanho, $m)
{
        if (file_exists($image_file))
        {
                $size = GetImageSize($image_file);

                if ($size[0] <=64) {
                        $larg=$size[0];
                }elseif ($size[0] > 64 && $size[0] <= 200) {
                        $larg=64;
                }elseif ($size[0] > 201 && $size[0] < 400) {
                        $larg=128;
                }elseif ($size[0] > 401) {
                        $larg=256;
                }

                if ($size[0] == 0) $size[0]=1;

                $alt= ($larg * $size[1])/$size[0];

                return "<a href=$image_file><img width=$larg height=$alt        src=$image_file border=0
                        TITLE='$image_file - $larg x $alt - $tamanho $m'></a>&nbsp;&nbsp;";
        }
}

?>

<?php
function DirStat($directory) {
        global $FolderCount, $FileCount, $FolderSize;

        chdir($directory);
        $directory = getcwd();
        if($open = opendir($directory)) {
                while($file = readdir($open)) {
                        if($file == '..' || $file == '.') continue;
                                if(is_file($file)) {
                                        $FileCount++;
                                        $FolderSize += filesize($file);
                                } elseif(is_dir($file)) {
                                        $FolderCount++;
                                }
                }
                if($FolderCount > 0) {
                        $open2 = opendir($directory);
                        while($folders = readdir($open2)) {
                                $folder = $directory.'/'.$folders;
                                if($folders == '..' || $folders == '.') continue;
                                        if(is_dir($folder)) {
                                                DirStat($folder);
                                        }
                                }
                                closedir($open2);
                        }
                        closedir($open);
                }
}

function ByteSize($bytes) {
        $size = $bytes / 1024;
        if($size < 1024){
                $size = number_format($size, 2);
                $size .= 'KB';
        } else {
                if($size / 1024 < 1024) {
                        $size = number_format($size / 1024, 2);
                        $size .= 'MB';
                } elseif($size / 1024 / 1024 < 1024) {
                        $size = number_format($size / 1024 / 1024, 2);
                        $size .= 'GB';
                } else {
                        $size = number_format($size / 1024 / 1024 / 1024,2);
                        $size .= 'TB';
                }
        }
        return $size;
}

?>
于 2012-09-27T19:34:00.580 回答