1. Home
  2. Blog
  3. Blog Post
Santanu
Santanu

General

PHP iptcembed Gallery3 to Flickr

some small scripting with PHP

Lego
Lego

A journey of sharing photo only got me into having a website of my own.

When digital camera came out with decent mega pixel was an early adapter of the same. But soon realized sharing Pictures on email was not an option and hosted pictures on Tripod. Oh! they used to give 10MB of free hosting :) now Flicker gives one 1TB free. So long story small after the managing own gallyery app writen initally with csv and php , later mysql and php and lastly Gallery for almost 10 years. found gallery development is kind of not that active it used to be. So thought of moving into Flickr which was big no in-between as they had restriction on free accounts. But now those restriction gone managing

7000+ picture and importing them with proper details remained a BIG challenage. At last came up with iptcembed where all the details where written into JPEG file itself andimported those image as set to Flickr.And all your details, tag, captions are set properly.

Here is how I did it

  1. Copy Gallery’s items table to temp table so we do not mess with live data

  2. Add a new column to the temp table

ALTER TABLE Yours_G3_items-new ADD album_name TEXT NOT NULL

  1. Find out the parent album id of each items and add that data in the new column.

UPDATE Yours_G3_items-new,Yours_G3_items SET Yours_G3_items-new.album_name = Yours_G3_items.title WHERE Yours_G3_items-new.parent_id = Yours_G3_items.id

  1. Export the new table with ”|” as separator and "" as enclosed.

Now the PHP script

IPTC Embedded with help from PHP
<?php
// File to be opened
$file = "/tmp/csv.txt";
$gallery_album_base = "...../gallery3/var/albums";
$new_gallery_album_base = "<new location>/albums";
$handle = fopen($file, 'r') or die("Fail to open input file \n\r");
$ln = 0;
// Some Functions Start
function csv_explode($delim = '|', $str, $enclose = '"', $preserve = false)
{
$resArr = array();
$n = 0;
$expEncArr = explode($enclose, $str);
foreach ($expEncArr as $EncItem) {
if ($n++ % 2) {
array_push($resArr, array_pop($resArr) . ($preserve ? $enclose : '') . $EncItem . ($preserve ? $enclose : ''));
} else {
$expDelArr = explode($delim, $EncItem);
array_push($resArr, array_pop($resArr) . array_shift($expDelArr));
$resArr = array_merge($resArr, $expDelArr);
}
}
return $resArr;
}
// iptc_make_tag() function by Thies C. Arntzen
function iptc_make_tag($rec, $data, $value)
{
$length = strlen($value);
$retval = chr(0x1C) . chr($rec) . chr($data);
if ($length < 0x8000) {
$retval .= chr($length >> 8) . chr($length & 0xFF);
} else {
$retval .= chr(0x80) .
chr(0x04) .
chr(($length >> 24) & 0xFF) .
chr(($length >> 16) & 0xFF) .
chr(($length >> 8) & 0xFF) .
chr($length & 0xFF);
}
return $retval . $value;
}
// Some Functions End
while ($line = fgets($handle)) {
$line = str_ireplace("\x0D", "", $line);
$line = urldecode($line);
++$ln;
print("just line : $ln \n");
if ($line === FALSE) {
print("FALSE\n");
} else {
$variables = csv_explode("|", $line);
// print_r ($variables);
// 26(album or photo) 25(title), 13(image_path), 4(details),33(album_name/tag)
if ($variables[26] == "photo") {
// Path new and old path
$old_path = $gallery_album_base . '/' . $variables[13];
$image_name_new = $new_gallery_album_base . '/' . $variables[13];
$path_parts = pathinfo($image_name_new);
if (!file_exists($path_parts['dirname'])) {
print("making directory {$path_parts['dirname']} \n");
mkdir($path_parts['dirname'], 0777, true);
}
// Set the IPTC tags
$iptc = array(
'2#120' => $variables[4],
'2#105' => $variables[25],
'2#005' => $variables[25],
'2#015' => $variables[33],
'2#116' => 'Copyright, www santm com'
);
// Convert the IPTC tags into binary code
$data = '';
foreach ($iptc as $tag => $string) {
$tag = substr($tag, 2);
$data .= iptc_make_tag(2, $tag, $string);
}
// Embed the IPTC data
$content = iptcembed($data, $old_path);
// Write the new image data out to the file.
$fp = fopen($image_name_new, "wb");
fwrite($fp, $content);
fclose($fp);
print("working on $image_name_new \n");
// $size = getimagesize($image_name_new, $info);
// if (isset($info['APP13'])) {
// $iptc = iptcparse($info['APP13']);
// var_dump($iptc);
// }
} // end of if photo loop
} // end of else loop
} // end of while loop
// Close the File.
fclose($handle);
?>

Wish there was tools avilable on the web where I could import the image from my old hosting company to Flickr directly instead of uploading them via their PC up-loader.

More here on major phpfunction

Read Next

26 Apr   2014

Driving Himachal

Goog Maps of the trip