all repos — wp-extraexif @ cc04a4c0b35b3f2278cff1e2981a49fe3f4eb626

0.4
Peter Molnar hello@petermolnar.eu
Wed, 07 Sep 2016 09:03:27 +0000
commit

cc04a4c0b35b3f2278cff1e2981a49fe3f4eb626

parent

4d20641b1b0320c2c011fa6b8c8ab6e448ad7965

3 files changed, 51 insertions(+), 92 deletions(-)

jump to
M composer.jsoncomposer.json

@@ -1,8 +1,8 @@

{ "name": "petermolnar/wp-extraexif", - "description": "Autofill content and tags from featured image EXIF & IPTC", + "description": "WordPress plugin to extract EXIF via exiftool", "require": { - "php": ">=5.3.0", + "php": ">=5.4.0", }, "license": "GPLv3", "authors": [
M readme.txtreadme.txt

@@ -1,13 +1,13 @@

=== wp-extraexif === Contributors: cadeyrn -Tags: -Requires at least: 3.0 -Tested up to: 4.5.3 -Stable tag: 0.1 +Tags: exif, image, media +Requires at least: 4.0 +Tested up to: 4.6 +Stable tag: 0.4 License: GPLv3 License URI: http://www.gnu.org/licenses/gpl-3.0.html -Autofill content and tags from featured image EXIF & IPTC +A plugin that uses [exiftool](http://owl.phy.queensu.ca/~phil/exiftool/) to read EXIF values of an image. == Description ==

@@ -25,6 +25,17 @@

* every A. indicates BIG changes. * every .B version indicates new features. * every ..C indicates bugfixes for A.B version. + += 0.4 = +*2016-09-07* + +* filters added for extractable EXIF values + += 0.3 = +*2016-08-20* + +* EXIF is not merged with WordPress attachment meta any more but instead created as standalone JSON files based on the full file path hash, so it's easy to read. + = 0.1 = *2016-07-22*
M wp-extraexif.phpwp-extraexif.php

@@ -2,8 +2,8 @@ <?php

/* Plugin Name: wp-extraexif Plugin URI: https://github.com/petermolnar/wp-extraexif -Description: Read extra EXIF for images with exiftool -Version: 0.2 +Description: Read EXIF for images with cli `exiftool` +Version: 0.4 Author: Peter Molnar <hello@petermolnar.net> Author URI: http://petermolnar.net/ License: GPLv3

@@ -29,39 +29,12 @@ namespace WP_EXTRAEXIF;

\add_action( 'init', 'WP_EXTRAEXIF\init' ); \register_activation_hook( __FILE__ , '\WP_EXTRAEXIF\plugin_activate' ); -\register_deactivation_hook( __FILE__ , '\WP_EXTRAEXIF\plugin_deactivate' ); +//\register_deactivation_hook( __FILE__ , '\WP_EXTRAEXIF\plugin_deactivate' ); -define ( 'WP_EXTRAEXIF\CACHEDIR', \WP_CONTENT_DIR . DIRECTORY_SEPARATOR. +define ( 'WP_EXTRAEXIF\CACHE', \WP_CONTENT_DIR . DIRECTORY_SEPARATOR. 'cache' . DIRECTORY_SEPARATOR . 'exif' . DIRECTORY_SEPARATOR ); /** - * - */ -function defaults() { - // hardcoded - $config = array ( - // exiftool value => store as meta key - 'LensID' => 'lens', - 'GPSLatitude' => 'geo_latitude', - 'GPSLongitude' => 'geo_longitude', - 'GPSAltitude' => 'geo_altitude', - 'Title' => 'title', - ); - - $ini = dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'config.ini'; - if ( file_exists ( $ini ) ) { - $config = array_merge ( $config, parse_ini_file( $ini ) ); - } - - $current = \get_option( __NAMESPACE__ ); - - if ( $current != $config ) - \update_option( __NAMESPACE__, $config ); - - return $config; -} - -/** * activate hook */ function plugin_activate() {

@@ -74,7 +47,7 @@

if ( true !== $test ) die ( $test ); - $dirs = [ CACHEDIR ]; + $dirs = [ CACHE ]; foreach ( $dirs as $dir ) { if ( ! is_dir( $dir ) ) { if ( ! mkdir( $dir ) )

@@ -84,19 +57,12 @@ }

} /** - * activate hook - */ -function plugin_deactivate() { - \delete_option( __NAMESPACE__ ); -} - -/** * */ function init() { add_filter( 'wp_read_image_metadata', 'WP_EXTRAEXIF\read_extra_exif', 1, 3 ); - $dirs = [ CACHEDIR ]; + $dirs = [ CACHE ]; foreach ( $dirs as $dir ) { if ( ! is_dir( $dir ) ) { if ( ! mkdir( $dir ) )

@@ -105,6 +71,10 @@ }

} } +/** + * check the existence and executability of exiftool + * + */ function test_exiftool () { if ( ! function_exists( 'exec' ) ) return "This plugin requires `exec` function which is not available.";

@@ -141,49 +111,6 @@ debug ( "can't find exiftool", 4 );

return $meta; } - $extra = \get_option( __NAMESPACE__, defaults() ); - - $args = $metaextra = array(); - - foreach ($extra as $exiftoolID => $metaid ) { - // only try to get the missing - if ( ! isset( $meta[ $metaid ]) ) { - $args[] = $exiftoolID; - } - } - - if ( empty( $args ) ) - return $meta; - - $args = join(' -', $args); - $cmd = "exiftool -s -{$args} {$path}"; - - //debug("Extracting extra EXIF for {$path} with command {$cmd}", 7 ); - exec( $cmd, $exif, $retval); - - if ($retval != 0 ) { - debug("Extracting extra EXIF failed with error code ${retval}", 4 ); - return $meta; - } - - foreach ( $exif as $cntr => $data ) { - $data = array_map( 'trim', explode (' : ', $data ) ); - - if ( $data[0] == 'GPSLatitude' || $data[0] == 'GPSLongitude' ) - $data[1] = exif_gps2dec( $data[1] ); - elseif ( $data[0] == 'GPSAltitude' ) - $data[1] = exif_gps2alt( $data[1] ); - - $metaextra[ $extra[ $data[0] ] ] = $data[1]; - } - - if ( ! empty( $metaextra ) ) { - //debug ( "Adding extra EXIF", 7); - //debug ( $metaextra, 7 ); - $meta = array_merge($meta, $metaextra); - } - - //debug ( $path ); exif_cache( $path ); return $meta;

@@ -216,15 +143,33 @@ $alt = $alt * -1;

return $alt; } +/** + * + */ +function clear_cache() { + $list = scandir( CACHE ); + + foreach ($list as $key => $name ) { + $path = realpath( CACHE . $name ); + + if ( is_file( $path ) && ! in_array ( $name, array( '.', '..' ) ) ) { + unlink( $path ); + } + } +} + +/** + * + */ function exif_cache( $jpg ) { if ( ! is_file( $jpg ) ) { - debug( "nonexistend JPG file at {$jpg}", 4 ); + debug( "nonexistent JPG file at {$jpg}", 4 ); return; } $hash = md5 ( $jpg ); - $cached = CACHEDIR . $hash; + $cached = CACHE . $hash; $img_timestamp = @filemtime ( $jpg ); if ( is_file( $cached ) ) {

@@ -251,11 +196,13 @@ 'ISO',

'Create Date', 'Copyright Notice', ]; + $filters = \apply_filters( 'wp_extraexif_list', $filters ); $merges = [ 'Shutter Speed' => 'Exposure Time', 'Aperture' => 'F Number', ]; + $merges = \apply_filters( 'wp_extraexif_merges', $merges ); $mapping = [ 'Make' => 'make',

@@ -273,6 +220,7 @@ 'ISO' => 'iso',

'Create Date' => 'date', 'Copyright Notice' => 'copyright', ]; + $mapping = \apply_filters( 'wp_extraexif_mapping', $mapping ); $cmd = "exiftool {$jpg}"; exec( $cmd, $exif_raw, $retval);