dou91855 2014-08-25 09:20
浏览 38
已采纳

从最新到最旧的PHP排序图像

Hi I have the following script to show images from a directory and make thumbs , but I would like to know how to sort the images from newest to oldest and how to implement it in this script Many Thanks in Advance!

<?php
    # SETTINGS
    $max_width = 800;
    $max_height = 600;
    $per_page = 10;

    $page = $_GET['page'];

    $has_previous = false;
    $has_next = false;

$images = array();
$times = array();
// read the images folder for jpg, jpeg, png and gif images using glob() - see http://php.net/glob for info
foreach(glob('images/*.{jpg,jpeg,png,gif}', GLOB_BRACE) as $image)
{
    // add the file to the images array
    $images[] = $image;

    // get the files creation/last modification timestamp add it to the times array. 
    // This array will be used later to sort the images array
    $times[] = filemtime($image);

   // generate the image thumbnail if needed
   if(!file_exists('thumbs/' . $image))
   {
       // calling your makeThumb function, pass it the file extension for the image
       makeThumb(basename($image), pathinfo($image, PATHINFO_EXTENSION));
   }
}
// using the times array, to sort the images newest to oldest
array_multisort($times , SORT_DESC, 
                $images);



    function getPictures() {
        global $page, $per_page, $has_previous, $has_next;
        if ( $handle = opendir(".") ) {
            $lightbox = rand();
            echo '<ul id="pictures">';

            $count = 0;
            $skip = $page * $per_page;

            if ( $skip != 0 )
                $has_previous = true;

            while ( $count < $skip && ($file = readdir($handle)) !== false ) {
                if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
                    $count++;
            }
            $count = 0;
            while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
    if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {

        // make the thumbs directory if it doesn't already exist
        if ( ! is_dir('thumbs') ) {
            mkdir('thumbs');
        }
        // make a thumbnail if it doesn't already exist
        if ( ! file_exists('thumbs/'.$file) ) {
            makeThumb( $file, $type );
        }

        // create a link to $file, add the thumbnail
        echo '<li><a href="' . $file . '">';
        echo '<img src="thumbs/'.$file.'" alt="" /></a></li>';
        $count++;

echo substr($file,strlen($folder),strpos($file, '.')-strlen($folder));

    }
}

            echo '</ul>';

            while ( ($file = readdir($handle)) !== false ) {
                if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
                    $has_next = true;
                    break;
                }
            }
        }
    }

    function getPictureType($file) {
        $split = explode('.', $file); 
        $ext = $split[count($split) - 1];
        if ( preg_match('/jpg|jpeg/i', $ext) ) {
            return 'jpg';
        } else if ( preg_match('/png/i', $ext) ) {
            return 'png';
        } else if ( preg_match('/gif/i', $ext) ) {
            return 'gif';
        } else {
            return '';
        }
    }

    function makeThumb( $file, $type ) {
        global $max_width, $max_height;
        if ( $type == 'jpg' ) {
            $src = imagecreatefromjpeg($file);
        } else if ( $type == 'png' ) {
            $src = imagecreatefrompng($file);
        } else if ( $type == 'gif' ) {
            $src = imagecreatefromgif($file);
        }
        if ( ($oldW = imagesx($src)) < ($oldH = imagesy($src)) ) {
            $newW = $oldW * ($max_width / $oldH);
            $newH = $max_height;
        } else {
            $newW = $max_width;
            $newH = $oldH * ($max_height / $oldW);
        }
        $new = imagecreatetruecolor($newW, $newH);
        imagecopyresampled($new, $src, 0, 0, 0, 0, $newW, $newH, $oldW, $oldH);
        if ( $type == 'jpg' ) {
            imagejpeg($new, 'thumbs/'.$file);
        } else if ( $type == 'png' ) {
            imagepng($new, 'thumbs/'.$file);
        } else if ( $type == 'gif' ) {
            imagegif($new, 'thumbs/'.$file);
        }
        imagedestroy($new);
        imagedestroy($src);
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UFT-8" />
<title>Pictures</title>
<link rel="stylesheet" href="css/lightbox.css" type="text/css" media="screen" />
<style type="text/css">
body {
    width:780px;
    margin:0 auto;
}
#pictures li {
    float:left;
    height:<?php echo ($max_height + 10); ?>px;
    list-style:none outside;
    width:<?php echo ($max_width + 10); ?>px;
    text-align:center;
}
img {
    border:0;
    outline:none;
}
.prev {
    float:left;
}
.next {
    float:right;
}
</style>
</head>
<body>

<?php getPictures(); ?>

<div style="clear:both"></div>

<?php
    if ( $has_previous )
        echo '<p class="prev"><a href="?page='.($page - 1).'">&larr; Previous Page</a></p>';

    if ( $has_next )
        echo '<p class="next"><a href="?page='.($page + 1).'">Next Page &rarr;</a></p>';
?>

<div style="clear:both"></div>

<script type="text/javascript" src="js/prototype.js"></script>
<script type="text/javascript" src="js/scriptaculous.js?load=effects,builder"></script>
<script type="text/javascript" src="js/lightbox.js"></script>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongshou2024 2014-08-25 10:15
    关注

    You can use filemtime($file); for getting the last modification date of your file. You can store your files in an array and use the timestamps as keys. You can sort the array contents by the keys with php easily and just foreach through the sorted array and output it like that. Another advice: You should store your file paths, extensions, upload/modification dates and file sizes in database, it would save much work for you. I hope my answer was helpful. The code:

    <?php
    function sort_images($basedir){
      $dir = scandir($basedir);
      $files = array();
      foreach($dir as $filename){
        $mimetype = mime_content_type($filename);
        if(explode('/', $mimetype)[0] == 'image'){
          $mod_time = filemtime(basedir.'/'.$filename;
          $files[$mod_time] = $basedir.'/'.$filename;
        }
      }
    
      $sorted = ksort($files);
      return $sorted;
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1   /DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UFT-8" />
    <title>Pictures</title>
    </head>
    <body>
    <?php foreach(sort_images('foo/bar') as $image): ?>
    <div class="image"><img src="<?php echo $image; ?>" />
    <?php endforeach; ?>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀