summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Almdal <tnalmdal@shaw.ca>2009-02-10 16:28:50 +0000
committerTim Almdal <tnalmdal@shaw.ca>2009-02-10 16:28:50 +0000
commit19f29fb1d6dae4dc9e290c2cc7112401c328a59a (patch)
treedaae35f8aeb71b798443cac7320a027bcb092c66
parent9ad072b33fb091c7cad901849662d0b865a2410b (diff)
Exif data is now collected when an image is added.
-rw-r--r--modules/exif/helpers/exif.php115
-rw-r--r--modules/exif/helpers/exif_event.php30
-rw-r--r--modules/exif/helpers/exif_installer.php12
-rw-r--r--modules/exif/models/exif_key.php21
-rw-r--r--modules/exif/tests/Exif_Test.php56
-rw-r--r--modules/exif/tests/data/image.jpgbin0 -> 11211 bytes
6 files changed, 234 insertions, 0 deletions
diff --git a/modules/exif/helpers/exif.php b/modules/exif/helpers/exif.php
new file mode 100644
index 00000000..61084b15
--- /dev/null
+++ b/modules/exif/helpers/exif.php
@@ -0,0 +1,115 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/**
+ * This is the API for handling exif data.
+ */
+class exif_Core {
+
+ protected static $exif_keys;
+
+ public static function extract($item) {
+ // Only try to extract exif from photos
+ if ($item->is_photo() && $item->mime_type == "image/jpeg") {
+ require_once(MODPATH . "exif/lib/exif.php");
+ $exif_raw = read_exif_data_raw($item->file_path(), false);
+ if (isset($exif_raw['ValidEXIFData'])) {
+ foreach(self::_keys() as $field => $exifvar) {
+ if (isset($exif_raw[$exifvar[0]][$exifvar[1]])) {
+ $exif_key = ORM::factory("exif_key");
+ $exif_key->item_id = $item->id;
+ $exif_key->name = $field;
+ $exif_key->value = $exif_raw[$exifvar[0]][$exifvar[1]];
+ $exif_key->summary = $exifvar[3];
+ $exif_key->save();
+ }
+ }
+ }
+ $size = getimagesize($item->file_path(), $info);
+ if (is_array($info) && !empty($info["APP13"])) {
+ $iptc = iptcparse($info["APP13"]);
+ foreach (array("Keywords" => "2#025", "Caption" => "2#120") as $keyword => $iptc_key) {
+ if (!empty($iptc[$iptc_key])) {
+ $exif_key = ORM::factory("exif_key");
+ $exif_key->item_id = $item->id;
+ $exif_key->name = $keyword;
+ $exif_key->value = implode(" ", $iptc[$iptc_key]);
+ $exif_key->summary = false;
+ $exif_key->save();
+ }
+ }
+ }
+ }
+ }
+
+ public static function get($item, $summary=true) {
+ $exif = array();
+ $exif_key = ORM::factory("exif_key")
+ ->where("item_id", $item->id);
+ if ($summary) {
+ $exif_key->where("summary", $summary);
+ }
+ $keys = $exif_key->find_all();
+ $definitions = self::_keys();
+ foreach ($keys as $key) {
+ $exif[] = array("caption" => $definitions[$key->name][2], "value" => $key->value);
+ }
+
+ return $exif;
+ }
+
+
+ private static function _keys() {
+ if (!isset(self::$exif_keys)) {
+ self::$exif_keys = array(
+ "Make" => array("IFD0", "Make", t("Camera Maker"), true),
+ "Model" => array("IFD0", "Model", t("Camera Model"), true),
+ "Aperture" => array("SubIFD", "FNumber", t("Aperture"), true),
+ "ColorSpace" => array("SubIFD", "ColorSpace", t("Color Space"), true),
+ "ExposureBias" => array("SubIFD", "ExposureBiasValue", t("Exposure Value"), true),
+ "ExposureProgram" => array("SubIFD", "ExposureProgram", t("Exposure Program"), true),
+ "Flash" => array("SubIFD", "Flash", t("Flash"), true),
+ "FocalLength" => array("SubIFD", "FocalLength", t("Focal Length"), true),
+ "ISO" => array("SubIFD", "ISOSpeedRatings", t("ISO"), true),
+ "MeteringMode" => array("SubIFD", "MeteringMode", t("Metering Mode"), true),
+ "ShutterSpeed" => array("SubIFD", "ShutterSpeedValue", t("Shutter Speed"), true),
+ "DateTime" => array("SubIFD", "DateTimeOriginal", t("Date/Time"), true),
+ "Copyright" => array("IFD0", "Copyright", t("Copyright"), false),
+ "ImageType" => array("IFD0", "ImageType", t("Image Type"), false),
+ "Orientation" => array("IFD0", "Orientation", t("Orientation"), false),
+ "ResolutionUnit" => array("IFD0", "ResolutionUnit", t("Resolution Unit"), false),
+ "xResolution" => array("IFD0", "xResolution", t("X Resolution"), false),
+ "yResolution" => array("IFD0", "yResolution", t("Y Resolution"), false),
+ "Compression" => array("IFD1", "Compression", t("Compression"), false),
+ "BrightnessValue" => array("SubIFD", "BrightnessValue", t("Brightness Value"), false),
+ "Contrast" => array("SubIFD", "Contrast", t("Contrast"), false),
+ "ExposureMode" => array("SubIFD", "ExposureMode", t("Exposure Mode"), false),
+ "FlashEnergy" => array("SubIFD", "FlashEnergy", t("Flash Energy"), false),
+ "Saturation" => array("SubIFD", "Saturation", t("Saturation"), false),
+ "SceneType" => array("SubIFD", "SceneType", t("Scene Type"), false),
+ "Sharpness" => array("SubIFD", "Sharpness", t("Sharpness"), false),
+ "SubjectDistance" => array("SubIFD", "SubjectDistance", t("Subject Distance"), false),
+ "Caption" => array("IPTC", "Caption", t("Caption"), false),
+ "Keywords" => array("IPTC", "Keywords", t("Keywords"), false)
+ );
+ }
+ return self::$exif_keys;
+ }
+}
diff --git a/modules/exif/helpers/exif_event.php b/modules/exif/helpers/exif_event.php
new file mode 100644
index 00000000..665f8c10
--- /dev/null
+++ b/modules/exif/helpers/exif_event.php
@@ -0,0 +1,30 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class exif_event_Core {
+ static function item_created($item) {
+ exif::extract($item);
+ }
+
+ static function item_before_delete($item) {
+ ORM::factory("exif_data")
+ ->where("item_id", $item->id)
+ ->delete_all();
+ }
+}
diff --git a/modules/exif/helpers/exif_installer.php b/modules/exif/helpers/exif_installer.php
index 6ebe69af..a008581d 100644
--- a/modules/exif/helpers/exif_installer.php
+++ b/modules/exif/helpers/exif_installer.php
@@ -22,11 +22,23 @@ class exif_installer {
$version = module::get_version("exif");
if ($version == 0) {
+ $db = Database::instance();
+ $db->query("CREATE TABLE IF NOT EXISTS `exif_keys` (
+ `id` int(9) NOT NULL auto_increment,
+ `item_id` int(9) NOT NULL,
+ `name` varchar(64) NOT NULL,
+ `summary` boolean NOT NULL,
+ `value` varchar(255) NOT NULL,
+ PRIMARY KEY (`id`),
+ UNIQUE KEY(`item_id`, `summary`, `name`))
+ ENGINE=InnoDB DEFAULT CHARSET=utf8;");
module::set_version("exif", 1);
}
}
static function uninstall() {
+ $db = Database::instance();
+ $db->query("DROP TABLE IF EXISTS `exif_keys`;");
module::delete("exif");
}
}
diff --git a/modules/exif/models/exif_key.php b/modules/exif/models/exif_key.php
new file mode 100644
index 00000000..2cce1141
--- /dev/null
+++ b/modules/exif/models/exif_key.php
@@ -0,0 +1,21 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Exif_Key_Model extends ORM {
+}
diff --git a/modules/exif/tests/Exif_Test.php b/modules/exif/tests/Exif_Test.php
new file mode 100644
index 00000000..4ab1e21e
--- /dev/null
+++ b/modules/exif/tests/Exif_Test.php
@@ -0,0 +1,56 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class Exif_Test extends Unit_Test_Case {
+ public function exif_extract_test() {
+ $rand = rand();
+ $root = ORM::factory("item", 1);
+ $photo = photo::create($root, dirname(__FILE__) . "/data/image.jpg", "$rand.jpg", $rand, $rand);
+
+ $exif = exif::get($photo);
+ $expected = array(
+ array("caption" => "Camera Maker", "value" => "Pentax Corporation"),
+ array("caption" => "Camera Model", "value" => "PENTAX K10D"),
+ array("caption" => "Aperture", "value" => "f/2.8"),
+ array("caption" => "Color Space", "value" => "Uncalibrated"),
+ array("caption" => "Exposure Value", "value" => "4294.67 EV"),
+ array("caption" => "Exposure Program", "value" => "Program"),
+ array("caption" => "Flash", "value" => "No Flash"),
+ array("caption" => "Focal Length", "value" => "50 mm"),
+ array("caption" => "ISO", "value" => "6553700"),
+ array("caption" => "Metering Mode", "value" => "Multi-Segment"),
+ array("caption" => "Shutter Speed", "value" => "1/60 sec"),
+ array("caption" => "Date/Time", "value" => "2008:03:17 17:41:25")
+ );
+ $this->assert_equal($expected, $exif);
+
+ $exif = exif::get($photo, false);
+ $expected = array_merge($expected, array(
+ array("caption" => "Copyright", "value" => "(C) 2008 - T. Almdal"),
+ array("caption" => "Orientation", "value" => "1: Normal (0 deg)"),
+ array("caption" => "Resolution Unit", "value" => "Inch"),
+ array("caption" => "X Resolution", "value" => "240 dots per ResolutionUnit"),
+ array("caption" => "Y Resolution", "value" => "240 dots per ResolutionUnit"),
+ array("caption" => "Brightness Value", "value" => "0"),
+ array("caption" => "Scene Type", "value" => "0"),
+ array("caption" => "Subject Distance", "value" => "0"),
+ ));
+ $this->assert_equal($expected, $exif);
+ }
+} \ No newline at end of file
diff --git a/modules/exif/tests/data/image.jpg b/modules/exif/tests/data/image.jpg
new file mode 100644
index 00000000..e5ee1458
--- /dev/null
+++ b/modules/exif/tests/data/image.jpg
Binary files differ