AJAX Thumbnail Rebuild - Version 1.02

Version Description

  • You can now select which thumbnail sizes you want to rebuild. Thanks to Nicolas Juen!
Download this release

Release Info

Developer junkcoder
Plugin Icon wp plugin AJAX Thumbnail Rebuild
Version 1.02
Comparing to
See all releases

Code changes from version 1.01 to 1.02

Files changed (3) hide show
  1. ajax-thumbnail-rebuild.php +125 -13
  2. readme.txt +8 -2
  3. screenshot-1.jpg +0 -0
ajax-thumbnail-rebuild.php CHANGED
@@ -3,9 +3,9 @@
3
  Plugin URI: http://breiti.cc/wordpress/ajax-thumbnail-rebuild
4
  Author: junkcoder
5
  Author URI: http://breiti.cc
6
- Version: 1.01
7
  Description: Rebuild all thumbnails
8
- Max WP Version: 3.0
9
 
10
  This program is free software; you can redistribute it and/or modify
11
  it under the terms of the GNU General Public License as published by
@@ -34,10 +34,8 @@ class AjaxThumbnailRebuild {
34
  }
35
 
36
  function ManagementPage() {
37
-
38
  ?>
39
  <div id="message" class="updated fade" style="display:none"></div>
40
- <div id="thumb" style="display:none">Last thumbnail: <img id="thumb-img" /></div>
41
  <script type="text/javascript">
42
  // <![CDATA[
43
 
@@ -49,6 +47,15 @@ class AjaxThumbnailRebuild {
49
  function regenerate() {
50
  jQuery("#ajax_thumbnail_rebuild").attr("disabled", true);
51
  setMessage("<p>Reading attachments...</p>");
 
 
 
 
 
 
 
 
 
52
  jQuery.ajax({
53
  url: "<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",
54
  type: "POST",
@@ -60,7 +67,6 @@ class AjaxThumbnailRebuild {
60
  function regenItem() {
61
  if (curr >= list.length) {
62
  jQuery("#ajax_thumbnail_rebuild").removeAttr("disabled");
63
- jQuery("#thumb").hide();
64
  setMessage("Done.");
65
  return;
66
  }
@@ -69,7 +75,7 @@ class AjaxThumbnailRebuild {
69
  jQuery.ajax({
70
  url: "<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",
71
  type: "POST",
72
- data: "action=ajax_thumbnail_rebuild&do=regen&id=" + list[curr].id,
73
  success: function(result) {
74
  jQuery("#thumb").show();
75
  jQuery("#thumb-img").attr("src",result);
@@ -91,22 +97,60 @@ class AjaxThumbnailRebuild {
91
  // ]]>
92
  </script>
93
 
94
- <form method="post" action="">
 
95
  <p>
96
- <input type="button" onClick="javascript:regenerate();" class="button" name="ajax_thumbnail_rebuild" id="ajax_thumbnail_rebuild"
97
- value="<?php _e( 'Regenerate All Thumbnails', 'ajax-thumbnail-rebuild' ) ?>" />
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
  </p>
 
 
 
 
99
  </form>
100
- <p>If you find this plugin useful, I'd be happy to read your comments on the <a href="http://breiti.cc/wordpress/ajax-thumbnail-rebuild" target="_blank">plugin homepage</a>.<br />If you experience any problems, feel free to leave a comment too.</p>
 
 
 
 
 
 
 
 
101
  <?php
102
  }
103
 
104
  };
105
 
106
- add_action('wp_ajax_ajax_thumbnail_rebuild', ajax_thumbnail_rebuild_ajax);
107
-
108
  function ajax_thumbnail_rebuild_ajax() {
109
  $action = $_POST["do"];
 
110
 
111
  if ($action == "getlist") {
112
  $attachments =& get_children( array(
@@ -129,12 +173,80 @@ function ajax_thumbnail_rebuild_ajax() {
129
 
130
  if ( FALSE !== $fullsizepath && @file_exists($fullsizepath) ) {
131
  set_time_limit( 30 );
132
- wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $fullsizepath ) );
133
  }
134
 
135
  die( wp_get_attachment_thumb_url( $id ));
136
  }
137
  }
 
138
 
139
  add_action( 'plugins_loaded', create_function( '', 'global $AjaxThumbnailRebuild; $AjaxThumbnailRebuild = new AjaxThumbnailRebuild();' ) );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  ?>
3
  Plugin URI: http://breiti.cc/wordpress/ajax-thumbnail-rebuild
4
  Author: junkcoder
5
  Author URI: http://breiti.cc
6
+ Version: 1.02
7
  Description: Rebuild all thumbnails
8
+ Max WP Version: 3.0.1
9
 
10
  This program is free software; you can redistribute it and/or modify
11
  it under the terms of the GNU General Public License as published by
34
  }
35
 
36
  function ManagementPage() {
 
37
  ?>
38
  <div id="message" class="updated fade" style="display:none"></div>
 
39
  <script type="text/javascript">
40
  // <![CDATA[
41
 
47
  function regenerate() {
48
  jQuery("#ajax_thumbnail_rebuild").attr("disabled", true);
49
  setMessage("<p>Reading attachments...</p>");
50
+
51
+ inputs = jQuery( 'input:checked' );
52
+ var thumbnails= '';
53
+ if( inputs.length != jQuery( 'input[type=checkbox]' ).length ){
54
+ inputs.each( function(){
55
+ thumbnails += '&thumbnails[]='+jQuery(this).val();
56
+ } );
57
+ }
58
+
59
  jQuery.ajax({
60
  url: "<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",
61
  type: "POST",
67
  function regenItem() {
68
  if (curr >= list.length) {
69
  jQuery("#ajax_thumbnail_rebuild").removeAttr("disabled");
 
70
  setMessage("Done.");
71
  return;
72
  }
75
  jQuery.ajax({
76
  url: "<?php bloginfo('wpurl'); ?>/wp-admin/admin-ajax.php",
77
  type: "POST",
78
+ data: "action=ajax_thumbnail_rebuild&do=regen&id=" + list[curr].id + thumbnails,
79
  success: function(result) {
80
  jQuery("#thumb").show();
81
  jQuery("#thumb-img").attr("src",result);
97
  // ]]>
98
  </script>
99
 
100
+ <form method="post" action="" style="display:inline; float:left; padding-right:30px;">
101
+ <h4>Select which thumbnails you want to rebuild:</h4>
102
  <p>
103
+
104
+ <?php
105
+ global $_wp_additional_image_sizes;
106
+
107
+ foreach ( get_intermediate_image_sizes() as $s ):
108
+
109
+ if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) // For theme-added sizes
110
+ $width = intval( $_wp_additional_image_sizes[$s]['width'] );
111
+ else // For default sizes set in options
112
+ $width = get_option( "{$s}_size_w" );
113
+
114
+ if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) // For theme-added sizes
115
+ $height = intval( $_wp_additional_image_sizes[$s]['height'] );
116
+ else // For default sizes set in options
117
+ $height = get_option( "{$s}_size_h" );
118
+
119
+ if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) // For theme-added sizes
120
+ $crop = intval( $_wp_additional_image_sizes[$s]['crop'] );
121
+ else // For default sizes set in options
122
+ $crop = get_option( "{$s}_crop" );
123
+ ?>
124
+
125
+ <input type="checkbox" name="thumbnails[]" checked="checked" value="<?php echo $s ?>" />
126
+ <label>
127
+ <em><?php echo $s ?></em> (width : <?php echo $width ?>, height : <?php echo $width ?>, crop : <?php echo $crop ?>)
128
+ </label>
129
+ <br/>
130
+ <?php endforeach;?>
131
  </p>
132
+ <input type="button" onClick="javascript:regenerate();" class="button"
133
+ name="ajax_thumbnail_rebuild" id="ajax_thumbnail_rebuild"
134
+ value="<?php _e( 'Regenerate All Thumbnails', 'ajax-thumbnail-rebuild' ) ?>" />
135
+ <br />
136
  </form>
137
+
138
+ <div id="thumb" style="display:none;"><h4>Last image:</h4><img id="thumb-img" /></div>
139
+
140
+ <p style="clear:both; padding-top:2em;">
141
+ If you find this plugin useful, I'd be happy to read your comments on
142
+ the <a href="http://breiti.cc/wordpress/ajax-thumbnail-rebuild" target="_blank">plugin homepage</a>.<br />
143
+ If you experience any problems, feel free to leave a comment too.
144
+ </p>
145
+
146
  <?php
147
  }
148
 
149
  };
150
 
 
 
151
  function ajax_thumbnail_rebuild_ajax() {
152
  $action = $_POST["do"];
153
+ $thumbnails = isset( $_POST['thumbnails'] )? $_POST['thumbnails'] : NULL;
154
 
155
  if ($action == "getlist") {
156
  $attachments =& get_children( array(
173
 
174
  if ( FALSE !== $fullsizepath && @file_exists($fullsizepath) ) {
175
  set_time_limit( 30 );
176
+ wp_update_attachment_metadata( $id, wp_generate_attachment_metadata_custom( $id, $fullsizepath, $thumbnails ) );
177
  }
178
 
179
  die( wp_get_attachment_thumb_url( $id ));
180
  }
181
  }
182
+ add_action('wp_ajax_ajax_thumbnail_rebuild', 'ajax_thumbnail_rebuild_ajax');
183
 
184
  add_action( 'plugins_loaded', create_function( '', 'global $AjaxThumbnailRebuild; $AjaxThumbnailRebuild = new AjaxThumbnailRebuild();' ) );
185
+
186
+
187
+ /**
188
+ * Generate post thumbnail attachment meta data.
189
+ *
190
+ * @since 2.1.0
191
+ *
192
+ * @param int $attachment_id Attachment Id to process.
193
+ * @param string $file Filepath of the Attached image.
194
+ * @return mixed Metadata for attachment.
195
+ */
196
+ function wp_generate_attachment_metadata_custom( $attachment_id, $file, $thumbnails = NULL ) {
197
+ $attachment = get_post( $attachment_id );
198
+
199
+ $metadata = array();
200
+ if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
201
+ $imagesize = getimagesize( $file );
202
+ $metadata['width'] = $imagesize[0];
203
+ $metadata['height'] = $imagesize[1];
204
+ list($uwidth, $uheight) = wp_constrain_dimensions($metadata['width'], $metadata['height'], 128, 96);
205
+ $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
206
+
207
+ // Make the file path relative to the upload dir
208
+ $metadata['file'] = _wp_relative_upload_path($file);
209
+
210
+ // make thumbnails and other intermediate sizes
211
+ global $_wp_additional_image_sizes;
212
+
213
+ foreach ( get_intermediate_image_sizes() as $s ) {
214
+ $sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
215
+ if ( isset( $_wp_additional_image_sizes[$s]['width'] ) )
216
+ $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes
217
+ else
218
+ $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
219
+ if ( isset( $_wp_additional_image_sizes[$s]['height'] ) )
220
+ $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes
221
+ else
222
+ $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
223
+ if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) )
224
+ $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes
225
+ else
226
+ $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
227
+ }
228
+
229
+ $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
230
+
231
+ foreach ($sizes as $size => $size_data ) {
232
+ if( isset( $thumbnails ) )
233
+ if( !in_array( $size, $thumbnails ) )
234
+ continue;
235
+
236
+ $resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] );
237
+
238
+ if ( $resized )
239
+ $metadata['sizes'][$size] = $resized;
240
+ }
241
+
242
+ // fetch additional metadata from exif/iptc
243
+ $image_meta = wp_read_image_metadata( $file );
244
+ if ( $image_meta )
245
+ $metadata['image_meta'] = $image_meta;
246
+
247
+ }
248
+
249
+ return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id );
250
+ }
251
+
252
  ?>
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: junkcoder
3
  Tags: ajax, thumbnail, rebuild, regenerate, admin
4
  Requires at least: 2.8
5
- Tested up to: 3.0
6
- Stable tag: 1.01
7
 
8
  AJAX Thumbnail Rebuild allows you to rebuild all thumbnails at once without script timeouts on your server.
9
 
@@ -13,6 +13,8 @@ AJAX Thumbnail Rebuild allows you to rebuild all thumbnails on your site. There
13
 
14
  Why would you want to rebuild your thumbnails? Wordpress allows you to change the size of thumbnails. This way, you can make the size of thumbnails fit the design of your website. When you change the size to fit for a new theme, all future photos you are going to upload will have this new size. Your old thumbnails won’t be resized. That’s where this plugin comes into action. After changing the image sizes, you can rebuild all thumbnails. But instead of telling the server to recreate all thumbnails at once, they are rebuilt one after another. Rebuilding thumbnails for one photo won’t take all too long, so you won’t run into any script timeouts. Note that you still have to wait until all thumbnails have been rebuilt. If you close the page before the task is completed, you have to start all over again.
15
 
 
 
16
  This plugin requires JavaScript to be enabled.
17
 
18
  == Installation ==
@@ -21,6 +23,10 @@ Upload the plugin to your blog, activate it, done. You can then rebuild all thum
21
 
22
  == Changelog ==
23
 
 
 
 
 
24
  = 1.01 =
25
 
26
  * Tested with Wordpress 3.0
2
  Contributors: junkcoder
3
  Tags: ajax, thumbnail, rebuild, regenerate, admin
4
  Requires at least: 2.8
5
+ Tested up to: 3.0.1
6
+ Stable tag: 1.02
7
 
8
  AJAX Thumbnail Rebuild allows you to rebuild all thumbnails at once without script timeouts on your server.
9
 
13
 
14
  Why would you want to rebuild your thumbnails? Wordpress allows you to change the size of thumbnails. This way, you can make the size of thumbnails fit the design of your website. When you change the size to fit for a new theme, all future photos you are going to upload will have this new size. Your old thumbnails won’t be resized. That’s where this plugin comes into action. After changing the image sizes, you can rebuild all thumbnails. But instead of telling the server to recreate all thumbnails at once, they are rebuilt one after another. Rebuilding thumbnails for one photo won’t take all too long, so you won’t run into any script timeouts. Note that you still have to wait until all thumbnails have been rebuilt. If you close the page before the task is completed, you have to start all over again.
15
 
16
+ You can also select the thumbnail sizes you want to rebuild, so that you don't need to recreate all images if you've just changed one thumbnail-size.
17
+
18
  This plugin requires JavaScript to be enabled.
19
 
20
  == Installation ==
23
 
24
  == Changelog ==
25
 
26
+ = 1.02 =
27
+
28
+ * You can now select which thumbnail sizes you want to rebuild. Thanks to Nicolas Juen!
29
+
30
  = 1.01 =
31
 
32
  * Tested with Wordpress 3.0
screenshot-1.jpg CHANGED
Binary file