Version Description
Download this release
Release Info
Developer | alexdunae |
Plugin | Smush Image Compression and Optimization |
Version | 1.1 |
Comparing to | |
See all releases |
Code changes from version 1.0.2 to 1.1
- history.txt +15 -0
- readme.txt +5 -4
- screenshot-1.jpg +0 -0
- smush.php +29 -0
- 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.
|
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.
|
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’re comfortable with Smush.it’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’re comfortable with Smush.it’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.
|
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.
|
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 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
add_filter('
|
46 |
-
|
47 |
-
|
48 |
-
add_action('
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
*
|
54 |
-
*
|
55 |
-
*
|
56 |
-
*
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
$
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
$
|
77 |
-
}
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
$
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
function
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
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&attachment_ID=%d&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 |
+
}
|