[wp-trac] [WordPress Trac] #55535: Pre-populate Image Alt Text field with IPTC Photo Metadata Standard Alt Text

WordPress Trac noreply at wordpress.org
Mon Dec 19 10:33:56 UTC 2022


#55535: Pre-populate Image Alt Text field with IPTC Photo Metadata Standard Alt
Text
-------------------------+----------------------------
 Reporter:  eatingrules  |       Owner:  joedolson
     Type:  enhancement  |      Status:  accepted
 Priority:  normal       |   Milestone:  6.2
Component:  Media        |     Version:
 Severity:  minor        |  Resolution:
 Keywords:  needs-patch  |     Focuses:  accessibility
-------------------------+----------------------------

Comment (by brendanquinn):

 Hi @joedolson and all, if I can explain a bit of the detail behind IPTC
 Photo Metadata (more details in the spec:
 https://www.iptc.org/std/photometadata/specification/IPTC-
 PhotoMetadata-2022.1.html#history):

 The "original" IPTC metadata is now referred to as IPTC/IIM. It is a
 binary format encoded in the Photoshop Image Resource Block (APP13 in
 JPEG). It was implemented by Photoshop in the 1990s. This is what is
 supported by PHP's native `iptcparse` function.

 In the 2000s, IPTC and Adobe moved to the XMP format, which is stored in a
 different image header using the APP1 block marker (which can also used to
 store Exif tags). Unfortunately, PHP's `iptcparse` has still not been
 updated. It's understandable, because the parsing is more complicated -
 it's an XML-encoded set of RDF triples.

 I'm not a PHP expert either, but here's some code to extract the XMP
 packet from an image and retrieve the Alt Text field (created with a bit
 of help from ChatGPT!)

 {{{#!php
 <?php

 // Read the contents of the JPEG file into a string
 $jpeg_contents = file_get_contents("image.jpg");

 // Find the start and end positions of the XMP metadata
 $xmp_start = strpos($jpeg_contents, "<x:xmpmeta");
 $xmp_end = strpos($jpeg_contents, "</x:xmpmeta>");

 // Extract the XMP metadata from the JPEG contents
 $xmp_data = substr($jpeg_contents, $xmp_start, $xmp_end - $xmp_start +
 12);

 // Parse the XMP metadata using DOMDocument
 $doc = new DOMDocument();
 $doc->loadXML($xmp_data);

 // Get the "Iptc4xmpCore:AltTextAccessibility" element
 $element =
 $doc->getElementsByTagName("Iptc4xmpCore:AltTextAccessibility")->item(0);

 // Extract the value of the element
 $alt_text = $element->nodeValue;
 }}}


 I hope that helps move towards a solution. I guess the best path forward
 would be to change the core `iptcparse` to support XMP metadata. But in
 the meantime this wouldn't be a bad step forward.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/55535#comment:20>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list