Click here to Skip to main content
15,891,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys, I found a php script online that helps me automatically display images in an image folder, including thumbnails. It works well, but I want to sort my pictures with the newest pictures first in the webpage. Can someone help me do this? Many thanks in advance!

PHP
<?php
/* function:  generates thumbnail */
function make_thumb($src,$dest,$desired_width) {
	/* read the source image */
	$source_image = imagecreatefromjpeg($src);
	$width = imagesx($source_image);
	$height = imagesy($source_image);
	/* find the "desired height" of this thumbnail, relative to the desired width  */
	$desired_height = floor($height*($desired_width/$width));
	/* create a new, "virtual" image */
	$virtual_image = imagecreatetruecolor($desired_width,$desired_height);
	/* copy source image at a resized size */
	imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
	/* create the physical thumbnail image to its destination */
	imagejpeg($virtual_image,$dest);
}

/* function:  returns files from dir */
function get_files($images_dir,$exts = array('jpg')) {
	$files = array();
	if($handle = opendir($images_dir)) {
		while(false !== ($file = readdir($handle))) {
			$extension = strtolower(get_file_extension($file));
			if($extension && in_array($extension,$exts)) {
				$files[] = $file;
			}
		}
		closedir($handle);
	}
	return $files;
}

/* function:  returns a file's extension */
function get_file_extension($file_name) {
	return substr(strrchr($file_name,'.'),1);
}

/** settings **/
$images_dir = 'August/';
$thumbs_dir = 'August Thumbnails/';
$thumbs_width = 200;
$images_per_row = 4;

/** generate photo gallery **/
$image_files = get_files($images_dir);
if(count($image_files)) {
	$index = 0;
	foreach($image_files as $index=>$file) {
		$index++;
		$thumbnail_image = $thumbs_dir.$file;
		if(!file_exists($thumbnail_image)) {
			$extension = get_file_extension($thumbnail_image);
			if($extension) {
				make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
			}
		}
		echo '<a href="',$images_dir.$file,'"><img src="',$thumbnail_image,'" /></a>';
		if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
	}
	echo '<div class="clear"></div>';
}
else {
	echo '<p>Hang on! I am still growing.</p>';
}

?>


If you have any questions, or need me to clarify something just let me know.


Jerome
Posted

You need to use filemtime to get a files last modified time

http://php.net/manual/en/function.filemtime.php[^]


next sort the files based on its last modified time.

That is one solution.
 
Share this answer
 
Comments
jump_ace 18-Aug-14 11:47am    
I'm just learning php, can you help me write a solution for sorting them? I'm not even sure where to put the code you linked?
I found a solution online:

PHP
function get_files($images_dir,$exts = array('jpg')) {
    $files = array();
    $times = array();
    if($handle = opendir($images_dir)) {
        while(false !== ($file = readdir($handle))) {
            $extension = strtolower(get_file_extension($file));
            if($extension && in_array($extension,$exts)) {
                $files[] = $file;
                $times[] = filemtime($images_dir . '/' . $file);
            }
        }
        closedir($handle);
    }
    array_multisort($files, SORT_ASC, $times);
    return $files;
}



Jerome
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900