PHP Classes

How to Implement a PHP JPEG Exif Data Editor to Read and Write JPEG Image Captions in Multiple Languages - PHP JPEG metadata package blog

Recommend this page to a friend!
  All package blogs All package blogs   PHP JPEG metadata PHP JPEG metadata   Blog PHP JPEG metadata package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement a PH...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Updated on: 2022-08-22

Posted on: 2022-08-22

Package: PHP JPEG metadata

If you are doing development work for clients whose native language is not English, you probably have been challenged to write IPTC-IM (International Press Telecommunications Council Image Metadata) that you can embed in JPEG image files with information like headlines, captions, keywords, etc.

You can embed that metadata information in JPEG image files in different languages. The challenge worsens if you want to offer the same images to different clients speaking or publishing in other languages.

Read this article to learn how to read and write IPTC-IM metadata in JPEG image files using PHP.




Loaded Article

In this article you will learn:

What is Exif Data on a Photo that You Can Access Using PHP that Supports Text in Muliple Languages

How to Implement a PHP JPEG Exif Data Automatic Translator for JPEG Image Captions

How to Download or Install the PHP JPEG Metadata Package Using PHP Composer Tool


What is Exif Data on a Photo that You Can Access Using PHP that Supports Text in Muliple Languages

Wouldn't it be great if you could associate IPTC-IM in different languages with a single image?

Although the XMP (eXtensible Metadata Platform) standard image metadata, also known as ISO 16684-1, supports multi-lingual metadata (at least for some fields), at the time I am writing this article, none of the commonly used software packages, like Photoshop and Photo Mechanic, support multi-lingual IPTC-IM.

Unless your customer uses proprietary data that can read and interpret multi-lingual IPTC-IM, you are bound to manage multiple copies of the same image, only differentiation by the language you use to write IPTC-IM data.

But a more challenging, or should I say, the time-consuming challenge, is to write IPTC-IM in multiple languages and ensure their consistency, not only when you are not native in each language.

At least here on this front, technology can offer a sound solution. It is called AI-based machine translation.

Many companies today provide high-quality translation engines that you can access through simple APIs (application programming interfaces). Most even offer free access if you are not a heavy user, meaning they often translate less than 500,000 characters per month.

How to Implement a PHP JPEG Exif Data Automatic Translator for JPEG Image Captions

Today's Open-Source environment offers multiple tools, libraries, and classes to read, translate, and write IPTC-IM automatically.

In this post, I show how you can have your IPTC-IM translated on the fly with only a handful of PHP program lines using existing libraries.

Accessing the Required Libraries

Although multiple freely available libraries exist on the Web (specifically on github.com), I chose to use DeepL's translation API simply because it offers the best translations I have seen.

In nearly all the cases, the translations suggested by DeepL were correct out of the box. The only time I had an issue was when DeepL translated "Cross Country" in the context of cycling into "Langlauf," which means "cross-country skiing."

For the second library, I am biased, as I am using the Metadata class library that I have developed myself in the context of the HOLIDAY photo database project. You can find this class on PHPClasses.org.

You can easily install both libraries using the PHP composer tool with the following commands you can enter in the command line shell console. Alternatively, you may download them from github.com.

composer require deeplcom/deepl-php
composer require diderich/metadata

Making the Libraries Available

Next, I make the libraries available to my IPTC-IM translation program using the recommended autoloading mechanism (using the PSR-4 standard).

require_once("vendor/autoload.php");

use \Holiday\Metadata;
use \DeepL\Translator;

Configuring the Data

To use the translation API from DeepL (or any other provider, like Google or IBM), you need to get an access key ($deep_key).

The process is simple but often requires a credit card to ensure that you do not create multiple accounts to circumvent free usage limitations.

Next, I create an array ($field_ary) to store all the IPTC-IM fields I want to have automatically translated.

For the sake of parameter checking (and the fact that DeepL distinguished between British English and American English), I create an array ($lang_ary) of supported languages.

$deepl_key = ‘xxxxxxxx-xxxx-xxxx-xxxxxxxxxx’;
$fields_ary = [Metadata::CAPTION => 'CAPTION', Metadata::HEADLINE => 'HEADLINE' , Metadata::EVENT => 'EVENT', 
    Metadata::LOCATION => 'LOCATION', Metadata::INSTRUCTIONS => 'INSTRUCTIONS', 
    Metadata::USAGE_TERMS => 'USAGE TERMS', Metadata::KEYWORDS =>  KEYWORDS',      Metadata::SCENES => 'SCENES', 
    Metadata::GENRE => 'GENRE', Metadata::CITY => 'CITY', Metadata::COUNTRY => 'COUNTRY'];
$lang_ary = [‘en’ => ‘en-US’, ‘de’ => ‘de’, ‘fr’ => ‘fr’];

Getting the Command Line Arguments

The next step is quite boring. It reads the arguments from the command line and checks their validity.
if($argv !== 3 && $argc !== 4) die("$argv[0] src_lang dst_lang src_file [dst_file]\n");
$src_lang = argv[1]; $dst_lan = $argv[2];
$src_file = $argv[3]; $dst_file = $argv[4];
if(!isset($lang_ary[$src_lang])) die("Error: Invalid source language specified '$src_lang'\n");
if(!isset($lang_ary[$dst_lang])) die("Error: Invalid target language specified '$dst_lang'\n");
if(!file_exists($src_file)) die("Error: Image file not found '$src_file'\n");

Initializing the Libraries Used

At the translation process’s core are Metadata (which handles reading and writing IPTC-IM from JPEG images) and Translator (which provides access to DeepL’s machine learning-based translation algorithm).
$metadata = new Metadata();
$translator = new Translator($deepl_key);

Reading the IPTC-IM

I start by reading the original image containing the IPTC metadata to be translated and written in English. One call to the Metadata::read() function does the job. The function raises an exception if the file cannot be found or an error occurs while reading the image and associated metadata.
$metadata->read($src_file);

Translating the IPTC-IM

Next, I iterate through all the fields I want to translate and translate their content using the DeepL API. Alternatively, you could use Google Translate or IBM Watson translate APIs.

While translating fields, I distinguish between those fields that contain a single text string and those, like keywords, that contain an array of a text string. For reference, I display the translated results on the screen.

foreach($fields_ary as $field_id => $field_name) {
    if($metadata->isset($field_id)) {
        $src_data = $metadata->get($field_id);
        if(is_array($src_data)) {
            $tr_data = array();
            foreach($src_data as $key => $src_text) {
                $tr_data[$key] = $translator->translateText($src_text, $src_lang, $lang_ary[$dst_lang])->text;
            }
            echo "$field_name\n\t".implode(', ', $src_data)."\n\t".implode(', ', $tr_data)."\n";
        }
        else {
            $tr_data = $translator->translateText($src_data, $src_lang, $lang_ary[$dst_lang])->text;
            echo "$field_name\n\t$src_data\n\t$tr_data\n";
        }
        $metadata->set($field_id, empty($tr_data) ? false : $tr_data);
    }
}

Writing the IPTC-IM back

Finally, with one line of code, I create a new file containing the translated IPTC-IM. The metadata is written in both the IPTC/APP13 and the XMP/APP1 segments of the image.
$metadata->write($dst_file);

That's it. Now you have a second file containing the translated IPTC-IM. Translating IPTC-IM has never been easier. If needed, you may edit the translated IPTC-IM in your favorite application.

Example

The example below shows all translated fields, as written back to the translated file example.de.jpg.

$ ./trcaption.php en de example.jpg example.de.jpg

CAPTION
    Silver medalists Timea Bacsinszky (left) and Martina Hingis (right) of Switzerland pose on the podium during the ceremony of the women's doubles gold medal match of the tennis tournament on day ten during the 2016 Rio Olympic Games in Rio de Janeiro, Brazil on August 14, 2016
    Die Silbermedaillengewinnerinnen Timea Bacsinszky (links) und Martina Hingis (rechts) aus der Schweiz posieren auf dem Podium während der Zeremonie des Goldmedaillenspiels im Damendoppel des Tennisturniers an Tag zehn der Olympischen Spiele 2016 in Rio de Janeiro, Brasilien, am 14. August 2016

HEADLINE
    Women's Double Tennis Gold Medal Match  During the 2016 Rio Olympic Games
    Tennis-Goldmedaillenmatch im Damendoppel bei den Olympischen Spielen 2016 in Rio

EVENT
    2016 Rio Olympic Games - Tennis
    Olympische Spiele 2016 in Rio - Tennis

INSTRUCTIONS
    * EDITORIAL USE ONLY *
    * NUR FÜR REDAKTIONELLE ZWECKE *

KEYWORDS
    Olympic Games, Tennis, Finals, Women, Sui, Silve Medal, Podium
    Olympische Spiele, Tennis, Endrunde, Frauen, Sui, Silvestermedaille, Podium

SCENES
    Jubilation
    Jubel

CITY
    Rio de Janeiro
    Rio de Janeiro

COUNTRY
    Brazil
    Brasilien

How to Download or Install the PHP JPEG Metadata Package Using PHP Composer Tool

The PHP JPEG Metadata can be downloaded from download page or be installed using the PHP Composer tool following instructions in the Composer install instructions page.



You need to be a registered user or login to post a comment

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

No comments were submitted yet.




  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   PHP JPEG metadata PHP JPEG metadata   Blog PHP JPEG metadata package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement a PH...  
For more information send a message to info at phpclasses dot org.