Categories
PHP Programming

Sorting your photo collection by date the PHP way

Ever wanted to sort your photo collection in the order by date taken? Can’t find the right program that will do it for you? I decided that I had a large enough collection myself to warrant organizing, but I could not find a program that can organize it the way I want it. I have always wanted the photos to be sorted by the date taken and grouped by the year, so I decided to create a short script for myself.

Before I get into the code, you’ll want to check that your PHP settings will allow the script to run by making sure the needed DLLs are enabled. The script uses the exif extension, but will also require mbstring enabled:

extension=php_mbstring.dll
extension=php_exif.dll

Also note that mbstring must be loaded before the exif DLL. Visit PHP.net for more information.

The Code

You must forgive me for the lackluster comments as I didn’t think I’d be sharing this code. The code isn’t too hard to read, and in my opinion is straight forward for the average programmer. For those that are beginners just remember to set the $dir and $target_dir variables for this script to work.

// your picture directory
$dir = "";

// your target directory to dump the sorted pictures
$target_dir = "";


// this script might take awhile
set_time_limit(0);

// recursive function to grab everything in subdirectories
function get_files($dir, $files = array()) {
	foreach(glob($dir . '*') as $file) {
		if(is_file($file)) {
			$files[] = $file;
		}
		elseif(is_dir($file)) {
			$files = get_files($file, $files);
		}
	}
	
	return $files;
}

$files = get_files($dir);

// loop through all the files
foreach($files as $file) {
	$dirname = dirname($file);
	
	// returns array if image file is readable
	$exif = exif_read_data($file, 0, true);
	$timestamp = null;
	$filename = basename($file);
	
	if($exif) {
		// prioritize the available datetimes
		if(isset($exif['EXIF']['DateTimeOriginal'])) {
			$timestamp = str_replace(array(':', ' '), '', $exif['EXIF']['DateTimeOriginal']);
		}
		elseif(isset($exif['EXIF']['DateTimeDigitized'])) {
			$timestamp = str_replace(array(':', ' '), '', $exif['EXIF']['DateTimeDigitized']);
		}
		elseif(isset($exif['IFD0']['DateTime'])) {
			$timestamp = str_replace(array(':', ' '), '', $exif['IFD0']['DateTime']);
		}
		elseif(isset($exif['FILE']['FileDateTime'])) {
			$timestamp = date('YmdHis', $exif['FILE']['FileDateTime']);
		}
		
		// bad timestamp
		if($timestamp == '00000000000000') {
			echo "0000 $file";
		}
	}
	else {
	}
	
	// if timestamp is good
	if($timestamp and $filename) {
		$new_filename = str_replace(' ', '_', ("{$timestamp}_{$filename}"));
		$folder = rtrim($target_dir, '\') . '\' . substr($timestamp, 0, 4);
		if(!is_dir($folder)) {
			mkdir($folder);
		}
		if(is_file($folder . '\' . $new_filename)) {
			echo $new_filename . ' already exists';
		}
		copy($file, $folder . '\' . $new_filename);
	}
	else {
		// timestamp wasn't good and this file was not processed
		echo "ignored $file";
	}
}

To give an idea what this script will do, if I have three files I want to sort:

00001.JPG
IMG_001.JPG
PIC001.JPG

The script will determine the timestamps of each file and place them in folders belonging to its years, while prefixing the filenames with the timestamps:

2011
	20110101131201_PIC001.JPG
	20110503111630_IMG_001.JPG
2012
	20120207101411_00001.JPG

It’s not the greatest script but I’m happy with my results.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s