jpgDate
Posted: Wed Nov 14, 2007 9:02 am
for those of you who have unorganized digital pictures
jpgdate.class.php
sample usage
My image folder before:
My image folder after:
jpgdate.class.php
Code: Select all
<?php
/**
* This is a class to scan a directory for jpg images, scan the meta data for
* each jpg found, convert the date it was taken into a string, and rename or
* copy each image into a file with the date in it.
*
* This would be useful for those of you who don't organize your digital images.
* (like me)
*
* @date November 14th, 2007
* @author Scott Martin <smp_info[at]yahoo[dot]com>
*/
class jpgDate
{
/**
* The directory with jpg images in it that you want to scan and date
*/
private $_directory;
/**
* The syntax of the date you want to add into the filename of images
*/
private $_dateSyntax = 'n-d-Y';
/**
* Whether or not to overwrite images, or rename them. Value may be true
* or false boolean
*/
private $_overwrite = false;
/**
* The position to add the date onto the file name. Values can be prepend
* (before) or append (after)
*/
private $_pos = 'prepend';
/**
* Contain array for jpg images found
*/
private $_jpgs = array();
/**
* Sets the directory to scan for jpg images
* @param string $directory
* @access public
*/
public function setDirectory($directory)
{
if (is_dir($directory))
{
if ((substr($directory, -1) == '/') || (substr($directory, -1) == '\\'))
{
$directory = substr($directory, 0, strlen($directory)-1);
}
$this->_directory = $directory;
} else
{
trigger_error('jpgDate: Cannot find directory ' . $directory, E_USER_ERROR);
}
}
/**
* Sets the syntax to use for dating the images
* @param strying $syntax - syntax values from http://www.php.net/date
* @access public
*/
public function setDateSyntax($syntax)
{
$this->_dateSyntax = $syntax;
}
/**
* Sets the position to add the date string in the file name
* @param string $pos - 'prepend' or 'append'
* @access public
*/
public function setPos($pos)
{
if (in_array($pos, array('prepend', 'append')))
{
$this->_pos = $pos;
} else
{
trigger_error('jpgDate: setPos() value must be prepend or append, ' . $pos . ' given', E_USER_ERROR);
}
}
/**
* Sets whether this script should overwrite existing image files with
* the new name, or create a copy of the file with the new name.
* @param boolean $bool - true = overwrite, false = don't overwrite
* @access public
*/
public function setOverwrite($bool)
{
if (is_bool($bool))
{
$this->_overwrite = $bool;
} else
{
trigger_error('jpgDate: setOverwrite() expecting boolean true or false, ' . $bool . ' given', E_USER_ERROR);
}
}
/**
* Runs through the methods
* @access public
*/
public function run()
{
if ($this->_hasJPGs())
{
$this->_getJPGDates();
$this->_dateJPGs();
$this->_writeJPGs();
} else
{
trigger_error('jpgDate: Jpg images were not found in the given directory', E_USER_ERROR);
}
}
/**
* Generates a list of valid jpg extensions, then checks the directory to
* see if any jpg's exist in that directory
* @return boolean
* @access private
*/
private function _hasJPGs()
{
foreach (glob($this->_directory . DIRECTORY_SEPARATOR . '*.*') AS $file)
{
if (preg_match("/.+?\.(jpg|jpeg)/i", $file))
{
$this->_jpgs[array_pop(explode(DIRECTORY_SEPARATOR, $file))] = '';
}
}
return !empty($this->_jpgs);
}
/**
* Scans each found jpg for meta data, then checks if it has a FileDateTime attribute
* @access private
*/
private function _getJPGDates()
{
foreach ($this->_jpgs AS $jpg => $time)
{
//var_dump($this->_jpgs);
if ($exif = exif_read_data($this->_directory . DIRECTORY_SEPARATOR . $jpg, 'COMPUTED'))
{
if (array_key_exists('FileDateTime', $exif))
{
$this->_jpgs[$jpg] = $exif['FileDateTime'];
} else
{
$this->_jpgs[$jpg] = filemtime($jpg);
}
} else
{
$this->_jpgs[$jpg] = filemtime($jpg);
}
}
}
/**
* Stores appropriate filenames with dates in the class member
* @access private
*/
private function _dateJPGs()
{
foreach ($this->_jpgs AS $jpg => $time)
{
if ($this->_pos == 'prepend')
{
$filename = date($this->_dateSyntax, $time) . '-' . $jpg;
} else
{
$ext = strstr($jpg, ".");
$filename = str_replace($ext, '', $jpg);
$filename = $filename . '-' . date($this->_dateSyntax, $time) . $ext;
}
$this->_jpgs[$jpg] = $filename;
}
}
/**
* Writes the new files names, be it a new file or renaming old files
* @access private
*/
private function _writeJPGs()
{
if ($this->_overwrite == false)
{
foreach ($this->_jpgs AS $original => $new)
{
if (!empty($new))
{
if (!copy($this->_directory . DIRECTORY_SEPARATOR . $original, $this->_directory . DIRECTORY_SEPARATOR . $new))
{
trigger_error('jpgDate: Could not save image (' . $original . ') with new image name, check permissions on the directory', E_USER_NOTICE);
}
}
}
} else
{
foreach ($this->_jpgs AS $original => $new)
{
if (!empty($new))
{
if (!rename($this->_directory . DIRECTORY_SEPARATOR . $original, $this->_directory . DIRECTORY_SEPARATOR . $new))
{
trigger_error('jpgDate: Could not rename image (' . $original . '), check permissions on the image', E_USER_NOTICE);
}
}
}
}
}
}Code: Select all
require_once 'jpgdate.class.php';
$jpgDate = new jpgDate();
$jpgDate->setDirectory('C:\Users\scott\Pictures\pictures');
$jpgDate->setDateSyntax('n-d-y-gi-A'); //this defaults to 'n-d-Y'
$jpgDate->run();Code: Select all
picturea.JPG
pictureb.JPG
picturec.JPG
pictured.JPG
picturee.JPG
picturef.JPGCode: Select all
picturea.JPG
pictureb.JPG
picturec.JPG
pictured.JPG
picturee.JPG
picturef.JPG
1-11-2007-658-PM-picturea.JPG
1-11-2007-701-PM-pictureb.JPG
1-11-2007-701-PM-picturec.JPG
3-10-2007-858-AM-pictured.JPG
4-25-2007-902-AM-picturee.JPG
10-31-2007-811-PM-picturef.JPG