1
0

Add IPTC module

Taken from http://gallery.menalto.com/node/95875 (file http://gallery.menalto.com/files/iptc_0.zip)
Author: gcog (I just imported it into Git unchanged)
This commit is contained in:
Thomas Bleher 2010-08-30 03:35:18 +08:00 committed by Bharat Mediratta
parent 8cf229ee72
commit d01599c1c1
12 changed files with 668 additions and 0 deletions

View File

@ -0,0 +1,84 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2010 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 Admin_Iptc_Controller extends Admin_Controller {
public function index() {
// Generate a new admin page.
$view = new Admin_View("admin.html");
$view->content = new View("admin_iptc.html");
$view->content->iptc_form = $this->_get_admin_form();
print $view;
}
public function saveprefs() {
// Save user preferences to the database.
// Prevent Cross Site Request Forgery
access::verify_csrf();
// Make sure the user filled out the form properly.
$form = $this->_get_admin_form();
if ($form->validate()) {
Kohana_Log::add("error",print_r($form,1));
// Save settings to Gallery's database.
foreach (iptc::keys() as $keyword => $iptcvar) {
$checkbox = false;
for ($i = 0; $i < count($form->Global->$keyword); $i++) {
if ($form->Global->$keyword->value[$i] == $keyword) {
$checkbox = true;
}
}
module::set_var("iptc", "show_".$keyword, $checkbox);
}
// Display a success message and redirect back to the TagsMap admin page.
message::success(t("Your settings have been saved."));
url::redirect("admin/iptc");
}
// Else show the page with errors
$view = new Admin_View("admin.html");
$view->content = new View("admin_iptc.html");
$view->content->iptc_form = $form;
print $view;
}
private function _get_admin_form() {
// Make a new Form.
$form = new Forge("admin/iptc/saveprefs", "", "post", array("id" => "g-iptc-adminForm"));
// Create group for display settings
$iptc_display_group = $form->group("Global")
->label(t("Display Settings"));
$show = t("Show");
foreach (iptc::keys() as $keyword => $iptcvar) {
unset($checkbox);
$checkbox[$keyword] = array($show." \"".$iptcvar[1]."\" ?", module::get_var("iptc", "show_".$keyword));
$iptc_display_group->checklist($keyword)
->options($checkbox);
}
// Add a save button to the form.
$form->submit("SaveSettings")->value(t("Save"));
// Return the newly generated form.
return $form;
}
}

View File

@ -0,0 +1,207 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2009 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.
*/
require_once(MODPATH . "iptc/lib/functions.php");
/**
* This is the API for handling iptc data.
*/
class iptc_Core {
protected static $iptc_keys;
static function extract($item) {
$keys = array();
// Only try to extract IPTC from photos
if ($item->is_photo() && $item->mime_type == "image/jpeg") {
$info = getJpegHeader($item->file_path());
if ($info !== FALSE) {
$iptcBlock = getIptcBlock($info);
if ($iptcBlock !== FALSE) {
$iptc = iptcparse($iptcBlock);
} else {
$iptc = array();
}
$xmp = getXmpDom($info);
foreach (self::keys() as $keyword => $iptcvar) {
$iptc_key = $iptcvar[0];
$xpath = $iptcvar[2];
$value = null;
if ($xpath != null) {
$value = getXmpValue($xmp, $xpath);
}
if ($value == null) {
if (!empty($iptc[$iptc_key])) {
$value = implode(";", $iptc[$iptc_key]);
if (function_exists("mb_detect_encoding") && mb_detect_encoding($value) != "UTF-8") {
$value = utf8_encode($value);
}
}
}
if ($value != null) {
$keys[$keyword] = Input::clean($value);
}
}
}
}
$record = ORM::factory("iptc_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) {
$iptc = array();
$record = ORM::factory("iptc_record")
->where("item_id", "=", $item->id)
->find();
if (!$record->loaded()) {
return array();
}
$definitions = self::keys();
$keys = unserialize($record->data);
foreach ($keys as $key => $value) {
if (module::get_var("iptc", "show_".$key) == 1)
$iptc[] = array("caption" => $definitions[$key][1], "value" => $value);
}
return $iptc;
}
public static function keys() {
if (!isset(self::$iptc_keys)) {
self::$iptc_keys = array(
"ObjectName" => array("2#005",
t("IPTC Object Name"),
"/x:xmpmeta/rdf:RDF/rdf:Description/dc:title/rdf:Alt/rdf:li" ),
"EditStatus" => array("2#007",
t("IPTC Edit Status"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@mediapro:Status" ),
"Category" => array("2#015",
t("IPTC Category"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:Category" ),
"SupplementalCategories" => array("2#020",
t("IPTC Categories"),
"/x:xmpmeta/rdf:RDF/rdf:Description/photoshop:SupplementalCategories/rdf:Bag/rdf:li" ),
"FixtureIdentifier" => array("2#022",
t("IPTC Identifier"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@mediapro:Event" ),
"Keywords" => array("2#025",
t("IPTC Keywords"),
"/x:xmpmeta/rdf:RDF/rdf:Description/dc:subject/rdf:Bag/rdf:li" ),
"LocationCode" => array("2#026",
t("IPTC Location Code"),
null ),
"SpecialInstructions" => array("2#040",
t("IPTC Instructions"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:Instructions" ),
"DateCreated" => array("2#055",
t("IPTC Created Date"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:DateCreated" ),
"ByLine" => array("2#080",
t("IPTC Author"),
"/x:xmpmeta/rdf:RDF/rdf:Description/dc:creator/rdf:Seq/rdf:li" ),
"ByLineTitle" => array("2#085",
t("IPTC Author Title"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:AuthorsPosition" ),
"City" => array("2#090",
t("IPTC City"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:City" ),
"SubLocation" => array("2#092",
t("IPTC SubLocation"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@Iptc4xmpCore:Location | /x:xmpmeta/rdf:RDF/rdf:Description/@mediapro:Location" ),
"ProvinceState" => array("2#095",
t("IPTC Province State"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:State" ),
"CountryCode" => array("2#100",
t("IPTC Country Code"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@Iptc4xmpCore:CountryCode" ),
"CountryName" => array("2#101",
t("IPTC Country Name"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:Country" ),
"Transmission" => array("2#103",
t("IPTC Transmission,"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:TransmissionReference" ),
"HeadLine" => array("2#105",
t("IPTC HeadLine"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:Headline" ),
"Credit" => array("2#110",
t("IPTC Credit"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:Credit | /x:xmpmeta/rdf:RDF/rdf:Description/photoshop:Credit" ),
"Source" => array("2#115",
t("IPTC Source"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:Source" ),
"Copyright" => array("2#116",
t("IPTC Copyright"),
"/x:xmpmeta/rdf:RDF/rdf:Description/dc:rights/rdf:Alt/rdf:li" ),
"Contacts" => array("2#118",
t("IPTC Contacts"),
"/x:xmpmeta/rdf:RDF/rdf:Description/mediapro:People/rdf:Bag/rdf:li" ),
"Caption" => array("2#120",
t("IPTC Caption"),
"/x:xmpmeta/rdf:RDF/rdf:Description/dc:description/rdf:Alt/rdf:li" ),
"Redactor" => array("2#122",
t("IPTC Redactor"),
"/x:xmpmeta/rdf:RDF/rdf:Description/@photoshop:CaptionWriter" )
);
}
return self::$iptc_keys;
}
static function stats() {
$missing_iptc = db::build()
->select("items.id")
->from("items")
->join("iptc_records", "items.id", "iptc_records.item_id", "left")
->where("type", "=", "photo")
->and_open()
->where("iptc_records.item_id", "IS", null)
->or_where("iptc_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_iptc, $total_items,
round(100 * (($total_items - $missing_iptc) / $total_items)));
}
static function check_index() {
list ($remaining) = iptc::stats();
if ($remaining) {
site_status::warning(
t('Your Iptc 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/iptc_task::update_index?csrf=__CSRF__")))),
"iptc_index_out_of_date");
}
}
}

View File

@ -0,0 +1,43 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2009 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 iptc_block_Core {
static function get_site_list() {
return array("iptc" => t("IPTC info"));
}
static function get($block_id, $theme) {
$block = "";
switch ($block_id) {
case "iptc":
if ($theme->item()) {
$details = iptc::get($theme->item());
if (count($details) > 0) {
$block = new Block();
$block->css_id = "g-metadata";
$block->title = t("IPTC info");
$block->content = new View("iptc_block.html");
$block->content->details = $details;
}
}
break;
}
return $block;
}
}

View File

@ -0,0 +1,42 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2009 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 iptc_event_Core {
static function item_created($item) {
if ($item->is_photo()) {
iptc::extract($item);
}
}
static function item_deleted($item) {
db::build()
->delete("iptc_records")
->where("item_id", "=", $item->id)
->execute();
}
static function admin_menu($menu, $theme) {
// Add a link to the admin page to the Settings menu.
$menu->get("settings_menu")
->append(Menu::factory("link")
->id("iptc")
->label(t("IPTC Settings"))
->url(url::site("admin/iptc")));
}
}

View File

@ -0,0 +1,46 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2009 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 iptc_installer {
static function install() {
$db = Database::instance();
$db->query("CREATE TABLE IF NOT EXISTS {iptc_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;");
module::set_version("iptc", 1);
}
static function activate() {
iptc::check_index();
}
static function deactivate() {
site_status::clear("iptc_index_out_of_date");
}
static function uninstall() {
Database::instance()->query("DROP TABLE IF EXISTS {iptc_records};");
}
}

View File

@ -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-2009 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 iptc_task_Core {
static function available_tasks() {
// Delete extra iptc_records
db::build()
->delete("iptc_records")
->where("item_id", "NOT IN",
db::build()->select("id")->from("items")->where("type", "=", "photo"))
->execute();
list ($remaining, $total, $percent) = iptc::stats();
return array(Task_Definition::factory()
->callback("iptc_task::update_index")
->name(t("Extract Iptc 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("IPTC 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("iptc_records", "items.id", "iptc_records.item_id", "left")
->where("type", "=", "photo")
->and_open()
->where("iptc_records.item_id", "IS", null)
->or_where("iptc_records.dirty", "=", 1)
->close()
->find_all() 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);
}
iptc::extract($item);
$completed++;
if (microtime(true) - $start > 1.5) {
break;
}
}
list ($remaining, $total, $percent) = iptc::stats();
$task->set("completed", $completed);
if ($remaining == 0 || !($remaining + $completed)) {
$task->done = true;
$task->state = "success";
site_status::clear("iptc_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);
}
}
}

View File

@ -0,0 +1,97 @@
<?php
function getJpegHeader($filename)
{
$file = @fopen($filename, 'rb');
if (!$file) {
return FALSE;
}
$startOfImage = fread($file, 2);
if ($startOfImage != "\xFF\xD8") {
fclose($file);
return FALSE;
}
$result = FALSE;
do {
$startOfSegment = fread($file, 1);
if ($startOfSegment != "\xFF") {
fclose($file);
return $result;
}
$typeOfSegment = ord(fread($file, 1));
if ($typeOfSegment === FALSE || $typeOfSegment == 0xDA || $typeOfSegment == 0xD9) { // Start of image or End of image
fclose($file);
return $result;
}
if ($typeOfSegment < 0xD0 || $typeOfSegment > 0xD7) {
$size = fread($file, 2);
if ($size === FALSE) {
fclose($file);
return $result;
}
$sizeOfSegment = unpack("nV", $size);
$data = fread($file, $sizeOfSegment['V']-2);
if ($data === FALSE) {
fclose($file);
return $result;
}
if ($result === FALSE)
unset($result);
$result[] = array("type" => $typeOfSegment, "data" => $data); // Multiple segments can have the same type like Exif and XMP
}
} while (!feof($file));
fclose($file);
return $result;
}
function getIptcBlock($jpegHeader)
{
for ($i = 0; $i < count($jpegHeader); $i++) {
if ($jpegHeader[$i]['type'] == 0xED) {
if (strncmp($jpegHeader[$i]['data'], "Photoshop 3.0\x00", 14) == 0) {
return $jpegHeader[$i]['data'];
}
}
}
return FALSE;
}
function getXmpDom($jpegHeader)
{
for ($i = 0; $i < count($jpegHeader); $i++) {
if ($jpegHeader[$i]['type'] == 0xE1) {
if (strncmp($jpegHeader[$i]['data'], "http://ns.adobe.com/xap/1.0/\x00", 29) == 0) {
$xmlstr = substr($jpegHeader[$i]['data'], 29);
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
return $doc;
}
}
}
return FALSE;
}
function getXmpValue($dom, $xpathQuery)
{
if ($dom === FALSE)
return null;
$xpath = new DOMXPath($dom);
$xpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
$xpath->registerNamespace('photoshop', "http://ns.adobe.com/photoshop/1.0/");
$xpath->registerNamespace('Iptc4xmpCore', "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/");
$xpath->registerNamespace('dc', "http://purl.org/dc/elements/1.1/");
$xpath->registerNamespace('mediapro', "http://ns.iview-multimedia.com/mediapro/1.0/");
$nodeList = $xpath->query($xpathQuery);
$result = "";
foreach ($nodeList as $node) {
if (!empty($result))
$result .= ';';
$result .= $node->nodeValue;
}
return $result;
}

View File

@ -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-2009 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 Iptc_Key_Model extends ORM {
}

View File

@ -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-2009 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 Iptc_Record_Model extends ORM {
}

3
modules/iptc/module.info Normal file
View File

@ -0,0 +1,3 @@
name = "Iptc Data"
description = "Extract Iptc data and display it on photo pages."
version = 2

View File

@ -0,0 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<div id="g-iptc-admin">
<h2> <?= t("IPTC Settings") ?> </h2>
<br />
<?= $iptc_form ?>
<br />
</div>

View File

@ -0,0 +1,9 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<ul class="g-metadata">
<? for ($i = 0; $i < count($details); $i++): ?>
<li>
<strong class="caption"><?= $details[$i]["caption"] ?></strong>
<?= html::clean($details[$i]["value"]) ?>
</li>
<? endfor ?>
</ul>