Goal
With the new compiler, there is now the possibility to create personalized tags.
The integration in SPIP of the jpg EXIF metadata could be done with this. The data stored in the jpg file can be the time the photo has been taken, a copyright or GPS data for example.
All the code presented in this article has to be copied at the end of your file mes_fonctions.php3.
PHP provides functions to access these data. We will then start to write a little wrapper to access them on SPIP documents:
The first function tests if a file can contains EXIF metadata (if it’s a TIFF or JPEG file).
The second one returns the field we are interested in. It is accessed with an argument giving the section where to look for it and another argument being the name of the field. These arguments are not mandatory and if not provided, a list of tag is returned [1].
The tag
A new tag #EXIF is declared. Here is a first simple version:
When this tag will be detected, the function
balise_exif will be called without any parameters. Hence, it will return a list of all the fields available in the file.
We begin by getting the name of the file attached to the document. For that we need the ID of the document. This is done by the sugar-function champ_sql that is a shortcut to index_pile:
index_pile returns the position in the stack of the SQL field $nom_champ. Starting from the nearest loop (given by $idb). If nothing is found, the field is considered to come from the outmost context (the URL or include) that is found in
$Pile[0]. If the name references a SQL field, then it is stored in the$bouclestructure to construct the minimal SQL query.
champ_sql will use $param to find the nearest loop, the second parameter is the name of the field: 'id_document'
The code for this tag must be run during the construction of the cache. Hence, we return a string containing the code to run later: "(verifier_JPG_TIFF($id_doc))?(tag_exif(generer_url_document($id_doc))):''"
To distinguish between php and pure html, the type of code is also specified by: $params->type = 'php';
Adding parameters
Well, this new tag is not really useful as is. In fact, we would prefer to extract only the field that we are interested in. But we do not really want to create one tag per field [2]. To avoid that, we will user parameters passed to this tag to select the right field.
Since version 1.8 [3], SPIP provides a simple function to retrieve parameters passed to a tag. This function is used in the following code. For earlier method of retrieving parameters, as for #EXPOSER|on,off, see the logo part of the code explained below.
The same method will be used to add two parameters to the #EXIF tag. For example:#EXIF{FILE,FileName} and #EXIF{IFD0,DateTime} will respectively retrieve the file name field from the File section and the date and time field from the IFD0 section (date and time the photo has been taken).
Here is the new code:
The difference with the code returned by the previous version is small. However, the parameters $section and $tag are retrieved from the filter list (params->fonctions) to be passed (if found) to the function tag_exif.
A tag for a thumbnail
In the EXIF metadata from a jpg image, there is — often — a thumbnail of the image [4]. This thumbnail is not of the best quality, but when you are unable to install any image processing plugin in PHP it is a good start.
Lets begin with two new tool functions:
generer_url_logo_EXIF look for the file name_file.exif.jpg on the server. If this one is not find, the thumbnail is retrieved from the image and written to disk. The name of the thumbnail file is returned at the end.
generer_html_logo_EXIF create the html code for the thumbnail, with alignment, link, etc...
This new tag should have the same behavior as the other LOGO tags in SPIP. Therefor the alignment and link filters should be implemented properly.
The principle is the same as before. The list of filters is parsed for one of the following:
an alignment,
fichier to return the name of the file,
a link to put around the thumbnail.
The first part of the code looks at the filters in $p->fonctions.
Then, the code for the link is generated if necessary. As another tag can be given as a link — e.g. [(#LOGO_EXIF|#URL_DOCUMENT)] — we call the function calculer_champ to compute the content of this tag.
A filter for the dates
The dates stored in the images are not in the same format as the one used by SPIP. Hence, it is not possible to use the filters on dates from SPIP. Here is a filter to apply before any date filter from SPIP:
Application example
Here is a little loop that list all the documents of an article with some basic informations extracted from the EXIF metatags:
Development version
This contrib is managed on spip-zone. You can checkout the last version with:
svn checkout svn://zone.spip.org/spip-zone/_contrib_/_balises_/exif/trunk/

