• Sort images from newest to oldest PHP

    Ganesh Member

    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.

    ';
    
                $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 '
  • '; echo '
  • '; $count++; echo substr($file,strlen($folder),strpos($file, '.')-strlen($folder)); } } echo '
'; 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); } ?> Pictures
← Previous Page

'; if ( $has_next ) echo '

Next Page →

'; ?>

cobusbo is offline View user’s profile Send private message

  • SapnaVishwas Member

    Assuming the above code is yours, then have a look at :

    http://php.net/manual/en/function.filemtime.php

    simply read the file modification time of the picture into an array alongside with the picture (file) name, run a asort over the array and there you go, then loop through the array and output it.

    the correct place is here :

    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 '
  • '; echo '
  • '; $count++; echo substr($file,strlen($folder),strpos($file, '.')-strlen($folder)); } }
  • Viewing 1 reply thread
    • You must be logged in to reply to this topic.
    en_USEnglish