Smush Image Compression and Optimization - Version 1.1

Version Description

Download this release

Release Info

Developer alexdunae
Plugin Icon 128x128 Smush Image Compression and Optimization
Version 1.1
Comparing to
See all releases

Code changes from version 1.0.2 to 1.1

Files changed (5) hide show
  1. history.txt +15 -0
  2. readme.txt +5 -4
  3. screenshot-1.jpg +0 -0
  4. smush.php +29 -0
  5. wp-smushit.php +244 -218
history.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === WP Smush.it Release History ===
2
+
3
+ == 1.1 ==
4
+ * improved handling of errors from Smush.it
5
+ * added ability to manually smush images from media library
6
+ * fixed inconsistent path handling from WP 2.5 -> WP 2.7
7
+
8
+ == 1.0.2 ==
9
+ * added 'Not processed' status message when browsing media library
10
+
11
+ == 1.0.1 ==
12
+ * added i10n functions
13
+
14
+ == 1.0 ==
15
+ * first edition
readme.txt CHANGED
@@ -1,13 +1,13 @@
1
  === WP Smush.it ===
2
  Plugin Name: WP Smush.it
3
- Version: 1.0.2
4
  Author: Dialect
5
  Author URI: http://dialect.ca/?wp_smush_it
6
  Contributors: alexdunae
7
  Tags: images, image, attachments, attachment
8
  Requires at least: 2.5
9
  Tested up to: 2.7
10
- Stable tag: 1.0.2
11
 
12
  Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
13
 
@@ -27,11 +27,12 @@ Every image you add to a page or post will be automatically run through Smush.it
27
 
28
  *N. B. In some cases GIFs should be replaced with PNG files. You can control this behaviour on the `Options` page. It is off by default.*
29
 
 
 
 
30
  = Privacy =
31
  Be sure you&rsquo;re comfortable with Smush.it&rsquo;s privacy policy (found on their <a href="http://smush.it/faq.php">FAQ</a>).
32
 
33
-
34
-
35
  == Screenshots ==
36
 
37
  1. See the savings from Smush.it in the Media Library.
1
  === WP Smush.it ===
2
  Plugin Name: WP Smush.it
3
+ Version: 1.1
4
  Author: Dialect
5
  Author URI: http://dialect.ca/?wp_smush_it
6
  Contributors: alexdunae
7
  Tags: images, image, attachments, attachment
8
  Requires at least: 2.5
9
  Tested up to: 2.7
10
+ Stable tag: 1.1
11
 
12
  Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
13
 
27
 
28
  *N. B. In some cases GIFs should be replaced with PNG files. You can control this behaviour on the `Options` page. It is off by default.*
29
 
30
+ = Existing images =
31
+ You can also run your existing images through Smush.it via the WordPress `Media Library`. Click on the `Smush.it now!` link for any image you'd like to smush.
32
+
33
  = Privacy =
34
  Be sure you&rsquo;re comfortable with Smush.it&rsquo;s privacy policy (found on their <a href="http://smush.it/faq.php">FAQ</a>).
35
 
 
 
36
  == Screenshots ==
37
 
38
  1. See the savings from Smush.it in the Media Library.
screenshot-1.jpg CHANGED
Binary file
smush.php ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Admin page for manually smushing an attachment.
4
+ *
5
+ * Expects an `attachment_ID` in the query string.
6
+ *
7
+ * @version 1.1
8
+ * @package WP_SmushIt
9
+ */
10
+
11
+ if ( !isset($_GET['attachment_ID']) )
12
+ wp_die('Must provide attachment ID');
13
+
14
+ $attachment_ID = intval($_GET['attachment_ID']);
15
+
16
+
17
+ $original_meta = wp_get_attachment_metadata( $attachment_ID );
18
+
19
+
20
+ $new_meta = wp_smushit_resize_from_meta_data( $original_meta );
21
+
22
+ wp_update_attachment_metadata( $attachment_ID, $new_meta );
23
+
24
+ $sendback = wp_get_referer();
25
+ $sendback = preg_replace('|[^a-z0-9-~+_.?#=&;,/:]|i', '', $sendback);
26
+ //$sendback .= '#post-' . $attachment_ID;
27
+ wp_redirect($sendback);
28
+
29
+ exit(0);
wp-smushit.php CHANGED
@@ -1,218 +1,244 @@
1
- <?php
2
- /**
3
- * Integrate the Smush.it API into WordPress.
4
- * @version 1.0.2
5
- * @package WP_SmushIt
6
- */
7
- /*
8
- Plugin Name: WP Smush.it
9
- Plugin URI: http://dialect.ca/code/wp-smushit/
10
- Description: Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
11
- Author: Dialect
12
- Version: 1.0.2
13
- Author URI: http://dialect.ca/?wp_smush_it
14
- */
15
-
16
- require_once('JSON/JSON.php');
17
-
18
-
19
- /**
20
- * Constants
21
- */
22
-
23
- define('SMUSHIT_REQ_URL', 'http://smush.it/ws.php?img=%s');
24
-
25
- define('SMUSHIT_BASE_URL', 'http://smush.it/');
26
-
27
- define('WP_SMUSHIT_DOMAIN', 'wp_smushit');
28
-
29
- define('WP_SMUSHIT_GIF_TO_PNG', intval(get_option('wp_smushit_gif_to_png')));
30
-
31
- if ( !defined('WP_CONTENT_URL') )
32
- define('WP_CONTENT_URL', get_option('url') . '/wp-content');
33
-
34
- if ( !defined('WP_CONTENT_DIR') )
35
- define('WP_CONTENT_DIR', ABSPATH . 'wp-content' );
36
-
37
- /**
38
- * Hooks
39
- */
40
-
41
- register_activation_hook(__FILE__,'wp_smushit_install');
42
-
43
- add_filter('wp_generate_attachment_metadata', 'wp_smushit_resize_from_meta_data');
44
-
45
- add_filter('manage_media_columns', 'wp_smushit_columns');
46
- add_action('manage_media_custom_column', 'wp_smushit_custom_column', 10, 2);
47
- add_action('admin_menu', 'wp_smushit_add_pages');
48
- add_action('admin_init', 'wp_smushit_init');
49
-
50
- /**
51
- * Process an image with Smush.it.
52
- *
53
- * Returns an array of the $file $results.
54
- *
55
- * @param string $file Full path to the image file
56
- * @returns array
57
- */
58
- function wp_smushit($file) {
59
- // dont't run on localhost
60
- if( $_SERVER['SERVER_ADDR'] == '127.0.0.1' )
61
- return array($file, __('Not processed (local file)', WP_SMUSHIT_DOMAIN));
62
-
63
- $url = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $file );
64
- $req = sprintf( SMUSHIT_REQ_URL, urlencode( $url ) );
65
-
66
- $fh = @fopen( $req, 'r' ); // post to Smush.it
67
-
68
- if ( !$fh )
69
- return array($file, __('Error posting to Smush.it', WP_SMUSHIT_DOMAIN));
70
-
71
- $data = stream_get_contents( $fh );
72
- fclose( $fh );
73
-
74
- // read the JSON response
75
- if ( function_exists('json_decode') ) {
76
- $data = json_decode( $data );
77
- } else {
78
- $json = new Services_JSON();
79
- $data = $json->decode($data);
80
- }
81
-
82
- if ( intval($data->dest_size) == -1 )
83
- return array($file, __('No savings', WP_SMUSHIT_DOMAIN));
84
-
85
- if ( !$data->dest )
86
- return array($file, __('Error: ', WP_SMUSHIT_DOMAIN) . $data->error);
87
-
88
- // download the processed image to a temp file
89
- $processed_url = SMUSHIT_BASE_URL . $data->dest;
90
-
91
- $temp_file = tempnam( WP_CONTENT_DIR . '/uploads', '___' );
92
-
93
- if( !@copy( $processed_url, $temp_file ) )
94
- return false;
95
-
96
- chmod( $temp_file, 0644 );
97
-
98
- // check if Smush.it converted a GIF to a PNG
99
- if( WP_SMUSHIT_GIF_TO_PNG == 1 && wp_smushit_did_gif_to_png($file, $data->dest) ) {
100
- $file = preg_replace('/.gif$/i', '.png', $file);
101
-
102
- if ( has_filter('wp_update_attachment_metadata', 'wp_smushit_update_attachment') === false )
103
- add_filter('wp_update_attachment_metadata', 'wp_smushit_update_attachment', 10, 2);
104
- }
105
-
106
- @rename( $temp_file, $file );
107
-
108
- $results_msg = sprintf(__("Reduced by %01.1f%%", WP_SMUSHIT_DOMAIN), $data->percent);
109
-
110
-
111
-
112
- return array($file, $results_msg);
113
- }
114
-
115
-
116
- /**
117
- * Update the attachment's meta data after being smushed.
118
- *
119
- * This is only needed when GIFs become PNGs so we add the filter near
120
- * the end of `wp_smushit()`. It's used by the `wp_update_attachment_metadata`
121
- * hook, which is called after the `wp_generate_attachment_metadata` on upload.
122
- */
123
- function wp_smushit_update_attachment($data, $ID) {
124
- $orig_file = get_attached_file( $ID );
125
-
126
- if( wp_smushit_did_gif_to_png($orig_file, $data['file']) ) {
127
- update_attached_file( $ID, $data['file'] );
128
-
129
- // get_media_item() uses the GUID for a display title so we should
130
- // update the GUID here
131
- $post = get_post( $ID );
132
- $guid = preg_replace('/.gif$/i', '.png', $post->guid);
133
-
134
- wp_update_post( array('ID' => $ID,
135
- 'post_mime_type' => 'image/png',
136
- 'guid' => $guid) );
137
- }
138
-
139
- return $data;
140
- }
141
-
142
-
143
- /**
144
- * Read the image paths from an attachment's meta data and process each image
145
- * with wp_smushit().
146
- *
147
- * This method also adds a `wp_smushit` meta key for use in the media library.
148
- *
149
- * Called after `wp_generate_attachment_metadata` is completed.
150
- */
151
- function wp_smushit_resize_from_meta_data($meta) {
152
- list($meta['file'], $meta['wp_smushit']) = wp_smushit($meta['file']);
153
-
154
- if ( !isset($meta['sizes']) )
155
- return $meta;
156
-
157
- $base_dir = dirname($meta['file']) . '/';
158
-
159
- foreach($meta['sizes'] as $size => $data) {
160
- list($smushed_file, $results) = wp_smushit($base_dir . $data['file']);
161
- $smushed_file = str_replace($base_dir, '', $smushed_file);
162
- $meta['sizes'][$size]['file'] = $smushed_file;
163
- $meta['sizes'][$size]['wp_smushit'] = $results;
164
- }
165
-
166
- return $meta;
167
- }
168
-
169
-
170
- /**
171
- * Print column header for Smush.it results in the media library using
172
- * the `manage_media_columns` hook.
173
- */
174
- function wp_smushit_columns($defaults) {
175
- $defaults['smushit'] = 'Smush.it';
176
- return $defaults;
177
- }
178
-
179
- /**
180
- * Print column data for Smush.it results in the media library using
181
- * the `manage_media_custom_column` hook.
182
- */
183
- function wp_smushit_custom_column($column_name, $id) {
184
- if( $column_name == 'smushit' ) {
185
- $data = wp_get_attachment_metadata($id);
186
- if ( isset($data['wp_smushit']) && !empty($data['wp_smushit']) )
187
- print $data['wp_smushit'];
188
- else
189
- print __('Not processed', WP_SMUSHIT_DOMAIN);
190
- }
191
- }
192
-
193
- /**
194
- * Compare file names to see if the extension changed from `.gif` to `.png`.
195
- *
196
- * @returns bool
197
- */
198
- function wp_smushit_did_gif_to_png($orig, $new) {
199
- return (stripos(strrev($new), 'gnp.') === 0 &&
200
- stripos(strrev($orig), 'fig.') === 0 );
201
-
202
- }
203
-
204
- function wp_smushit_install() {
205
- add_option('wp_smushit_gif_to_png', 0);
206
- }
207
-
208
- function wp_smushit_init() {
209
- load_plugin_textdomain(WP_SMUSHIT_DOMAIN);
210
- }
211
-
212
- function wp_smushit_add_pages() {
213
- add_options_page(__('WP Smush.it Options', WP_SMUSHIT_OPTIONS), 'WP Smush.it', 8, dirname(__FILE__) . '/options.php');
214
- }
215
-
216
- function wp_smushit_options() {
217
- include_once 'options.php';
218
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Integrate the Smush.it API into WordPress.
4
+ * @version 1.1
5
+ * @package WP_SmushIt
6
+ */
7
+ /*
8
+ Plugin Name: WP Smush.it
9
+ Plugin URI: http://dialect.ca/code/wp-smushit/
10
+ Description: Reduce image file sizes and improve performance using the <a href="http://smush.it/">Smush.it</a> API within WordPress.
11
+ Author: Dialect
12
+ Version: 1.1
13
+ Author URI: http://dialect.ca/?wp_smush_it
14
+ */
15
+
16
+ require_once('JSON/JSON.php');
17
+
18
+
19
+ /**
20
+ * Constants
21
+ */
22
+
23
+ define('SMUSHIT_REQ_URL', 'http://smush.it/ws.php?img=%s');
24
+
25
+ define('SMUSHIT_BASE_URL', 'http://smush.it/');
26
+
27
+ define('WP_SMUSHIT_DOMAIN', 'wp_smushit');
28
+
29
+ define('WP_SMUSHIT_GIF_TO_PNG', intval(get_option('wp_smushit_gif_to_png')));
30
+
31
+ define('WP_SMUSHIT_PLUGIN_DIR', dirname(plugin_basename(__FILE__)));
32
+
33
+ if ( !defined('WP_CONTENT_URL') )
34
+ define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content');
35
+
36
+ if ( !defined('WP_CONTENT_DIR') )
37
+ define('WP_CONTENT_DIR', ABSPATH . 'wp-content' );
38
+
39
+ /**
40
+ * Hooks
41
+ */
42
+
43
+ register_activation_hook(__FILE__,'wp_smushit_install');
44
+
45
+ add_filter('wp_generate_attachment_metadata', 'wp_smushit_resize_from_meta_data');
46
+
47
+ add_filter('manage_media_columns', 'wp_smushit_columns');
48
+ add_action('manage_media_custom_column', 'wp_smushit_custom_column', 10, 2);
49
+ add_action('admin_menu', 'wp_smushit_add_pages');
50
+ add_action('admin_init', 'wp_smushit_init');
51
+
52
+ /**
53
+ * Process an image with Smush.it.
54
+ *
55
+ * Returns an array of the $file $results.
56
+ *
57
+ * @param string $file Full path to the image file
58
+ * @returns array
59
+ */
60
+ function wp_smushit($file) {
61
+ // dont't run on localhost
62
+ if( $_SERVER['SERVER_ADDR'] == '127.0.0.1' )
63
+ return array($file, __('Not processed (local file)', WP_SMUSHIT_DOMAIN));
64
+
65
+
66
+ $file_path = $file;
67
+ $file_url = '';
68
+
69
+ if ( 0 === strpos($file, WP_CONTENT_DIR) ) {
70
+ // WordPress < 2.6.2: $file is already an absolute path
71
+ $file_url = str_replace( WP_CONTENT_DIR, WP_CONTENT_URL, $file );
72
+ } else {
73
+ // WordPress >= 2.6.2: determine the absolute $file_path and $file_url
74
+ $uploads = wp_upload_dir();
75
+ $file_path = trailingslashit( $uploads['basedir'] ) . $file;
76
+ $file_url = trailingslashit( $uploads['baseurl'] ) . $file;
77
+ }
78
+
79
+ $req = sprintf( SMUSHIT_REQ_URL, urlencode( $file_url ) );
80
+
81
+ $fh = @fopen( $req, 'r' ); // post to Smush.it
82
+
83
+ if ( !$fh )
84
+ return array($file, __('Error posting to Smush.it', WP_SMUSHIT_DOMAIN));
85
+
86
+ $data = stream_get_contents( $fh );
87
+ fclose( $fh );
88
+
89
+ // make sure the response looks like JSON -- added 2008-12-19 when
90
+ // Smush.it was returning PHP warnings before the JSON
91
+ if ( strpos( trim($data), '{' ) != 0 )
92
+ return array($file, __('Bad response from Smush.it', WP_SMUSHIT_DOMAIN));
93
+
94
+ // read the JSON response
95
+ if ( function_exists('json_decode') ) {
96
+ $data = json_decode( $data );
97
+ } else {
98
+ $json = new Services_JSON();
99
+ $data = $json->decode($data);
100
+ }
101
+
102
+ if ( intval($data->dest_size) == -1 )
103
+ return array($file, __('No savings', WP_SMUSHIT_DOMAIN));
104
+
105
+ if ( !$data->dest ) {
106
+ $err = ($data->error ? $data->error : 'Unknown error');
107
+ return array($file, __($err, WP_SMUSHIT_DOMAIN) );
108
+ }
109
+
110
+ // download the processed image to a temp file
111
+ $processed_url = SMUSHIT_BASE_URL . $data->dest;
112
+
113
+ $temp_file = tempnam( WP_CONTENT_DIR, '___' );
114
+
115
+ if( !@copy( $processed_url, $temp_file ) )
116
+ return false;
117
+
118
+ chmod( $temp_file, 0644 );
119
+
120
+ // check if Smush.it converted a GIF to a PNG
121
+ if( WP_SMUSHIT_GIF_TO_PNG == 1 && wp_smushit_did_gif_to_png($file, $data->dest) ) {
122
+ $file = preg_replace('/.gif$/i', '.png', $file);
123
+ $file_path = preg_replace('/.gif$/i', '.png', $file_path);
124
+
125
+ if ( has_filter('wp_update_attachment_metadata', 'wp_smushit_update_attachment') === false )
126
+ add_filter('wp_update_attachment_metadata', 'wp_smushit_update_attachment', 10, 2);
127
+ }
128
+
129
+ @rename( $temp_file, $file_path );
130
+
131
+ $results_msg = sprintf(__("Reduced by %01.1f%%", WP_SMUSHIT_DOMAIN), $data->percent);
132
+
133
+ return array($file, $results_msg);
134
+ }
135
+
136
+
137
+ /**
138
+ * Update the attachment's meta data after being smushed.
139
+ *
140
+ * This is only needed when GIFs become PNGs so we add the filter near
141
+ * the end of `wp_smushit()`. It's used by the `wp_update_attachment_metadata`
142
+ * hook, which is called after the `wp_generate_attachment_metadata` on upload.
143
+ */
144
+ function wp_smushit_update_attachment($data, $ID) {
145
+ $orig_file = get_attached_file( $ID );
146
+
147
+ if( wp_smushit_did_gif_to_png($orig_file, $data['file']) ) {
148
+ update_attached_file( $ID, $data['file'] );
149
+
150
+ // get_media_item() uses the GUID for a display title so we should
151
+ // update the GUID here
152
+ $post = get_post( $ID );
153
+ $guid = preg_replace('/.gif$/i', '.png', $post->guid);
154
+
155
+ wp_update_post( array('ID' => $ID,
156
+ 'post_mime_type' => 'image/png',
157
+ 'guid' => $guid) );
158
+ }
159
+
160
+ return $data;
161
+ }
162
+
163
+
164
+ /**
165
+ * Read the image paths from an attachment's meta data and process each image
166
+ * with wp_smushit().
167
+ *
168
+ * This method also adds a `wp_smushit` meta key for use in the media library.
169
+ *
170
+ * Called after `wp_generate_attachment_metadata` is completed.
171
+ */
172
+ function wp_smushit_resize_from_meta_data($meta) {
173
+ list($meta['file'], $meta['wp_smushit']) = wp_smushit($meta['file']);
174
+
175
+ if ( !isset($meta['sizes']) )
176
+ return $meta;
177
+
178
+ $base_dir = dirname($meta['file']) . '/';
179
+
180
+ foreach($meta['sizes'] as $size => $data) {
181
+ list($smushed_file, $results) = wp_smushit($base_dir . $data['file']);
182
+ $smushed_file = str_replace($base_dir, '', $smushed_file);
183
+ $meta['sizes'][$size]['file'] = $smushed_file;
184
+ $meta['sizes'][$size]['wp_smushit'] = $results;
185
+ }
186
+
187
+ return $meta;
188
+ }
189
+
190
+
191
+ /**
192
+ * Print column header for Smush.it results in the media library using
193
+ * the `manage_media_columns` hook.
194
+ */
195
+ function wp_smushit_columns($defaults) {
196
+ $defaults['smushit'] = 'Smush.it';
197
+ return $defaults;
198
+ }
199
+
200
+ /**
201
+ * Print column data for Smush.it results in the media library using
202
+ * the `manage_media_custom_column` hook.
203
+ */
204
+ function wp_smushit_custom_column($column_name, $id) {
205
+ if( $column_name == 'smushit' ) {
206
+ $data = wp_get_attachment_metadata($id);
207
+ if ( isset($data['wp_smushit']) && !empty($data['wp_smushit']) )
208
+ print $data['wp_smushit'];
209
+ else
210
+ print __('Not processed', WP_SMUSHIT_DOMAIN);
211
+
212
+ printf("<br><a href=\"admin.php?page=%s/smush.php&amp;attachment_ID=%d&amp;noheader\">%s</a>",
213
+ WP_SMUSHIT_PLUGIN_DIR,
214
+ $id,
215
+ __('Smush.it now!', WP_SMUSHIT_DOMAIN));
216
+ }
217
+ }
218
+
219
+ /**
220
+ * Compare file names to see if the extension changed from `.gif` to `.png`.
221
+ *
222
+ * @returns bool
223
+ */
224
+ function wp_smushit_did_gif_to_png($orig, $new) {
225
+ return (stripos(strrev($new), 'gnp.') === 0 &&
226
+ stripos(strrev($orig), 'fig.') === 0 );
227
+
228
+ }
229
+
230
+ function wp_smushit_install() {
231
+ add_option('wp_smushit_gif_to_png', 0);
232
+ }
233
+
234
+ function wp_smushit_init() {
235
+ load_plugin_textdomain(WP_SMUSHIT_DOMAIN);
236
+ }
237
+
238
+ function wp_smushit_add_pages() {
239
+ add_options_page(__('WP Smush.it Options', WP_SMUSHIT_OPTIONS), 'WP Smush.it', 8, dirname(__FILE__) . '/options.php');
240
+ }
241
+
242
+ function wp_smushit_options() {
243
+ include_once 'options.php';
244
+ }