Plugin Directory

Changeset 676662


Ignore:
Timestamp:
03/05/2013 10:10:01 PM (13 years ago)
Author:
PeterHudec
Message:

Updating to version 1.7

Location:
image-metadata-cruncher/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • image-metadata-cruncher/trunk/README.txt

    r666836 r676662  
    55Requires at least: 2.7
    66Tested up to: 3.5
    7 Stable tag: 1.6
     7Stable tag: 1.7
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3030> Image was taken with Canon 7D, exposure was 1/125s and aperture was f/2.8.
    3131
    32 You can even extract metadata to unlimited custom **post meta** that will be saved with the image to the database.
     32You can even extract metadata to unlimited custom **post meta** that will be saved
     33with the image to the database.
     34
     35The plugin also has some usefull public methods which you can use in your code:
     36
     37This will return the complete metadata of the image in a stuctured array same as {ALL:PHP}.
     38   
     39    $metadata = $image_metadata_cruncher->get_meta_by_path( '/path/to/your/image.jpg' );
     40   
     41The same but by attachment post ID.
     42   
     43    $metadata = $image_metadata_cruncher->get_meta_by_id( $attachment_ID );
     44   
     45You can crunch an allready uploaded attachment with the `crunch()` method.
     46The template and its indexes are optional. If missing, templates from plugin's settings will be used.
     47   
     48    $template = array(
     49        'title' => '{IPTC:Title}',
     50        'caption' => '{IPTC:Caption}',
     51        'description' => '{IPTC:Headline}',
     52        'alt' => '{IPTC:Caption}',
     53        'custom_meta' => array(
     54            'camera-make' => '{EXIF:Make}',
     55            'camera-model' => '{EXIF:Model}',
     56        ),
     57    )
     58   
     59    $image_metadata_cruncher->crunch( $attachment_ID, $template )
     60
     61
    3362
    3463== Installation ==
     
    42713. How to Use Template tags
    4372
     73== Frequently Asked Questions ==
     74
     75= Is it possible to extract metadata from allready uploaded images? =
     76
     77You can use the plugin's methods like this:
     78
     79This will return the complete metadata of the image in a stuctured array same as {ALL:PHP}.
     80   
     81    $metadata = $image_metadata_cruncher->get_meta_by_path( '/path/to/your/image.jpg' );
     82   
     83The same but by attachment post ID.
     84   
     85    $metadata = $image_metadata_cruncher->get_meta_by_id( $attachment_ID );
     86   
     87You can crunch an allready uploaded attachment with the `crunch()` method:
     88Which will extract metadata from the attachment image file and
     89update the attachment post according to the optional template array.
     90Templates from plugin settings will be used for missing indexes.
     91   
     92    $template = array(
     93        'title' => '{IPTC:Title}',
     94        'caption' => '{IPTC:Caption}',
     95        'description' => '{IPTC:Headline}',
     96        'alt' => '{IPTC:Caption}',
     97        'custom_meta' => array(
     98            'camera-make' => '{EXIF:Make}',
     99            'camera-model' => '{EXIF:Model}',
     100        ),
     101    )
     102   
     103    $image_metadata_cruncher->crunch( $attachment_ID, $template )
     104   
     105
    44106== Changelog ==
     107
     108= 1.7 =
     109* Now you can also use the plugin in your code to extract and crunch metadata of an attachment post.
     110* The syntax highlighting should now work on every browser.
     111* Fixed a bug when text inserted after the syntax highlighting has been disabled didn't get saved.
    45112
    46113= 1.6 =
  • image-metadata-cruncher/trunk/image-metadata-cruncher.php

    r666836 r676662  
    33Plugin Name: Image Metadata Cruncher
    44Description: Gives you ultimate controll over which image metadata (EXIF or IPTC) WordPress extracts from an uploaded image and where and in what form it then goes. You can even specify unlimited custom post meta tags as the target of the extracted image metadata.
    5 Version: 1.6
     5Version: 1.7
    66Author: Peter Hudec
    77Author URI: http://peterhudec.com
     
    1414 * Main plugin class
    1515 */
    16 class Image_Metadata_Cruncher_Plugin {
     16class Image_Metadata_Cruncher {
    1717   
    1818    // stores metadata between wp_handle_upload_prefilter and add_attachment hooks
     
    2323    private $pattern;
    2424    public $plugin_name = 'Image Metadata Cruncher';
    25     private $version = 1.6;
     25    private $version = 1.7;
    2626    private $after_update = FALSE;
    2727    private $settings_slug = 'image_metadata_cruncher-options';
     
    8888           
    8989        // get meta
    90        
    91         $this->metadata = $this->extract_metadata( $file );
     90        $this->metadata = $this->get_meta_by_path( $file['name'], $file['tmp_name'] );
    9291       
    9392        // return untouched file
     
    9998     * In Wordpress media uploads are handled as posts.
    10099     */
    101     public function add_attachment( $post_ID ) {
     100    public function add_attachment( $post_ID, $template = array() ) {
    102101       
    103102        // get plugin options
     
    107106        $post = get_post( $post_ID );
    108107       
     108        // Try to get template from $template array then from $options.
     109        $title = isset( $template['title'] ) ? $template['title'] : $options['title'];
     110        $caption = isset( $template['caption'] ) ? $template['caption'] : $options['caption'];
     111        $description = isset( $template['description'] ) ? $template['description'] : $options['description'];
     112        $alt = isset( $template['alt'] ) ? $template['alt'] : $options['alt'];
     113       
     114        if ( isset( $template['custom_meta'] ) ) {
     115            $meta = $template['custom_meta'];
     116        } elseif ( isset( $options['custom_meta'] ) ) {
     117            $meta = $options['custom_meta'];
     118        } else {
     119            $meta = array();
     120        }
     121       
     122        // Apply new values to post properties:
     123       
    109124        // title
    110         $post->post_title = $this->render_template( $options['title'] );
     125        $post->post_title = $this->render_template( $title );
    111126        // caption
    112         $post->post_excerpt = $this->render_template( $options['caption'] );
     127        $post->post_excerpt = $this->render_template( $caption );
    113128        // description
    114         $post->post_content = $this->render_template( $options['description'] );
     129        $post->post_content = $this->render_template( $description );
    115130        // alt is meta attribute
    116         update_post_meta( $post_ID, '_wp_attachment_image_alt', $this->render_template( $options['alt'] ) );
     131        update_post_meta( $post_ID, '_wp_attachment_image_alt', $this->render_template( $alt ) );
    117132       
    118133        // add custom post meta if any
    119         if ( isset( $options['custom_meta'] ) ) {
    120             foreach ( $options['custom_meta'] as $key => $value ) {
    121                 // get value
    122                 $value = $this->render_template( $value );
    123                
    124                 // update or create the post meta
    125                 add_post_meta( $post_ID, $key, $value, true ) or update_post_meta( $post_ID, $key, $value );
    126             }
     134        foreach ( $meta as $key => $value ) {
     135            // get value
     136            $value = $this->render_template( $value );
     137           
     138            // update or create the post meta
     139            add_post_meta( $post_ID, $key, $value, true ) or update_post_meta( $post_ID, $key, $value );
    127140        }
    128141       
    129142        // finally sanitize and update post
    130         sanitize_post( $post );
     143        // sanitize_post( $post );
    131144        wp_update_post( $post );
    132145    }
    133146   
    134     /**
    135      * Extracts image metadata of the supplied image
     147   
     148    /**
     149     * Extracts image metadata from the image specified by its path.
    136150     *
    137151     * @return structured array with all available metadata
    138152     */
    139     private function extract_metadata( $file ) {
     153    public function get_meta_by_path( $name, $tmp_name = NULL ) {
     154       
     155        if ( !$tmp_name ) {
     156            $tmp_name = $name;
     157        }
    140158       
    141159        $this->metadata = array();
     
    143161        // extract metadata from file
    144162        //  the $meta variable will be populated with it
    145         $size = getimagesize( $file['tmp_name'], $meta );
     163        $size = getimagesize( $tmp_name, $meta );
    146164       
    147165        // extract pathinfo and merge with size
    148         $this->metadata['Image'] = array_merge( $size, pathinfo( $file['name'] ) );
     166        $this->metadata['Image'] = array_merge( $size, pathinfo( $name ) );
    149167       
    150168        // remove index 'dirname'
     
    198216        if ( in_array( $size['mime'], $safe_file_formats ) ) {
    199217       
    200             $exif = exif_read_data( $file['tmp_name'] );
     218            $exif = exif_read_data( $tmp_name );
    201219           
    202220            if ( is_array( $exif ) ) {
     
    228246    }
    229247   
     248   
     249    /**
     250     * Extracts image metadata from the image specified by the attachment post ID.
     251     *
     252     * @return structured array with all available metadata
     253     */
     254    public function get_meta_by_id( $ID ) {
     255        $post = get_post( $ID );
     256        return $this->get_meta_by_path( $post->guid );
     257    }
     258   
     259    /**
     260     * Extracts metadata from the image file belonging to the attachment post
     261     * specified by the $ID and updates the post according to supplied $template array.
     262     * The $template array should have following structure:
     263     *
     264     * $template = array(
     265     *      'title' => 'Title template',
     266     *      'caption' => 'Caption template',
     267     *      'description' => 'Description template',
     268     *      'alt' => 'Alt template',
     269     *      'custom_meta' => array(
     270     *          'meta-name' => 'Meta template',
     271     *          'another-meta-name' => 'Another meta template',
     272     *      ),
     273     * )
     274     *
     275     * Settings templates will be used for missing indexes in the array.
     276     */
     277    public function crunch( $ID, $template = array() ) {
     278        # Get the attachment post.
     279        $post = get_post( $ID );
     280       
     281        # Extract metadata.
     282        $this->get_meta_by_id( $ID );
     283       
     284        # Update attachment.
     285        $this->add_attachment( $ID, $template );
     286    }
     287   
     288   
    230289    /**
    231290     * Replaces template tags in template string.
     
    238297        $template = str_replace(
    239298            array(
     299                '<',
    240300                '>',
    241301                ''',
     
    243303            ),
    244304            array(
     305                '<',
    245306                '>',
    246307                "'",
     
    803864        $options = get_option( $this->prefix );
    804865       
    805         if ( intval( $options['version'] ) > 1.0 ) {
     866        if ( isset( $options['version'] ) && intval( $options['version'] ) > 1.0 ) {
    806867            //TODO: Move to defaults
    807868            // if the plugin has been updated from version 1.0 enable highlighting by default
     
    15271588
    15281589// instantiate the plugin
    1529 $image_metadata_cruncher_plugin = new Image_Metadata_Cruncher_Plugin();
     1590$image_metadata_cruncher = new Image_Metadata_Cruncher();
    15301591
    15311592?>
  • image-metadata-cruncher/trunk/js/script.js

    r627803 r676662  
    9191    $('#metadata-cruncher').delegate('.highlighted', 'keyup', function(event) {
    9292        var $target = $(event.target);
     93        var text;
    9394       
    9495        // highlight and get non HTML text
    9596        if(enableHighlighting()){
    96             var text = highlight(event);
     97            text = highlight(event);
     98        }else{
     99            text = $target.text();
    97100        }
    98101       
     
    206209            // replace rangy boundary markers with this unusual unicode character \u25A8 which survives html to text conversion
    207210            //   and save them to a temporary array
    208             var p = /(<span[\s]*id="selectionBoundary[^<>]*>[^<>]*<\/[\s]*span>)/g;
     211            var p = /<span[^>]*rangySelectionBoundary[^>]*>[^<]*<\/[^>]*>/g
    209212            var markers = [];
    210213            var html = $in.html().replace(p, function(match){
Note: See TracChangeset for help on using the changeset viewer.