2016-07-25 16:07:33 +01:00
< ? php
/*
Plugin Name : wp - extraexif
Plugin URI : https :// github . com / petermolnar / wp - extraexif
2016-09-07 10:03:27 +01:00
Description : Read EXIF for images with cli `exiftool`
2017-07-14 11:56:15 +01:00
Version : 1.0
Author : Peter Molnar < hello @ petermolnar . eu >
2016-07-25 16:07:33 +01:00
Author URI : http :// petermolnar . net /
License : GPLv3
*/
2017-07-14 11:56:15 +01:00
/* Copyright 2016 - 2017 Peter Molnar ( hello @ petermolnar . eu )
2016-07-25 16:07:33 +01:00
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License , version 3 , as
published by the Free Software Foundation .
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 St , Fifth Floor , Boston , MA 02110 - 1301 USA
*/
2017-07-14 11:56:15 +01:00
add_action ( 'init' , 'WP_EXTRAEXIF_init' );
register_activation_hook ( __FILE__ , 'WP_EXTRAEXIF_plugin_activate' );
2016-08-20 18:15:38 +01:00
2016-07-25 16:07:33 +01:00
/**
* activate hook
*/
2017-07-14 11:56:15 +01:00
function WP_EXTRAEXIF_plugin_activate () {
if ( ! function_exists ( 'shell_exec' ) )
die ( " This plugin requires `exec` function to call `exiftool` and it is not available. " );
2016-07-25 16:07:33 +01:00
$cmd = 'exiftool -ver' ;
2017-07-14 11:56:15 +01:00
$r = shell_exec ( $cmd );
2016-07-25 16:07:33 +01:00
2017-07-14 11:56:15 +01:00
if ( empty ( $r ) )
die ( " exiftool cannot be executed via `exec`. This plugin requires `exiftool` to be installed on the system and available in \$ PATH variable; please talk to your system administrator. " );
2016-07-25 16:07:33 +01:00
}
2017-07-14 11:56:15 +01:00
function WP_EXTRAEXIF_init () {
add_filter ( 'wp_read_image_metadata' , 'WP_EXTRAEXIF_read_meta' , 1 , 4 );
2016-07-25 16:07:33 +01:00
}
2017-07-14 11:56:15 +01:00
function WP_EXTRAEXIF_read_meta ( $meta , $file , $sourceImageType , $iptc ) {
$meta = WP_EXTRAEXIF_run ( $meta , $file );
return $meta ;
2016-09-07 10:03:27 +01:00
}
2017-07-14 11:56:15 +01:00
function WP_EXTRAEXIF_run ( $current_meta , $file ) {
if ( isset ( $current_meta [ 'extraexif' ]) && ! empty ( $current_meta [ 'extraexif' ])) {
return $current_meta ;
2016-08-20 18:15:38 +01:00
}
2017-08-10 13:06:22 +01:00
$vars = array (
'-sort' ,
'-json' ,
'-MIMEType' ,
'-FileType' ,
'-FileName' ,
'-ModifyDate' ,
'-CreateDate' ,
'-DateTimeOriginal' ,
'-ImageHeight' ,
'-ImageWidth' ,
'-Aperture' ,
'-FOV' ,
'-ISO' ,
'-FocalLength' ,
'-FNumber' ,
'-FocalLengthIn35mmFormat' ,
'-ExposureTime' ,
'-Copyright' ,
'-Artist' ,
'-Model' ,
'-GPSLongitude#' ,
'-GPSLatitude#' ,
'-LensID'
);
2017-07-14 11:56:15 +01:00
$vars = apply_filters ( 'wp_extraexif_exiftool_vars' , $vars );
array_unshift ( $vars , 'exiftool' );
array_push ( $vars , $file );
$cmd = join ( ' ' , $vars );
WP_EXTRAEXIF_debug ( " executing: { $cmd } " , LOG_DEBUG );
$meta = $current_meta ;
$out = shell_exec ( $cmd );
$out = json_decode ( $out );
$out = array_pop ( $out );
foreach ( $out as $mkey => $mvalue ) {
if ( isset ( $current_meta [ 'image_meta' ][ $mkey ])) {
2016-08-20 18:15:38 +01:00
continue ;
2017-07-14 11:56:15 +01:00
}
2016-08-20 18:15:38 +01:00
2017-07-14 11:56:15 +01:00
$current_meta [ 'image_meta' ][ $mkey ] = $mvalue ;
2016-08-20 18:15:38 +01:00
}
2017-07-14 11:56:15 +01:00
$current_meta [ 'extraexif' ] = True ;
WP_EXTRAEXIF_debug ( " returning: " . var_export ( $current_meta , True ), LOG_DEBUG );
2016-08-20 18:15:38 +01:00
2017-07-14 11:56:15 +01:00
return $current_meta ;
2016-08-20 18:15:38 +01:00
}
2016-07-25 16:07:33 +01:00
/**
*
* debug messages ; will only work if WP_DEBUG is on
* or if the level is LOG_ERR , but that will kill the process
*
* @ param string $message
* @ param int $level
*
* @ output log to syslog | wp_die on high level
* @ return false on not taking action , true on log sent
*/
2017-07-14 11:56:15 +01:00
function WP_EXTRAEXIF_debug ( $message , $level = LOG_NOTICE ) {
2016-07-25 16:07:33 +01:00
if ( empty ( $message ) )
return false ;
if ( @ is_array ( $message ) || @ is_object ( $message ) )
$message = json_encode ( $message );
$levels = array (
LOG_EMERG => 0 , // system is unusable
LOG_ALERT => 1 , // Alert action must be taken immediately
LOG_CRIT => 2 , // Critical critical conditions
LOG_ERR => 3 , // Error error conditions
LOG_WARNING => 4 , // Warning warning conditions
LOG_NOTICE => 5 , // Notice normal but significant condition
LOG_INFO => 6 , // Informational informational messages
LOG_DEBUG => 7 , // Debug debug-level messages
);
// number for number based comparison
// should work with the defines only, this is just a make-it-sure step
$level_ = $levels [ $level ];
// ERR, CRIT, ALERT and EMERG
if ( 3 >= $level_ ) {
2017-07-14 11:56:15 +01:00
wp_die ( '<h1>Error:</h1>' . '<p>' . $message . '</p>' );
2016-07-25 16:07:33 +01:00
exit ;
}
$trace = debug_backtrace ();
$caller = $trace [ 1 ];
$parent = $caller [ 'function' ];
return error_log ( " { $parent } : { $message } " );
}