Changeset 676662
- Timestamp:
- 03/05/2013 10:10:01 PM (13 years ago)
- Location:
- image-metadata-cruncher/trunk
- Files:
-
- 3 edited
-
README.txt (modified) (3 diffs)
-
image-metadata-cruncher.php (modified) (13 diffs)
-
js/script.js (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
image-metadata-cruncher/trunk/README.txt
r666836 r676662 5 5 Requires at least: 2.7 6 6 Tested up to: 3.5 7 Stable tag: 1. 67 Stable tag: 1.7 8 8 License: GPLv2 or later 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 30 30 > Image was taken with Canon 7D, exposure was 1/125s and aperture was f/2.8. 31 31 32 You can even extract metadata to unlimited custom **post meta** that will be saved with the image to the database. 32 You can even extract metadata to unlimited custom **post meta** that will be saved 33 with the image to the database. 34 35 The plugin also has some usefull public methods which you can use in your code: 36 37 This 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 41 The same but by attachment post ID. 42 43 $metadata = $image_metadata_cruncher->get_meta_by_id( $attachment_ID ); 44 45 You can crunch an allready uploaded attachment with the `crunch()` method. 46 The 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 33 62 34 63 == Installation == … … 42 71 3. How to Use Template tags 43 72 73 == Frequently Asked Questions == 74 75 = Is it possible to extract metadata from allready uploaded images? = 76 77 You can use the plugin's methods like this: 78 79 This 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 83 The same but by attachment post ID. 84 85 $metadata = $image_metadata_cruncher->get_meta_by_id( $attachment_ID ); 86 87 You can crunch an allready uploaded attachment with the `crunch()` method: 88 Which will extract metadata from the attachment image file and 89 update the attachment post according to the optional template array. 90 Templates 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 44 106 == 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. 45 112 46 113 = 1.6 = -
image-metadata-cruncher/trunk/image-metadata-cruncher.php
r666836 r676662 3 3 Plugin Name: Image Metadata Cruncher 4 4 Description: 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. 65 Version: 1.7 6 6 Author: Peter Hudec 7 7 Author URI: http://peterhudec.com … … 14 14 * Main plugin class 15 15 */ 16 class Image_Metadata_Cruncher _Plugin{16 class Image_Metadata_Cruncher { 17 17 18 18 // stores metadata between wp_handle_upload_prefilter and add_attachment hooks … … 23 23 private $pattern; 24 24 public $plugin_name = 'Image Metadata Cruncher'; 25 private $version = 1. 6;25 private $version = 1.7; 26 26 private $after_update = FALSE; 27 27 private $settings_slug = 'image_metadata_cruncher-options'; … … 88 88 89 89 // get meta 90 91 $this->metadata = $this->extract_metadata( $file ); 90 $this->metadata = $this->get_meta_by_path( $file['name'], $file['tmp_name'] ); 92 91 93 92 // return untouched file … … 99 98 * In Wordpress media uploads are handled as posts. 100 99 */ 101 public function add_attachment( $post_ID ) {100 public function add_attachment( $post_ID, $template = array() ) { 102 101 103 102 // get plugin options … … 107 106 $post = get_post( $post_ID ); 108 107 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 109 124 // title 110 $post->post_title = $this->render_template( $ options['title']);125 $post->post_title = $this->render_template( $title ); 111 126 // caption 112 $post->post_excerpt = $this->render_template( $ options['caption']);127 $post->post_excerpt = $this->render_template( $caption ); 113 128 // description 114 $post->post_content = $this->render_template( $ options['description']);129 $post->post_content = $this->render_template( $description ); 115 130 // 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 ) ); 117 132 118 133 // 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 ); 127 140 } 128 141 129 142 // finally sanitize and update post 130 sanitize_post( $post );143 // sanitize_post( $post ); 131 144 wp_update_post( $post ); 132 145 } 133 146 134 /** 135 * Extracts image metadata of the supplied image 147 148 /** 149 * Extracts image metadata from the image specified by its path. 136 150 * 137 151 * @return structured array with all available metadata 138 152 */ 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 } 140 158 141 159 $this->metadata = array(); … … 143 161 // extract metadata from file 144 162 // the $meta variable will be populated with it 145 $size = getimagesize( $ file['tmp_name'], $meta );163 $size = getimagesize( $tmp_name, $meta ); 146 164 147 165 // 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 ) ); 149 167 150 168 // remove index 'dirname' … … 198 216 if ( in_array( $size['mime'], $safe_file_formats ) ) { 199 217 200 $exif = exif_read_data( $ file['tmp_name']);218 $exif = exif_read_data( $tmp_name ); 201 219 202 220 if ( is_array( $exif ) ) { … … 228 246 } 229 247 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 230 289 /** 231 290 * Replaces template tags in template string. … … 238 297 $template = str_replace( 239 298 array( 299 '<', 240 300 '>', 241 301 ''', … … 243 303 ), 244 304 array( 305 '<', 245 306 '>', 246 307 "'", … … 803 864 $options = get_option( $this->prefix ); 804 865 805 if ( i ntval( $options['version'] ) > 1.0 ) {866 if ( isset( $options['version'] ) && intval( $options['version'] ) > 1.0 ) { 806 867 //TODO: Move to defaults 807 868 // if the plugin has been updated from version 1.0 enable highlighting by default … … 1527 1588 1528 1589 // instantiate the plugin 1529 $image_metadata_cruncher _plugin = new Image_Metadata_Cruncher_Plugin();1590 $image_metadata_cruncher = new Image_Metadata_Cruncher(); 1530 1591 1531 1592 ?> -
image-metadata-cruncher/trunk/js/script.js
r627803 r676662 91 91 $('#metadata-cruncher').delegate('.highlighted', 'keyup', function(event) { 92 92 var $target = $(event.target); 93 var text; 93 94 94 95 // highlight and get non HTML text 95 96 if(enableHighlighting()){ 96 var text = highlight(event); 97 text = highlight(event); 98 }else{ 99 text = $target.text(); 97 100 } 98 101 … … 206 209 // replace rangy boundary markers with this unusual unicode character \u25A8 which survives html to text conversion 207 210 // and save them to a temporary array 208 var p = / (<span[\s]*id="selectionBoundary[^<>]*>[^<>]*<\/[\s]*span>)/g;211 var p = /<span[^>]*rangySelectionBoundary[^>]*>[^<]*<\/[^>]*>/g 209 212 var markers = []; 210 213 var html = $in.html().replace(p, function(match){
Note: See TracChangeset
for help on using the changeset viewer.