Reputation: 1
I found the following PHP script on the net which is taking a directory of images and creating thumbnails. This part works great, expect I've noticed the sorting is off. Best I can tell is it's using file names, I'd like to use creation date as I'm sorting a directory of images created from a security camera.
Any input and a bit (ok, a lot) of direction would be great as I don't have a lot of PHP experience, but can get around somewhat.
Thanks again for the help!
<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'> <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> ";
}
}
?>
<?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;
}
?>
Upvotes: 0
Views: 152
Reputation: 35
I have added a very dirty way of sorting (not efficient). Change below line to SORT_DESC or SORT_ASC in the code to sort descending or ascending.
arraySortByColumn($exifData, 'time', SORT_DESC);
Your script with added sorting functionality:
<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'> <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> ";
}
}
?>
<?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;
}
?>
Upvotes: 1