Post Thumbnail Editor - Version 0.2.1

Version Description

Fixed an error with older PHP installs

Download this release

Release Info

Developer sewpafly
Plugin Icon Post Thumbnail Editor
Version 0.2.1
Comparing to
See all releases

Code changes from version 0.1.1 to 0.2.1

README.txt CHANGED
@@ -23,11 +23,13 @@ To meet the needs of themes where the post-thumbnails have random and capricious
23
 
24
  == Installation ==
25
 
26
- 1. Upload `plugin-name.php` to the `/wp-content/plugins/` directory
27
- 2. Activate the plugin through the 'Plugins' menu in WordPress
28
- 3. Rock On
 
29
 
30
  = or =
 
31
  1. Install from within your wordpress admin area by searching for "post thumbnail editor"
32
 
33
  == Frequently Asked Questions ==
@@ -36,6 +38,10 @@ To meet the needs of themes where the post-thumbnails have random and capricious
36
 
37
  No, thanks for asking. But [let me know if you're having problems](https://github.com/sewpafly/post-thumbnail-editor/issues) and I'll see what I can do.
38
 
 
 
 
 
39
  == Screenshots ==
40
 
41
  1. Before/After
@@ -44,6 +50,10 @@ No, thanks for asking. But [let me know if you're having problems](https://githu
44
 
45
  == Changelog ==
46
 
 
 
 
 
47
  = 0.1.1 =
48
  * Fixed IE8/firefox javascript errors
49
 
@@ -52,5 +62,11 @@ No, thanks for asking. But [let me know if you're having problems](https://githu
52
 
53
  == Upgrade Notice ==
54
 
 
 
 
 
 
 
55
  = 0.1.1 =
56
  This version fixes a IE8/firefox javascript error.
23
 
24
  == Installation ==
25
 
26
+ 1. Download the zip file from <http://downloads.wordpress.org/plugin/post-thumbnail-editor.zip>
27
+ 2. Unzip to your wp-content/plugins directory under the wordpress installation.
28
+ 3. Activate the plugin through the 'Plugins' menu in WordPress
29
+ 4. Rock On
30
 
31
  = or =
32
+
33
  1. Install from within your wordpress admin area by searching for "post thumbnail editor"
34
 
35
  == Frequently Asked Questions ==
38
 
39
  No, thanks for asking. But [let me know if you're having problems](https://github.com/sewpafly/post-thumbnail-editor/issues) and I'll see what I can do.
40
 
41
+ = Is there a way to regenerate images created by this plugin? =
42
+
43
+ Not yet, Coming in the next release (0.3). However, I'm hopeful that any images you've created with previous versions will be able to be regenerated by release 0.3.
44
+
45
  == Screenshots ==
46
 
47
  1. Before/After
50
 
51
  == Changelog ==
52
 
53
+ = 0.2 =
54
+ * Added support to change thumbnails aspect ratio. Previously a square image was generated. (Only works for "medium" or "large" thumbnails by default. Will work for "thumbnail" size if the crop checkbox isn't checked under Media Settings).
55
+ * Thumbnails are appended with "-pte" to differentiate images created with this plugin
56
+
57
  = 0.1.1 =
58
  * Fixed IE8/firefox javascript errors
59
 
62
 
63
  == Upgrade Notice ==
64
 
65
+ = 0.2.1 =
66
+ Fixed an error with older PHP installs
67
+
68
+ = 0.2 =
69
+ Change thumbnails aspect ratio (only works for medium/large. Will work for thumbnail if the crop checkbox isn't checked under Media Settings)
70
+
71
  = 0.1.1 =
72
  This version fixes a IE8/firefox javascript error.
html/editor.phtml DELETED
@@ -1,33 +0,0 @@
1
- <!DOCTYPE HTML>
2
- <html>
3
- <head>
4
- <meta http-equiv="content-type" content="text/html; charset=utf-8">
5
- <title>Editor</title>
6
- <?php
7
- wp_print_styles();
8
- wp_print_scripts();
9
- ?>
10
- <script type="text/javascript" charset="utf-8">
11
-
12
- jQuery(document).ready(function($){
13
- var timeout = 5;
14
-
15
- function countdown(){
16
- if (timeout-- <= 0){
17
- parent.jQuery.fancybox.close();
18
- return;
19
- }
20
- else {
21
- $("h1").html("Closing in " + timeout + " seconds.");
22
- window.setTimeout(countdown, 1000);
23
- }
24
- }
25
-
26
- window.setTimeout(countdown, 1000);
27
- });
28
- </script>
29
- </head>
30
- <body>
31
- <h1>BOOM</h1>
32
- </body>
33
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
js/pte_admin_media.js CHANGED
@@ -1,18 +1,21 @@
1
  // log
2
  function log(obj){
3
- if (!window.console || !console.firebug)
4
  {
5
- var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
6
- "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
7
 
8
- window.console = {};
9
- for (var i = 0; i < names.length; ++i)
10
- window.console[names[i]] = function() {}
 
11
  }
12
  if (!console) console = window.console;
13
 
 
 
 
14
  //console.log(obj);
15
- //alert(obj);
16
  }
17
 
18
  jQuery(document).ready(function($){
@@ -32,13 +35,14 @@ jQuery(document).ready(function($){
32
  }
33
  }
34
 
 
35
  function imgDebug(img, s){
36
- $('#pte-debug').html( "x1: " + s.x1 + "<br />"
37
- + "y1: " + s.y1 + "<br />"
38
- + "x2: " + s.x2 + "<br />"
39
- + "y2: " + s.y2 + "<br />"
40
- + "width: " + s.width + "<br />"
41
- + "height: " + s.height + "<br />");
42
  }
43
 
44
  function closeImgAreaSelect(){
@@ -167,31 +171,44 @@ jQuery(document).ready(function($){
167
 
168
  // Find the aspect ratio
169
  var sizes = $('body').data('sizes');
170
- var cd = gcd( parseInt(sizes[size]['width'])
171
- , parseInt(sizes[size]['height']));
172
- var ar = null;
173
- if (cd){
174
- ar = parseInt(sizes[size]['width']) / cd
175
- + ":"
176
- + parseInt(sizes[size]['height']) / cd;
177
- }
178
-
179
 
180
  // Get the images (the fake one & the thumbnail)
181
  //var main_img_url = $('#image-preview-'+id).attr('src');
182
  createPteDisplay();
183
  var img_preview = $('#image-preview-'+id)
184
  ias_instance = img_preview
185
- .clone(false)
186
- .appendTo('#pte-edit')
187
- .css({'height': img_preview.height()})
188
- //.removeAttr('onload')
189
- .imgAreaSelect( { handles: true
190
- , zIndex: 1200
191
- , instance: true
192
- , aspectRatio: ar
193
- , onSelectEnd: imgDebug
194
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
196
  $.get( ajaxurl
197
  , { 'action': 'pte_ajax'
@@ -244,19 +261,19 @@ jQuery(document).ready(function($){
244
  + "<input class='button-primary' type='submit' name='pte-edit-thumb' id='pte-submit' value='Edit'/>"
245
  + "</div");
246
 
247
- log("Attempts remaining: " + pte_max_attempts);
248
  var editor = $(".imgedit-settings");
249
  if (editor.size() < 1 && pte_max_attempts-- < 0){
250
- log("Tried too many times, stopping");
251
  return;
252
  }
253
  else if (editor.size() < 1){
254
- log("No editor... Try again.");
255
  window.setTimeout(getImageEditor, pte_timeout);
256
  return;
257
  }
258
 
259
- log("Found: " + editor.size());
260
 
261
  // Action: create image pop-up
262
  // Used when the user selects a post thumbnail to edit
@@ -276,12 +293,12 @@ jQuery(document).ready(function($){
276
  });
277
 
278
  // Get list of available sizes
279
- log("ADMINAJAX: " + ajaxurl);
280
  $.get(ajaxurl
281
  , { 'action': 'pte_ajax'
282
  , 'pte_action': 'get-alternate-sizes'}
283
  , function(data, status, xhr){
284
- log(data);
285
  $('body').data('sizes',data.sizes);
286
  try {
287
  $.each(data.sizes, function(i,elem){
1
  // log
2
  function log(obj){
3
+ if (!window.console )
4
  {
5
+ var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
6
+ "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
7
 
8
+ window.console = {};
9
+ for (var i = 0; i < names.length; ++i)
10
+ //window.console[names[i]] = function() {}
11
+ window.console[names[i]] = alert
12
  }
13
  if (!console) console = window.console;
14
 
15
+ /*
16
+ * TODO: Comment this out when publishing to WORDPRESS.ORG
17
+ */
18
  //console.log(obj);
 
19
  }
20
 
21
  jQuery(document).ready(function($){
35
  }
36
  }
37
 
38
+ // Make it an option to turn on debug statements?
39
  function imgDebug(img, s){
40
+ //$('#pte-debug').html( "x1: " + s.x1 + "<br />"
41
+ // + "y1: " + s.y1 + "<br />"
42
+ // + "x2: " + s.x2 + "<br />"
43
+ // + "y2: " + s.y2 + "<br />"
44
+ // + "width: " + s.width + "<br />"
45
+ // + "height: " + s.height + "<br />");
46
  }
47
 
48
  function closeImgAreaSelect(){
171
 
172
  // Find the aspect ratio
173
  var sizes = $('body').data('sizes');
 
 
 
 
 
 
 
 
 
174
 
175
  // Get the images (the fake one & the thumbnail)
176
  //var main_img_url = $('#image-preview-'+id).attr('src');
177
  createPteDisplay();
178
  var img_preview = $('#image-preview-'+id)
179
  ias_instance = img_preview
180
+ .clone(false)
181
+ .appendTo('#pte-edit')
182
+ .css({'height': img_preview.height()})
183
+ .imgAreaSelect({ handles: true
184
+ , zIndex: 1200
185
+ , instance: true
186
+ , onSelectEnd: imgDebug
187
+ });
188
+
189
+ if (sizes[size]['crop'] != 0){
190
+ // set aspect ratio
191
+ log("Set aspect ratio: " + sizes[size]['crop']);
192
+ var ar = null;
193
+ var cd = gcd( parseInt(sizes[size]['width'])
194
+ , parseInt(sizes[size]['height']));
195
+
196
+ if (cd){
197
+ ar = parseInt(sizes[size]['width']) / cd
198
+ + ":"
199
+ + parseInt(sizes[size]['height']) / cd;
200
+ var options = ias_instance.getOptions();
201
+ options['aspectRatio'] = ar;
202
+ // YAY, IE dies here due to a bug in imgAreaSelect
203
+ // HOWEVER, it doesn't seem to matter...
204
+ try {
205
+ ias_instance.setOptions(options);
206
+ }
207
+ catch(e){
208
+ }
209
+ }
210
+ //ias_instance.update(false);
211
+ }
212
 
213
  $.get( ajaxurl
214
  , { 'action': 'pte_ajax'
261
  + "<input class='button-primary' type='submit' name='pte-edit-thumb' id='pte-submit' value='Edit'/>"
262
  + "</div");
263
 
264
+ //log("Attempts remaining: " + pte_max_attempts);
265
  var editor = $(".imgedit-settings");
266
  if (editor.size() < 1 && pte_max_attempts-- < 0){
267
+ //log("Tried too many times, stopping");
268
  return;
269
  }
270
  else if (editor.size() < 1){
271
+ //log("No editor... Try again.");
272
  window.setTimeout(getImageEditor, pte_timeout);
273
  return;
274
  }
275
 
276
+ //log("Found: " + editor.size());
277
 
278
  // Action: create image pop-up
279
  // Used when the user selects a post thumbnail to edit
293
  });
294
 
295
  // Get list of available sizes
296
+ //log("ADMINAJAX: " + ajaxurl);
297
  $.get(ajaxurl
298
  , { 'action': 'pte_ajax'
299
  , 'pte_action': 'get-alternate-sizes'}
300
  , function(data, status, xhr){
301
+ //log(data);
302
  $('body').data('sizes',data.sizes);
303
  try {
304
  $.each(data.sizes, function(i,elem){
post-thumbnail-editor.php CHANGED
@@ -1,11 +1,10 @@
1
  <?php
2
  /* Plugin name: Post Thumbnail Editor
3
- Plugin URI: http://github.com/sewpafly/post-thumbnail-editor
4
  Author: sewpafly
5
- Author URI: http://sewpafly.github.com/
6
- Version: 0.1.1
7
  Description: Individually manage your post thumbnails
8
- Max WP Version: 3.1
9
 
10
  LICENSE
11
 
@@ -32,6 +31,8 @@
32
  */
33
  define( PTE_PLUGINURL, plugins_url(basename( dirname(__FILE__))) . "/");
34
  define( PTE_PLUGINPATH, dirname(__FILE__) . "/");
 
 
35
 
36
  /*
37
  * Put Hooks and immediate hook functions in this file
@@ -41,8 +42,11 @@ define( PTE_PLUGINPATH, dirname(__FILE__) . "/");
41
  function pte_admin_media_styles(){
42
  wp_enqueue_style('fancybox',
43
  PTE_PLUGINURL . 'apps/fancybox/jquery.fancybox-1.3.4.css');
44
- wp_enqueue_style('pte',
45
- PTE_PLUGINURL . 'css/pte.css');
 
 
 
46
  }
47
 
48
  function pte_admin_media_scripts(){
@@ -51,9 +55,10 @@ function pte_admin_media_scripts(){
51
  PTE_PLUGINURL . 'apps/fancybox/jquery.fancybox-1.3.4.min.js',
52
  array('jquery')
53
  );
54
- wp_enqueue_script('pte',
55
- PTE_PLUGINURL . 'js/pte_admin_media.js',
56
- array('jquery')
 
57
  );
58
  }
59
 
@@ -66,27 +71,16 @@ function pte_ajax(){
66
  pte_get_alternate_sizes();
67
  break;
68
  case "get-image-data":
69
- if ( is_numeric( $_GET['id'] ) ){
70
- pte_get_image_data($_GET['id'], $_GET['size']);
71
- }
72
  break;
73
  case "resize-img":
74
- // Check that the parameters are digits
75
- if ( is_numeric($_GET['id']) &&
76
- is_numeric($_GET['x']) &&
77
- is_numeric($_GET['y']) &&
78
- is_numeric($_GET['w']) &&
79
- is_numeric($_GET['h'])
80
- ) {
81
- check_ajax_referer("pte-{$_GET['id']}-{$_GET['size']}");
82
- pte_resize_img($_GET['id'],
83
- $_GET['size'],
84
- $_GET['x'],
85
- $_GET['y'],
86
- $_GET['w'],
87
- $_GET['h']
88
- );
89
- }
90
  break;
91
  }
92
  die(-1);
1
  <?php
2
  /* Plugin name: Post Thumbnail Editor
3
+ Plugin URI: http://wordpress.org/extend/plugins/post-thumbnail-editor/
4
  Author: sewpafly
5
+ Author URI: http://sewpafly.github.com/post-thumbnail-editor
6
+ Version: 0.3pre
7
  Description: Individually manage your post thumbnails
 
8
 
9
  LICENSE
10
 
31
  */
32
  define( PTE_PLUGINURL, plugins_url(basename( dirname(__FILE__))) . "/");
33
  define( PTE_PLUGINPATH, dirname(__FILE__) . "/");
34
+ define( PTE_VERSION, "0.2.1");
35
+ define( PTE_POST_DATA, "pte-data");
36
 
37
  /*
38
  * Put Hooks and immediate hook functions in this file
42
  function pte_admin_media_styles(){
43
  wp_enqueue_style('fancybox',
44
  PTE_PLUGINURL . 'apps/fancybox/jquery.fancybox-1.3.4.css');
45
+ wp_enqueue_style( 'pte'
46
+ , PTE_PLUGINURL . 'css/pte.css'
47
+ , false
48
+ , PTE_VERSION
49
+ );
50
  }
51
 
52
  function pte_admin_media_scripts(){
55
  PTE_PLUGINURL . 'apps/fancybox/jquery.fancybox-1.3.4.min.js',
56
  array('jquery')
57
  );
58
+ wp_enqueue_script( 'pte'
59
+ , PTE_PLUGINURL . 'js/pte_admin_media.js'
60
+ , array('jquery')
61
+ , PTE_VERSION
62
  );
63
  }
64
 
71
  pte_get_alternate_sizes();
72
  break;
73
  case "get-image-data":
74
+ pte_get_image_data($_GET['id'], $_GET['size']);
 
 
75
  break;
76
  case "resize-img":
77
+ pte_resize_img($_GET['id'],
78
+ $_GET['size'],
79
+ $_GET['x'],
80
+ $_GET['y'],
81
+ $_GET['w'],
82
+ $_GET['h']
83
+ );
 
 
 
 
 
 
 
 
 
84
  break;
85
  }
86
  die(-1);
pte_functions.php CHANGED
@@ -1,29 +1,104 @@
1
  <?php
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  function pte_get_alternate_sizes($return_php = false){
4
  global $_wp_additional_image_sizes;
5
  $sizes = array();
6
  foreach (get_intermediate_image_sizes() as $s){
7
- //$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => FALSE );
8
- //if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) // For theme-added sizes
9
- // $sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] );
10
- //else // For default sizes set in options
11
- // $sizes[$s]['width'] = get_option( "{$s}_size_w" );
12
-
13
- //if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) // For theme-added sizes
14
- // $sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] );
15
- //else // For default sizes set in options
16
- // $sizes[$s]['height'] = get_option( "{$s}_size_h" );
17
-
18
- //if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) // For theme-added sizes
19
- // $sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] );
20
- //else // For default sizes set in options
21
- // $sizes[$s]['crop'] = get_option( "{$s}_crop" );
22
- ////$sizes[$s] = array(
23
- //// 'width' => $width,
24
- //// 'height' => $height,
25
- //// 'crop' => $crop
26
- ////);
27
  if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) // For theme-added sizes
28
  $width = intval( $_wp_additional_image_sizes[$s]['width'] );
29
  else // For default sizes set in options
@@ -45,41 +120,29 @@ function pte_get_alternate_sizes($return_php = false){
45
  );
46
  }
47
 
48
- if ($return_php) return $sizes;
49
- else if ( function_exists('json_encode') ){
50
- print(json_encode(array('sizes' => $sizes)));
51
- }
52
- else {
53
- print("{\"error\":\"json_encode not available, upgrade your php\"}");
54
- }
55
- die();
56
  }
57
 
58
- //wp_enqueue_scripts('jquery');
59
- //include(dirname(__FILE__) . "/html/editor.phtml");
60
-
61
- // TODO: Check your inputs...
62
  function pte_get_image_data($id, $size){
63
 
64
- $fullsizepath = get_attached_file( $id );
65
- $path_information = image_get_intermediate_size($id, $size);
66
-
67
- $size_information = pte_get_alternate_sizes(true);
68
- if (! array_key_exists( $size, $size_information ) ){
69
- print_r($size_information);
70
- pte_error("Invalid size: {$size}");
71
- }
72
 
73
  // Get/Create nonce
 
74
  $nonce = wp_create_nonce("pte-{$id}-{$size}");
75
 
76
  if ( $path_information &&
77
- $path_information['width'] == $size_information[$size]['width'] &&
78
- $path_information['height'] == $size_information[$size]['height'] &&
79
  @file_exists(dirname($fullsizepath)."/".$path_information['file']))
80
  {
81
  $path_information['nonce'] = $nonce;
82
- die(json_encode($path_information));
 
83
  }
84
 
85
  // We don't really care how it gets generated, just that it is...
@@ -102,7 +165,7 @@ function pte_get_image_data($id, $size){
102
  $path_information = image_get_intermediate_size($id, $size);
103
  if ($path_information){
104
  $path_information['nonce'] = $nonce;
105
- die(json_encode($path_information));
106
  }
107
  else {
108
  //print("{\"error\":\"Couldn't find metadata for image: $id\"}");
@@ -116,49 +179,65 @@ function pte_error($message){
116
 
117
  /*
118
  * See wordpress: wp-includes/media.php for image_resize
 
 
 
119
  */
120
  function pte_resize_img($id, $thumb_size, $x, $y, $w, $h, $save = true){
121
  // Check your inputs...
122
- $id = (int) $id;
123
- if ( !$post =& get_post( $id ) )
124
- pte_error("Invalid id: {$id}");
125
-
126
- $file = get_attached_file( $id );
127
- $image = wp_load_image( $file );
 
 
 
 
 
 
 
 
 
 
128
 
129
- $size_information = pte_get_alternate_sizes(true);
130
- if (! array_key_exists( $thumb_size, $size_information ) ){
131
- pte_error("Invalid size: {$thumb_size}");
 
 
 
 
 
132
  }
133
-
134
- if (! $path_information = image_get_intermediate_size($id, $thumb_size)){
135
- pte_error("Invalid image: {$id} {$thumb_size}");
 
 
 
136
  }
137
- //die(json_encode($path_information));
138
-
139
- if ( !is_resource( $image ) )
140
- pte_error("Error loading image");
141
-
142
- $size = @getimagesize( $file );
143
- if ( !$size )
144
- pte_error("Could not read image size");
145
-
146
- list($orig_w, $orig_h, $orig_type) = $size;
147
-
148
- // Error checking that the src is big enough to go into dst?
149
- if (
150
- $x < 0 ||
151
- $y < 0 ||
152
- $x + $w > $orig_w ||
153
- $y + $h > $orig_h ||
154
- $w <= 0 ||
155
- $h <= 0 ){
156
- pte_error("Invalid input parameters: {$x} {$y} {$w} {$h}");
157
  }
158
- $dst_x = 0;
159
- $dst_y = 0;
160
- $dst_w = $size_information[$thumb_size]['width'];
161
- $dst_h = $size_information[$thumb_size]['height'];
 
 
 
 
 
 
 
 
 
 
 
 
162
  $src_x = $x;
163
  $src_y = $y;
164
  $src_w = $w;
@@ -170,12 +249,16 @@ function pte_resize_img($id, $thumb_size, $x, $y, $w, $h, $save = true){
170
  // Save the conversion data so if a batch script (a la ajax-thumbnail-rebuild)
171
  // that we can find what the scale/crop area and rebuild using it.
172
  if ($save){
173
- $data = get_post_meta( $post->ID, 'pte-data', true );
174
- $data[$thumb_size] = compact('dst_x', 'dst_y', 'dst_w', 'dst_h', 'src_x', 'src_y', 'src_w', 'src_h');
175
- add_post_meta($post->ID, 'pte-data', $data, true) or update_post_meta($post->ID, 'pte-data', $data);
 
 
176
  }
177
 
178
- imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
 
 
179
 
180
  // convert from full colors to index colors, like original PNG.
181
  if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $image ) )
@@ -184,9 +267,6 @@ function pte_resize_img($id, $thumb_size, $x, $y, $w, $h, $save = true){
184
  // we don't need the original in memory anymore
185
  imagedestroy( $image );
186
 
187
- // Get the output filename
188
- $destfilename = dirname($file)."/".$path_information['file'];
189
-
190
  if ( IMAGETYPE_GIF == $orig_type ) {
191
  if ( !imagegif( $newimage, $destfilename ) )
192
  //return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
@@ -212,6 +292,15 @@ function pte_resize_img($id, $thumb_size, $x, $y, $w, $h, $save = true){
212
  $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
213
  @ chmod( $destfilename, $perms );
214
 
215
- die(json_encode(array("url" => $path_information['url'])));
 
 
 
 
 
 
 
 
 
216
  }
217
 
1
  <?php
2
 
3
+ function pte_json_encode($mixed){
4
+ if ( function_exists('json_encode') ){
5
+ die( json_encode($mixed) );
6
+ }
7
+ else {
8
+ pte_error( "json_encode not available, upgrade your php" );
9
+ }
10
+ }
11
+
12
+ /**
13
+ * Validation functions
14
+ *
15
+ * Return if the GET/POST data is valid
16
+ * pte-error/die if invalid
17
+ */
18
+ function validate_image_data_inputs($id, $size){
19
+ if ( !is_numeric( $id ) )
20
+ pte_error("Parameter 'id' is not numeric");
21
+
22
+ $fullsizepath = get_attached_file( $id );
23
+ $path_information = image_get_intermediate_size($id, $size);
24
+
25
+ $size_information = pte_get_alternate_sizes(true);
26
+ if (! array_key_exists( $size, $size_information ) ){
27
+ pte_error("Invalid size: {$size}");
28
+ }
29
+ return compact( 'fullsizepath'
30
+ , 'path_information'
31
+ , 'size_information'
32
+ );
33
+ }
34
+
35
+ function validate_resize_inputs( $id, $thumb_size, $x, $y, $w, $h ){
36
+ // Verifies that the user has proper access and intent
37
+ check_ajax_referer("pte-{$id}-{$thumb_size}");
38
+
39
+ //Check that the parameters are digits
40
+ if ( !( is_numeric($id) &&
41
+ is_numeric($x) &&
42
+ is_numeric($y) &&
43
+ is_numeric($w) &&
44
+ is_numeric($h) ) ){
45
+ pte_error("One of 'id', 'x', 'y', 'w', or 'h' is not numeric");
46
+ }
47
+
48
+ // Check the POST
49
+ $id = (int) $id;
50
+ if ( !$post =& get_post( $id ) )
51
+ pte_error("Invalid id: {$id}");
52
+
53
+ $file = get_attached_file( $id );
54
+ $image = wp_load_image( $file );
55
+
56
+ $size_information = pte_get_alternate_sizes(true);
57
+ if (! array_key_exists( $thumb_size, $size_information ) ){
58
+ pte_error("Invalid size: {$thumb_size}");
59
+ }
60
+
61
+ if (! $path_information = image_get_intermediate_size($id, $thumb_size)){
62
+ pte_error("Invalid image: {$id} {$thumb_size}");
63
+ }
64
+
65
+ if ( !is_resource( $image ) )
66
+ pte_error("Error loading image");
67
+
68
+ $size = @getimagesize( $file );
69
+ if ( !$size )
70
+ pte_error("Could not read image size");
71
+
72
+ list($orig_w, $orig_h, $orig_type) = $size;
73
+
74
+ // Error checking that the src is big enough to go into dst?
75
+ if (
76
+ $x < 0 ||
77
+ $y < 0 ||
78
+ $x + $w > $orig_w ||
79
+ $y + $h > $orig_h ||
80
+ $w <= 0 ||
81
+ $h <= 0 ){
82
+ pte_error("Invalid input parameters: {$x} {$y} {$w} {$h}");
83
+ }
84
+ $array = compact('id'
85
+ , 'post'
86
+ , 'file'
87
+ , 'image'
88
+ , 'size_information'
89
+ , 'path_information'
90
+ , 'orig_w'
91
+ , 'orig_h'
92
+ , 'orig_type'
93
+ );
94
+ return $array;
95
+ }
96
+ /** END VALIDATION **/
97
+
98
  function pte_get_alternate_sizes($return_php = false){
99
  global $_wp_additional_image_sizes;
100
  $sizes = array();
101
  foreach (get_intermediate_image_sizes() as $s){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) // For theme-added sizes
103
  $width = intval( $_wp_additional_image_sizes[$s]['width'] );
104
  else // For default sizes set in options
120
  );
121
  }
122
 
123
+ if ( $return_php ) return $sizes;
124
+ pte_json_encode( array( 'sizes' => $sizes ) );
 
 
 
 
 
 
125
  }
126
 
 
 
 
 
127
  function pte_get_image_data($id, $size){
128
 
129
+ // Validate the inputs
130
+ // Create and return:
131
+ // $fullsizepath
132
+ // $path_information
133
+ // $size_information
134
+ extract( validate_image_data_inputs( $id, $size ) );
 
 
135
 
136
  // Get/Create nonce
137
+ // - This key grants limited 24 hour access to modify the attachment $id's $size
138
  $nonce = wp_create_nonce("pte-{$id}-{$size}");
139
 
140
  if ( $path_information &&
 
 
141
  @file_exists(dirname($fullsizepath)."/".$path_information['file']))
142
  {
143
  $path_information['nonce'] = $nonce;
144
+ //$path_information['debug'] = "Finished without regenerating image";
145
+ pte_json_encode($path_information);
146
  }
147
 
148
  // We don't really care how it gets generated, just that it is...
165
  $path_information = image_get_intermediate_size($id, $size);
166
  if ($path_information){
167
  $path_information['nonce'] = $nonce;
168
+ pte_json_encode($path_information);
169
  }
170
  else {
171
  //print("{\"error\":\"Couldn't find metadata for image: $id\"}");
179
 
180
  /*
181
  * See wordpress: wp-includes/media.php for image_resize
182
+ *
183
+ * TODO: Break into smaller pieces
184
+ * TODO: Test saving POST META
185
  */
186
  function pte_resize_img($id, $thumb_size, $x, $y, $w, $h, $save = true){
187
  // Check your inputs...
188
+ // Also creates and returns:
189
+ // $post
190
+ // $file
191
+ // $image
192
+ // $size_information
193
+ // $path_information
194
+ // $orig_w
195
+ // $orig_h
196
+ // $orig_type
197
+ //extract(validate_resize_inputs( $id, $thumb_size, $x, $y, $w, $h ));
198
+ $validation = validate_resize_inputs( $id, $thumb_size, $x, $y, $w, $h );
199
+ extract( $validation );
200
+
201
+ // Set the output (destination) information
202
+ $dst_x = 0;
203
+ $dst_y = 0;
204
 
205
+ // ==============================
206
+ // Get Destination width & height
207
+ // ==============================
208
+ // When the crop isn't set the biggest dimension is accurate,
209
+ // but the other dimension is wrong
210
+ if ($size_information[$thumb_size]['crop']){
211
+ $dst_w = $size_information[$thumb_size]['width'];
212
+ $dst_h = $size_information[$thumb_size]['height'];
213
  }
214
+ // Crop isn't set so the height / width should be based on the biggest side
215
+ // Filename changes
216
+ // Update wp_attachment_metadata with the correct file/width/height
217
+ else if ($w > $h){
218
+ $dst_w = $size_information[$thumb_size]['width'];
219
+ $dst_h = round( ($dst_w/$w) * $h, 0);
220
  }
221
+ else {
222
+ $dst_h = $size_information[$thumb_size]['height'];
223
+ $dst_w = round( ($dst_h/$h) * $w, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
  }
225
+ // ==============================
226
+
227
+
228
+ // ================
229
+ // Get the filename
230
+ // ================
231
+ // See image_resize function in wp-includes/media.php to follow the same conventions
232
+ $info = pathinfo($file);
233
+ $dir = $info['dirname'];
234
+ $ext = $info['extension'];
235
+ $name = wp_basename($file, ".$ext");
236
+ $suffix = "{$dst_w}x{$dst_h}-pte";
237
+ $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
238
+ // ================
239
+
240
+ // Set the input information
241
  $src_x = $x;
242
  $src_y = $y;
243
  $src_w = $w;
249
  // Save the conversion data so if a batch script (a la ajax-thumbnail-rebuild)
250
  // that we can find what the scale/crop area and rebuild using it.
251
  if ($save){
252
+ $data = get_post_meta( $post->ID, PTE_POST_DATA, true );
253
+ $data[$thumb_size] = compact( 'dst_x', 'dst_y', 'dst_w', 'dst_h'
254
+ , 'src_x', 'src_y', 'src_w', 'src_h'
255
+ );
256
+ update_post_meta($post->ID, PTE_POST_DATA, $data);
257
  }
258
 
259
+ imagecopyresampled( $newimage, $image
260
+ , $dst_x, $dst_y, $src_x, $src_y
261
+ , $dst_w, $dst_h, $src_w, $src_h );
262
 
263
  // convert from full colors to index colors, like original PNG.
264
  if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $image ) )
267
  // we don't need the original in memory anymore
268
  imagedestroy( $image );
269
 
 
 
 
270
  if ( IMAGETYPE_GIF == $orig_type ) {
271
  if ( !imagegif( $newimage, $destfilename ) )
272
  //return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
292
  $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
293
  @ chmod( $destfilename, $perms );
294
 
295
+ // Update attachment metadata
296
+ $metadata = wp_get_attachment_metadata($id);
297
+ $metadata['sizes'][$thumb_size] = array( 'file' => "{$name}-{$suffix}.{$ext}"
298
+ , 'width' => $dst_w
299
+ , 'height' => $dst_h
300
+ );
301
+ wp_update_attachment_metadata( $id, $metadata);
302
+
303
+ $path_information = image_get_intermediate_size($id, $thumb_size);
304
+ pte_json_encode( array( "url" => $path_information['url'] ) );
305
  }
306