Skip to content

Instantly share code, notes, and snippets.

@melihbuyuk
Created February 14, 2012 21:05
Show Gist options
  • Save melihbuyuk/1830433 to your computer and use it in GitHub Desktop.
Save melihbuyuk/1830433 to your computer and use it in GitHub Desktop.
php exif
public function upload($localFile, $name, $attributes = array())
{
$id = $this->user->getNextId('photo');
if($id === false)
{
$this->logger->crit('Could not fetch next photo ID');
return false;
}
$tagObj = new Tag;
$attributes = $this->whitelistParams($attributes);
$paths = $this->generatePaths($name);
$exiftran = $this->config->modules->exiftran;
if(is_executable($exiftran))
exec(sprintf('%s -ai %s', $exiftran, escapeshellarg($localFile)));
// resize the base image before uploading
$localFileCopy = "{$localFile}-copy";
$this->logger->info("Making a local copy of the uploaded image. {$localFile} to {$localFileCopy}");
copy($localFile, $localFileCopy);
$baseImage = $this->image->load($localFileCopy);
if(!$baseImage)
{
$this->logger->warn('Could not load image, possibly an invalid image file.');
return false;
}
$baseImage->scale($this->config->photos->baseSize, $this->config->photos->baseSize);
$baseImage->write($localFileCopy);
$uploaded = $this->fs->putPhotos(
array(
array($localFile => $paths['pathOriginal']),
array($localFileCopy => $paths['pathBase'])
)
);
if($uploaded)
{
$this->logger->info("Photo ({$id}) successfully stored on the file system");
$exif = $this->readExif($localFile);
$iptc = $this->readIptc($localFile);
$defaults = array('title', 'description', 'tags', 'latitude', 'longitude');
foreach($iptc as $iptckey => $iptcval)
{
if($iptckey == 'tags')
{
$iptcval = implode(',', $iptcval);
}
$attributes[$iptckey] = $iptcval;
}
foreach($defaults as $default)
{
if(!isset($attributes[$default]))
$attributes[$default] = null;
}
if(isset($attributes['dateUploaded']) && !empty($attributes['dateUploaded']))
$dateUploaded = $attributes['dateUploaded'];
else
$dateUploaded = time();
if(isset($attributes['dateTaken']) && !empty($attributes['dateTaken']))
$dateTaken = $attributes['dateTaken'];
else
$dateTaken = @$exif['dateTaken'];
if($this->config->photos->autoTagWithDate == 1)
{
$dateTags = sprintf('%s,%s', date('F', $dateTaken), date('Y', $dateTaken));
if(!isset($attributes['tags']) || empty($attributes['tags']))
$attributes['tags'] = $dateTags;
else
$attributes['tags'] .= ",{$dateTags}";
}
if(isset($exif['latitude']))
$attributes['latitude'] = floatval($exif['latitude']);
if(isset($exif['longitude']))
$attributes['longitude'] = floatval($exif['longitude']);
if(isset($attributes['tags']) && !empty($attributes['tags']))
{
$attributes['tags'] = $tagObj->sanitizeTagsAsString($attributes['tags']);
}
$attributes = array_merge(
$this->getDefaultAttributes(),
array(
'hash' => sha1_file($localFile),
'size' => intval(filesize($localFile)/1024),
'exifCameraMake' => @$exif['cameraMake'],
'exifCameraModel' => @$exif['cameraModel'],
'exifFNumber' => @$exif['FNumber'],
'exifExposureTime' => @$exif['exposureTime'],
'exifISOSpeed' => @$exif['ISO'],
'exifFocalLength' => @$exif['focalLength'],
'width' => @$exif['width'],
'height' => @$exif['height'],
'dateTaken' => $dateTaken,
'dateTakenDay' => date('d', $dateTaken),
'dateTakenMonth' => date('m', $dateTaken),
'dateTakenYear' => date('Y', $dateTaken),
'dateUploaded' => $dateUploaded,
'dateUploadedDay' => date('d', $dateUploaded),
'dateUploadedMonth' => date('m', $dateUploaded),
'dateUploadedYear' => date('Y', $dateUploaded),
'pathOriginal' => $paths['pathOriginal'],
'pathBase' => $paths['pathBase']
),
$attributes
);
$stored = $this->db->putPhoto($id, $attributes);
unlink($localFile);
unlink($localFileCopy);
if($stored)
{
if(isset($attributes['tags']) && !empty($attributes['tags']))
$tagObj->updateTagCounts(array(), (array)explode(',', $attributes['tags']), $attributes['permission'], $attributes['permission']);
$this->logger->info("Photo ({$id}) successfully stored to the database");
return $id;
}
else
{
$this->logger->warn("Photo ({$id}) could NOT be stored to the database");
return false;
}
}
$this->logger->warn("Photo ({$id}) could NOT be stored to the file system");
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment