summaryrefslogtreecommitdiff
path: root/modules/exif/helpers
diff options
context:
space:
mode:
authorTristan Zur <tzur@webserver.ccwn.org>2015-06-10 20:55:53 +0200
committerTristan Zur <tzur@webserver.ccwn.org>2015-06-10 20:55:53 +0200
commit406abd7c4df1ace2cd3e4e17159e8941a2e8c0c4 (patch)
treea324be16021f44f2fd6d55e609f47024e945b1db /modules/exif/helpers
Initial import
Diffstat (limited to 'modules/exif/helpers')
-rw-r--r--modules/exif/helpers/exif.php167
-rw-r--r--modules/exif/helpers/exif_event.php39
-rw-r--r--modules/exif/helpers/exif_installer.php45
-rw-r--r--modules/exif/helpers/exif_task.php88
-rw-r--r--modules/exif/helpers/exif_theme.php38
5 files changed, 377 insertions, 0 deletions
diff --git a/modules/exif/helpers/exif.php b/modules/exif/helpers/exif.php
new file mode 100644
index 0000000..b17f460
--- /dev/null
+++ b/modules/exif/helpers/exif.php
@@ -0,0 +1,167 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 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;
+
+ static function extract($item) {
+ $keys = array();
+ // Only try to extract EXIF from photos
+ if ($item->is_photo() && $item->mime_type == "image/jpeg") {
+ $data = array();
+ 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]])) {
+ $value = $exif_raw[$exifvar[0]][$exifvar[1]];
+ $value = encoding::convert_to_utf8($value);
+ $keys[$field] = Input::clean($value);
+
+ if ($field == "DateTime") {
+ $time = strtotime($value);
+ if ($time > 0) {
+ $item->captured = $time;
+ }
+ } else if ($field == "Caption" && !$item->description) {
+ $item->description = $value;
+ }
+ }
+ }
+ }
+
+ $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])) {
+ $value = implode(" ", $iptc[$iptc_key]);
+ $value = encoding::convert_to_utf8($value);
+ $keys[$keyword] = Input::clean($value);
+
+ if ($keyword == "Caption" && !$item->description) {
+ $item->description = $value;
+ }
+ }
+ }
+ }
+ }
+ $item->save();
+
+ $record = ORM::factory("exif_record")->where("item_id", "=", $item->id)->find();
+ if (!$record->loaded()) {
+ $record->item_id = $item->id;
+ }
+ $record->data = serialize($keys);
+ $record->key_count = count($keys);
+ $record->dirty = 0;
+ $record->save();
+ }
+
+ static function get($item) {
+ $exif = array();
+ $record = ORM::factory("exif_record")
+ ->where("item_id", "=", $item->id)
+ ->find();
+ if (!$record->loaded()) {
+ return array();
+ }
+
+ $definitions = self::_keys();
+ $keys = unserialize($record->data);
+ foreach ($keys as $key => $value) {
+ $exif[] = array("caption" => $definitions[$key][2], "value" => $value);
+ }
+
+ return $exif;
+ }
+
+ private static function _keys() {
+ if (!isset(self::$exif_keys)) {
+ self::$exif_keys = array(
+ "Make" => array("IFD0", "Make", t("Camera Maker"), ),
+ "Model" => array("IFD0", "Model", t("Camera Model"), ),
+ "Aperture" => array("SubIFD", "FNumber", t("Aperture"), ),
+ "ColorSpace" => array("SubIFD", "ColorSpace", t("Color Space"), ),
+ "ExposureBias" => array("SubIFD", "ExposureBiasValue", t("Exposure Value"), ),
+ "ExposureProgram" => array("SubIFD", "ExposureProgram", t("Exposure Program"), ),
+ "ExposureTime" => array("SubIFD", "ExposureTime", t("Exposure Time"), ),
+ "Flash" => array("SubIFD", "Flash", t("Flash"), ),
+ "FocalLength" => array("SubIFD", "FocalLength", t("Focal Length"), ),
+ "ISO" => array("SubIFD", "ISOSpeedRatings", t("ISO"), ),
+ "MeteringMode" => array("SubIFD", "MeteringMode", t("Metering Mode"), ),
+ "DateTime" => array("SubIFD", "DateTimeOriginal", t("Date/Time"), ),
+ "Copyright" => array("IFD0", "Copyright", t("Copyright"), ),
+ "ImageType" => array("IFD0", "ImageType", t("Image Type"), ),
+ "Orientation" => array("IFD0", "Orientation", t("Orientation"), ),
+ "ResolutionUnit" => array("IFD0", "ResolutionUnit", t("Resolution Unit"), ),
+ "xResolution" => array("IFD0", "xResolution", t("X Resolution"), ),
+ "yResolution" => array("IFD0", "yResolution", t("Y Resolution"), ),
+ "Compression" => array("IFD1", "Compression", t("Compression"), ),
+ "BrightnessValue" => array("SubIFD", "BrightnessValue", t("Brightness Value"), ),
+ "Contrast" => array("SubIFD", "Contrast", t("Contrast"), ),
+ "ExposureMode" => array("SubIFD", "ExposureMode", t("Exposure Mode"), ),
+ "FlashEnergy" => array("SubIFD", "FlashEnergy", t("Flash Energy"), ),
+ "Saturation" => array("SubIFD", "Saturation", t("Saturation"), ),
+ "SceneType" => array("SubIFD", "SceneType", t("Scene Type"), ),
+ "Sharpness" => array("SubIFD", "Sharpness", t("Sharpness"), ),
+ "SubjectDistance" => array("SubIFD", "SubjectDistance", t("Subject Distance"), ),
+ "Caption" => array("IPTC", "Caption", t("Caption"), ),
+ "Keywords" => array("IPTC", "Keywords", t("Keywords"), )
+ );
+ }
+ return self::$exif_keys;
+ }
+
+ static function stats() {
+ $missing_exif = db::build()
+ ->select("items.id")
+ ->from("items")
+ ->join("exif_records", "items.id", "exif_records.item_id", "left")
+ ->where("type", "=", "photo")
+ ->and_open()
+ ->where("exif_records.item_id", "IS", null)
+ ->or_where("exif_records.dirty", "=", 1)
+ ->close()
+ ->execute()
+ ->count();
+
+ $total_items = ORM::factory("item")->where("type", "=", "photo")->count_all();
+ if (!$total_items) {
+ return array(0, 0, 0);
+ }
+ return array($missing_exif, $total_items,
+ round(100 * (($total_items - $missing_exif) / $total_items)));
+ }
+
+ static function check_index() {
+ list ($remaining) = exif::stats();
+ if ($remaining) {
+ site_status::warning(
+ t('Your Exif index needs to be updated. <a href="%url" class="g-dialog-link">Fix this now</a>',
+ array("url" => html::mark_clean(url::site("admin/maintenance/start/exif_task::update_index?csrf=__CSRF__")))),
+ "exif_index_out_of_date");
+ }
+ }
+}
diff --git a/modules/exif/helpers/exif_event.php b/modules/exif/helpers/exif_event.php
new file mode 100644
index 0000000..cd5068f
--- /dev/null
+++ b/modules/exif/helpers/exif_event.php
@@ -0,0 +1,39 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 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) {
+ if (!$item->is_album()) {
+ exif::extract($item);
+ }
+ }
+
+ static function item_updated_data_file($item) {
+ if (!$item->is_album()) {
+ exif::extract($item);
+ }
+ }
+
+ static function item_deleted($item) {
+ db::build()
+ ->delete("exif_records")
+ ->where("item_id", "=", $item->id)
+ ->execute();
+ }
+}
diff --git a/modules/exif/helpers/exif_installer.php b/modules/exif/helpers/exif_installer.php
new file mode 100644
index 0000000..75d0f83
--- /dev/null
+++ b/modules/exif/helpers/exif_installer.php
@@ -0,0 +1,45 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 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_installer {
+ static function install() {
+ $db = Database::instance();
+ $db->query("CREATE TABLE IF NOT EXISTS {exif_records} (
+ `id` int(9) NOT NULL auto_increment,
+ `item_id` INTEGER(9) NOT NULL,
+ `key_count` INTEGER(9) default 0,
+ `data` TEXT,
+ `dirty` BOOLEAN default 1,
+ PRIMARY KEY (`id`),
+ KEY(`item_id`))
+ DEFAULT CHARSET=utf8;");
+ }
+
+ static function activate() {
+ exif::check_index();
+ }
+
+ static function deactivate() {
+ site_status::clear("exif_index_out_of_date");
+ }
+
+ static function uninstall() {
+ Database::instance()->query("DROP TABLE IF EXISTS {exif_records};");
+ }
+}
diff --git a/modules/exif/helpers/exif_task.php b/modules/exif/helpers/exif_task.php
new file mode 100644
index 0000000..f8a108a
--- /dev/null
+++ b/modules/exif/helpers/exif_task.php
@@ -0,0 +1,88 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 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_task_Core {
+ static function available_tasks() {
+ // Delete extra exif_records
+ db::build()
+ ->delete("exif_records")
+ ->where("item_id", "NOT IN",
+ db::build()->select("id")->from("items")->where("type", "=", "photo"))
+ ->execute();
+
+ list ($remaining, $total, $percent) = exif::stats();
+ return array(Task_Definition::factory()
+ ->callback("exif_task::update_index")
+ ->name(t("Extract Exif data"))
+ ->description($remaining
+ ? t2("1 photo needs to be scanned",
+ "%count (%percent%) of your photos need to be scanned",
+ $remaining, array("percent" => (100 - $percent)))
+ : t("Exif data is up-to-date"))
+ ->severity($remaining ? log::WARNING : log::SUCCESS));
+ }
+
+ static function update_index($task) {
+ try {
+ $completed = $task->get("completed", 0);
+
+ $start = microtime(true);
+ foreach (ORM::factory("item")
+ ->join("exif_records", "items.id", "exif_records.item_id", "left")
+ ->where("type", "=", "photo")
+ ->and_open()
+ ->where("exif_records.item_id", "IS", null)
+ ->or_where("exif_records.dirty", "=", 1)
+ ->close()
+ ->find_all(100) as $item) {
+ // The query above can take a long time, so start the timer after its done
+ // to give ourselves a little time to actually process rows.
+ if (!isset($start)) {
+ $start = microtime(true);
+ }
+
+ exif::extract($item);
+ $completed++;
+
+ if (microtime(true) - $start > .75) {
+ break;
+ }
+ }
+
+ list ($remaining, $total, $percent) = exif::stats();
+ $task->set("completed", $completed);
+ if ($remaining == 0 || !($remaining + $completed)) {
+ $task->done = true;
+ $task->state = "success";
+ site_status::clear("exif_index_out_of_date");
+ $task->percent_complete = 100;
+ } else {
+ $task->percent_complete = round(100 * $completed / ($remaining + $completed));
+ }
+ $task->status = t2("one record updated, index is %percent% up-to-date",
+ "%count records updated, index is %percent% up-to-date",
+ $completed, array("percent" => $percent));
+ } catch (Exception $e) {
+ $task->done = true;
+ $task->state = "error";
+ $task->status = $e->getMessage();
+ $task->log((string)$e);
+ }
+ }
+}
diff --git a/modules/exif/helpers/exif_theme.php b/modules/exif/helpers/exif_theme.php
new file mode 100644
index 0000000..df7c6f4
--- /dev/null
+++ b/modules/exif/helpers/exif_theme.php
@@ -0,0 +1,38 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2013 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_theme_Core {
+ static function sidebar_bottom($theme) {
+ $item = $theme->item();
+ if ($item && $item->is_photo()) {
+ $record = db::build()
+ ->select("key_count")
+ ->from("exif_records")
+ ->where("item_id", "=", $item->id)
+ ->execute()
+ ->current();
+ if ($record && $record->key_count) {
+ $view = new View("exif_sidebar.html");
+ $view->item = $item;
+ return $view;
+ }
+ }
+ return null;
+ }
+}