photo/application/classes/Task/Photo/Import.php
2015-06-04 14:25:58 +10:00

84 lines
2.6 KiB
PHP

<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* Import photo to the database
*
* @package Photo
* @category Tasks
* @author Deon George
* @copyright (c) 2014 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Task_Photo_Import extends Minion_Task {
private $_accepted = array(
'jpg',
);
protected $_options = array(
'file'=>NULL, // Photo File to Import
'verbose'=>FALSE, // Photo File to Import
);
protected function _execute(array $params) {
if (is_null($params['file']))
throw new Kohana_Exception('Missing filename, please use --file=');
if (preg_match('/@__thumb/',$params['file']) OR preg_match('/\/._/',$params['file']))
return sprintf("Ignoring file [%s]\n",$params['file']);
if (! in_array(strtolower(pathinfo($params['file'],PATHINFO_EXTENSION)),$this->_accepted))
return sprintf("Ignoring file [%s]\n",$params['file']);
$po = ORM::factory('Photo',array('filename'=>$params['file']));
if (! $po->loaded())
$po->filename = realpath($params['file']);
$po->date_taken = $this->dbcol(strtotime($po->io('exif:DateTime')));
$po->signature = $this->dbcol($po->io()->getImageSignature());
$po->make = $this->dbcol($po->io('exif:Make'));
$po->model = $this->dbcol($po->io('exif:Model'));
$po->height = $this->dbcol($po->io()->getImageheight());
$po->width = $this->dbcol($po->io()->getImageWidth());
$po->orientation = $this->dbcol($po->io()->getImageOrientation());
$po->subsectime = $this->dbcol($po->io('exif:SubSecTimeOriginal'));
$po->gps_lat = $this->dbcol($po->gps(preg_split('/,\s?/',$po->io()->getImageProperty('exif:GPSLatitude')),$po->io()->getImageProperty('exif:GPSLatitudeRef')));
$po->gps_lon = $this->dbcol($po->gps(preg_split('/,\s?/',$po->io()->getImageProperty('exif:GPSLongitude')),$po->io()->getImageProperty('exif:GPSLongitudeRef')));
try {
$po->thumbnail = $this->dbcol(exif_thumbnail($po->filename));
} catch (Exception $e) {
}
switch ($params['verbose']) {
case 1:
print_r($po->what_changed());
break;
case 2:
print_r($po->io()->getImageProperties());
break;
}
if ($po->list_duplicate()->find_all()->count())
$po->duplicate = '1';
if (! $po->changed())
return sprintf("Image [%s] already in DB: %s\n",$params['file'],$po->id);
$po->save();
if ($po->saved())
return sprintf("Image [%s] stored in DB: %s\n",$params['file'],$po->id);
return sprintf("Image [%s] processed in DB: %s\n",$params['file'],$po->id);
}
// Force the return of a string or NULL
private function dbcol($val,$noval=NULL) {
return $val ? (string)$val : $noval;
}
}
?>