<?php
/* Functions for working with originals.
*
* Written by: Chris Studholme
* Copyright: GPL (http://www.fsf.org/copyleft/gpl.html)
* $Id: originals.php,v 1.7 2003/06/07 23:28:15 cstudhol Exp $
*/
// obsolete
function read_tiff_data($path) {
$handle = fopen($path,"r");
if (!$handle)
return false;
/*
$buf = fread($handle,12);
if (substr($buf,6,4)!="Exif")
return false;
$length = ord($buf[4])*256 + ord($buf[5]);
$data = fread($handle,$length-8);
$result = array();
*/
$data = fread($handle,1000);
if (ereg("([12][0-9][0-9][0-9]:[01][0-9]:[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9])",substr($data,536,19),$regs)) {
$result["DateTime"]=$regs[1];
}
fclose($handle);
return $result;
}
// obsolete
function date_from_original($path) {
$date = false;
if ($exif=read_exif_data($path)) {
if ($exif["DateTime"])
$date = $exif["DateTime"];
}
else if ($tiff=read_tiff_data($path)) {
if ($tiff["DateTime"])
$date = $tiff["DateTime"];
}
if (ereg("^([12][0-9][0-9][0-9]):([01][0-9]):([0-3][0-9]) ([012][0-9]:[0-5][0-9]:[0-5][0-9])$",$date,$regs)) {
$date = $regs[1]."-".$regs[2]."-".$regs[3]." ".$regs[4];
}
else
$date = false;
return $date;
}
/*
function find_original($dir,$image) {
global $original_dir;
// check for invalid chars
if (ereg("[/\\;'\"`()]",$dir)||
ereg("[/\\;'\"`()]",$image))
return false;
$dh = opendir("$original_dir/$dir");
if ($dh) {
while ($file = readdir($dh)) {
if (ereg("^$image.*[.](jpg|tif|kdc)$",$file)&&
is_file("$original_dir/$dir/$file")&&
is_readable("$original_dir/$dir/$file")) {
$original=$file;
break;
}
}
closedir($dh);
}
return $original;
}
*/
/* Find file matching $pattern (a regex w/o ^ and $). The path to the
* original is returned only in the case that a single unique file matches
* the pattern. False is return in the case that an original cannot be
* found.
*/
function original_find_one($pattern) {
global $path_originals;
if (ereg("^(.*)/([^/]*)$",$pattern,$regs)) {
$dir = $regs[1];
$path = $path_originals."/".$dir;
$pattern = "^".$regs[2]."$";
}
else {
$dir = false;
$path = $path_originals;
$pattern = "^".$pattern."$";
}
if (!is_dir($path))
return false;
$handle = opendir($path);
if (!$handle)
return false;
$result = false;
while ($file = readdir($handle)) {
if (ereg($pattern,$file)&&is_file($path."/".$file)) {
if (!$result)
$result = $file;
else {
closedir($handle);
return false;
}
}
}
closedir($handle);
if ($result)
return $dir ? $dir."/".$result : $result;
return false;
}
/*
*/
function original_find_many() {
}
/* Hack for certain Fujifilm cameras that read_exif_data doesn't like.
*/
function fuji_detect($file) {
$fh = fopen($file,"rb");
if (!$fh)
return false;
if (fseek($fh,158,SEEK_SET)!=0) {
fclose($fh);
return false;
}
$id = fread($fh,8);
fclose($fh);
return ($id=="FUJIFILM");
}
function fuji_date($file) {
$fh = fopen($file,"rb");
if (!$fh)
return false;
if (fseek($fh,244,SEEK_SET)!=0) {
fclose($fh);
return false;
}
$date = fread($fh,19);
fclose($fh);
return vpa_strtotime($date);
}
/* Read embedded date from original and return as UNIX timestamp (integer).
* Returns false in case of failure.
*/
function original_get_date($filename,$mimetype=false) {
if (!$mimetype)
$mimetype = mime_get_type($filename);
switch ($mimetype) {
case "image/jpeg":
if (fuji_detect($filename))
return fuji_date($filename);
$exif = array();
if (function_exists("read_exif_data"))
@$exif=read_exif_data($filename);
else if (function_exists("exif_read_data"))
@$exif=exif_read_data($filename);
if ($exif["DateTime"]) {
$date = vpa_strtotime($exif["DateTime"]);
if ($date>0)
return $date;
}
break;
case "image/tiff":
case "image/x-kdc":
break;
}
return false;
}
// read embedded thumbnail image from original and return as image/jpeg
// binary data
// returns false in case of failure
function original_get_thumbnail($filename,$mimetype=false) {
if (!$mimetype)
$mimetype = mime_get_type($filename);
switch ($mimetype) {
case "image/jpeg":
if (fuji_detect($filename))
return false;
if (function_exists("exif_thumbnail"))
return exif_thumbnail($filename);
$exif = array();
if (function_exists("read_exif_data"))
$exif=read_exif_data($filename);
else if (function_exists("exif_read_data"))
$exif=exif_read_data($filename);
if ($exif["Thumbnail"])
return $exif["Thumbnail"];
break;
case "image/tiff":
case "image/x-kdc":
break;
}
return false;
}
?>