Resize Image After Upload - Version 1.6

Version Description

  • [Update] Tidied up the plugin settings page.
  • [Fix] Significant number of bug fixes through extensive testing.
  • [Fix] Resizing wasn't running on square images.
  • [FIX] Media upload was failing for BMP images.
  • [Fix] Replaced use of deprecated PHP functions with correct ones.
Download this release

Release Info

Developer jepsonrae
Plugin Icon 128x128 Resize Image After Upload
Version 1.6
Comparing to
See all releases

Code changes from version 1.5.0 to 1.6

class.resize.php CHANGED
@@ -64,7 +64,7 @@ class RVJ_ImageResize {
64
  var $arrResizedDetails;
65
  var $resOriginalImage;
66
  var $resResizedImage;
67
- var $numQuality = 95;
68
  var $boolProtect = true;
69
 
70
  /*
@@ -200,7 +200,7 @@ class RVJ_ImageResize {
200
  *
201
  */
202
 
203
- function saveImage($numQuality = 95){
204
  switch($this->arrResizedDetails['mime']){
205
  case "image/jpeg":
206
  imagejpeg($this->resResizedImage, $this->strResizedImagePath, $numQuality);
64
  var $arrResizedDetails;
65
  var $resOriginalImage;
66
  var $resResizedImage;
67
+ var $numQuality = 90;
68
  var $boolProtect = true;
69
 
70
  /*
200
  *
201
  */
202
 
203
+ function saveImage($numQuality = 90){
204
  switch($this->arrResizedDetails['mime']){
205
  case "image/jpeg":
206
  imagejpeg($this->resResizedImage, $this->strResizedImagePath, $numQuality);
icon-128x128.png ADDED
Binary file
readme.txt CHANGED
@@ -1,37 +1,46 @@
1
  === Resize Image After Upload ===
2
- Contributors: jepsonrae
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3W4M254AA3KZG
4
- Tags: image, plugin, resize, upload
5
  Requires at least: 3.5
6
  Tested up to: 4.0
7
- Stable tag: 1.5.0
8
 
9
- This plugin resizes uploaded images to within a given maximum width and height after uploading, discarding the original uploaded file in the process.
10
 
11
  == Description ==
12
 
13
- This plugin resizes uploaded images to within a given maximum width and height after uploading, discarding the original uploaded file in the process. The original image is destroyed to save space, and unfortunately EXIF dataa is lost due to this process.
14
 
15
- The requirement for this plugin is due to the fact that digital cameras and mobile phones now take pictures of over 4000x3000 pixels in dimension, and can range in size from a few MB, to 20MB. Having these original images stored on the server can quickly consume up valuable disk space. This plugin will reduce the size of the uploaded image at point of upload; then either WordPress or some other resize script such as TimThumb can then further reduce the image size to suit positioning in the website theme.
16
 
17
- This plugin uses standard PHP resizing functions so resizing is not on par with what you could produce in Photoshop. However for the large majority of cases, this is not noticeable in the slightest.
18
 
19
- The plugin uses a class originally from Jacob Wyke (www.redvodkajelly.com) and is a direct update of another plugin called Resize at Upload which is no longer maintained.
 
 
20
 
21
  == Installation ==
22
 
23
- 1. Upload the plugin 'resize-image-after-upload' to the '/wp-content/plugins/' directory
24
- 2. Activate the plugin through the 'Plugins' menu in WordPress
25
- 3. Activate the resize function and set maximum width and height through the 'Settings > Resize Image Upload' menu in WordPress
26
- 4. Upload images while writing posts and pages.
27
 
28
  == Screenshots ==
29
 
30
- 1. Full preview of the settings screen
31
 
32
  == Changelog ==
33
 
34
- = 1.5.0 =
 
 
 
 
 
 
 
35
  * [Added] Ability to force re-compression even if resize is not required.
36
  * [Fix] Compression quality value was not adhered to.
37
 
@@ -41,13 +50,13 @@ The plugin uses a class originally from Jacob Wyke (www.redvodkajelly.com) and i
41
  = 1.4.1 =
42
  * [Fix] Reverting code back to how it was in v1.3.0 after previous premature deployment of v1.4.0. Please use this version.
43
 
44
- = 1.4.0 =
45
  * [Error] Code was deployed prematurely and should not have made its way to the live repository. Please do NOT use this version.
46
 
47
- = 1.3.0 =
48
  * [Update] Added ability to set the JPEG compression quality level when JPEGs are resized. Default is 90.
49
 
50
- = 1.2.0 =
51
  * [Update] Now only runs images through the resizer if required, i.e. the image is actually larger than the max height/width.
52
 
53
  = 1.1.1 =
@@ -56,11 +65,11 @@ The plugin uses a class originally from Jacob Wyke (www.redvodkajelly.com) and i
56
  * [Update] Updated the screenshot to include new maximum height field.
57
  * [Update] Increased the default maximum height and width to 1200px each.
58
 
59
- = 1.1.0 =
60
  * [Update] Added ability to set maximum height (thanks @Wonderm00n).
61
 
62
  = 1.0.1 =
63
  * [Update] Update to read me, no feature updates unfortunately.
64
 
65
- = 1.0.0 =
66
  * [Added] Initial release.
1
  === Resize Image After Upload ===
2
+ Contributors: iamphilrae
3
  Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3W4M254AA3KZG
4
+ Tags: image, processing, plugin, resize, upload, resizing, optimization, optimize, optimise, optimisation, downsize
5
  Requires at least: 3.5
6
  Tested up to: 4.0
7
+ Stable tag: 1.6
8
 
9
+ Simple plugin to automatically resize uploaded images to within specified maximum width and height. Also has option to force recompression of JPEGs.
10
 
11
  == Description ==
12
 
13
+ This plugin automatically resizes uploaded images (JPEG, GIF, and PNG) to within a given maximum width and/or height to reduce server space usage. This may be necessary due to the fact that images from digital cameras and smartphones can now be over 10MB each due to higher megapixel counts.
14
 
15
+ In addition, the plugin can force re-compression of uploaded JPEG images, regardless of whether they are resized or not; and convert uploaded GIF and PNG images into JPEG format.
16
 
17
+ Note: the resizing/recompression process will discard the original uploaded file including EXIF data.
18
 
19
+ This plugin is not intended to replace the WordPress add_image_size() function, but rather complement it. Use this plugin to ensure that no excessively large images are stored on your server, then use add_image_size() to create versions of the images suitable for positioning in your website theme.
20
+
21
+ This plugin uses standard PHP image resizing functions and will require a high amount of memory (RAM) to be allocated to PHP in your php.ini file (e.g 512MB).
22
 
23
  == Installation ==
24
 
25
+ 1. Upload the plugin 'resize-image-after-upload' to the '/wp-content/plugins/' directory.
26
+ 2. Activate the plugin through the 'Plugins' menu in WordPress.
27
+ 3. Edit the maxwidth/maxheight settings under 'Settings > Resize Image Upload'.
28
+ 4. Once active, just upload images as normal and it will just work!.
29
 
30
  == Screenshots ==
31
 
32
+ 1. Full preview of the settings screen.
33
 
34
  == Changelog ==
35
 
36
+ = 1.6 =
37
+ * [Update] Tidied up the plugin settings page.
38
+ * [Fix] Significant number of bug fixes through extensive testing.
39
+ * [Fix] Resizing wasn't running on square images.
40
+ * [FIX] Media upload was failing for BMP images.
41
+ * [Fix] Replaced use of deprecated PHP functions with correct ones.
42
+
43
+ = 1.5 =
44
  * [Added] Ability to force re-compression even if resize is not required.
45
  * [Fix] Compression quality value was not adhered to.
46
 
50
  = 1.4.1 =
51
  * [Fix] Reverting code back to how it was in v1.3.0 after previous premature deployment of v1.4.0. Please use this version.
52
 
53
+ = 1.4 =
54
  * [Error] Code was deployed prematurely and should not have made its way to the live repository. Please do NOT use this version.
55
 
56
+ = 1.3 =
57
  * [Update] Added ability to set the JPEG compression quality level when JPEGs are resized. Default is 90.
58
 
59
+ = 1.2 =
60
  * [Update] Now only runs images through the resizer if required, i.e. the image is actually larger than the max height/width.
61
 
62
  = 1.1.1 =
65
  * [Update] Updated the screenshot to include new maximum height field.
66
  * [Update] Increased the default maximum height and width to 1200px each.
67
 
68
+ = 1.1 =
69
  * [Update] Added ability to set maximum height (thanks @Wonderm00n).
70
 
71
  = 1.0.1 =
72
  * [Update] Update to read me, no feature updates unfortunately.
73
 
74
+ = 1.0 =
75
  * [Added] Initial release.
resize-image-after-upload.php CHANGED
@@ -1,25 +1,19 @@
1
  <?php
2
  /*
3
  Plugin Name: Resize Image After Upload
4
- Plugin URI: http://www.jepsonrae.com/?utm_campaign=plugins&utm_source=wp-resize-image-after-upload&utm_medium=plugin-url
5
- Description: This plugin resizes uploaded images to a given width or height (whichever is the largest) after uploading, discarding the original uploaded file in the process.
6
- Author: JEPSONRAE
7
- Version: 1.5.0
8
- Author URI: http://www.jepsonrae.com/?utm_campaign=plugins&utm_source=wp-resize-image-after-upload&utm_medium=author-url
9
-
10
-
11
-
12
- Copyright (C) 2008 A. Huizinga (original Resize at Upload plugin)
13
- Copyright (C) 2013 Jepson Rae Ltd
14
-
15
 
 
 
16
 
17
  Includes hints and code by:
18
  Huiz.net (www.huiz.net)
19
- Jacob Wyke (www.redvodkajelly.com)
20
- Paolo Tresso / Pixline (http://pixline.net)
21
-
22
-
23
 
24
  This program is free software; you can redistribute it and/or
25
  modify it under the terms of the GNU General Public License
@@ -36,7 +30,8 @@ along with this program; if not, write to the Free Software
36
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
37
  */
38
 
39
- $PLUGIN_VERSION = '1.5.0';
 
40
 
41
 
42
  // Default plugin values
@@ -48,8 +43,9 @@ if(get_option('jr_resizeupload_version') != $PLUGIN_VERSION) {
48
  add_option('jr_resizeupload_quality', '90', '', 'yes');
49
  add_option('jr_resizeupload_resize_yesno', 'yes', '','yes');
50
  add_option('jr_resizeupload_recompress_yesno', 'no', '','yes');
51
- add_option('jr_resizeupload_convertbmp_yesno', 'no', '', 'no');
52
- add_option('jr_resizeupload_convertpng_yesno', 'no', '', 'no');
 
53
  }
54
 
55
 
@@ -58,11 +54,7 @@ if(get_option('jr_resizeupload_version') != $PLUGIN_VERSION) {
58
  add_action('admin_menu', 'jr_uploadresize_options_page');
59
 
60
  // Hook the function to the upload handler
61
- if (get_option('jr_resizeupload_resize_yesno') == 'yes') {
62
- add_action('wp_handle_upload', 'jr_uploadresize_resize'); // apply our modifications
63
- }
64
-
65
-
66
 
67
 
68
 
@@ -89,71 +81,72 @@ function jr_uploadresize_options_page(){
89
  */
90
  function jr_uploadresize_options(){
91
 
92
- if (isset($_POST['jr_options_update'])) {
93
 
94
- $maxwidth = trim(mysql_real_escape_string($_POST['maxwidth']));
95
- $maxheight = trim(mysql_real_escape_string($_POST['maxheight']));
96
- $quality = trim(mysql_real_escape_string($_POST['quality']));
97
- $yesno = $_POST['yesno'];
98
- $recompress_yesno = $_POST['recompress_yesno'];
99
- $convert_png = $_POST['convertpng'];
100
- $convert_bmp = $_POST['convertbmp'];
101
 
102
- // if input is empty or not an integer, use previous setting
103
- if ($maxwidth == '' || ctype_digit(strval($maxwidth)) == FALSE) {
104
- $maxwidth = get_option('jr_resizeupload_width');
105
- }
106
 
107
- if ($maxheight == '' || ctype_digit(strval($maxheight)) == FALSE) {
108
- $maxheight = get_option('jr_resizeupload_height');
109
- }
110
 
111
- if ($quality == '' || ctype_digit(strval($quality)) == FALSE) {
112
- $quality = get_option('jr_resizeupload_quality');
113
- }
114
 
115
- if($quality<0) {
116
- $quality=0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  }
118
- else if($quality>100) {
119
- $quality=100;
120
  }
121
 
 
 
 
122
 
123
- update_option('jr_resizeupload_width',$maxwidth);
124
- update_option('jr_resizeupload_height',$maxheight);
125
- update_option('jr_resizeupload_quality',$quality);
126
 
127
- if ($yesno == 'yes') {
128
- update_option('jr_resizeupload_resize_yesno','yes');
129
- } // if
130
  else {
131
- update_option('jr_resizeupload_resize_yesno','no');
132
- } // else
133
 
134
 
135
- if ($recompress_yesno == 'yes') {
136
- update_option('jr_resizeupload_recompress_yesno','yes');
137
- } // if
138
  else {
139
- update_option('jr_resizeupload_recompress_yesno','no');
140
- } // else
141
 
142
 
143
- if ($convert_png == 'yes') {
144
- update_option('jr_resizeupload_convertpng_yesno','yes');
145
- } // if
146
  else {
147
- update_option('jr_resizeupload_convertpng_yesno','no');
148
- } // else
149
-
 
 
 
150
 
151
- if ($convert_bmp == 'yes') {
152
- update_option('jr_resizeupload_convertbmp_yesno','yes');
153
- } // if
154
  else {
155
- update_option('jr_resizeupload_convertbmp_yesno','no');
156
- } // else
157
 
158
 
159
 
@@ -163,105 +156,179 @@ function jr_uploadresize_options(){
163
 
164
 
165
  // get options and show settings form
166
- $maxwidth = get_option('jr_resizeupload_width');
167
- $maxheight = get_option('jr_resizeupload_height');
168
- $quality = intval(get_option('jr_resizeupload_quality'));
169
- $yesno = get_option('jr_resizeupload_resize_yesno');
170
- $recompress_yesno = get_option('jr_resizeupload_recompress_yesno');
171
- $convert_png = get_option('jr_resizeupload_convertpng_yesno');
172
- $convert_bmp = get_option('jr_resizeupload_convertbmp_yesno');
 
 
 
173
  ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
 
175
- <div class="wrap">
176
- <form method="post" accept-charset="utf-8">
 
 
 
 
177
 
178
- <h2>Resize Image After Upload Options</h2>
179
- <p>This plugin resizes uploaded images to given maximum width and/or height after uploading, discarding the original uploaded file in the process.
180
- You can set the max width and max height, and images (JPEG, PNG or GIF) will be resized automatically after they are uploaded.</p>
181
-
182
- <!-- <p>If 'Convert BMPs to JPEGs' is enabled, then BMP files will also be resized.</p> -->
183
 
184
- <p>Your file will be resized, there will not be a copy or backup with the original size.</p>
185
 
186
- <p>Set the option 'Resize' to no if you want to disable resizing, this way you shouldn&#8217;t need to deactivate the plugin if you don&#8217;t want to resize for a while.</p>
 
187
 
188
- <h3 style="margin-top:20px;border-top:1px solid #eee;padding-top:20px;">Settings</h3>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  <table class="form-table">
190
  <tr>
191
- <td valign="top">Re-size images:&nbsp;</td>
192
  <td valign="top">
193
  <select name="yesno" id="yesno">
194
- <option value="no" label="no" <?php echo ($yesno == 'no') ? 'selected="selected"' : ''; ?>>No</option>
195
- <option value="yes" label="yes" <?php echo ($yesno == 'yes') ? 'selected="selected"' : ''; ?>>Yes</option>
196
- </select>
197
- </td>
198
- </tr>
199
-
200
- <tr>
201
- <td valign="top">Re-compress even if no resize required:&nbsp;</td>
202
- <td valign="top">
203
- <select name="recompress_yesno" id="yesno">
204
- <option value="no" label="no" <?php echo ($recompress_yesno == 'no') ? 'selected="selected"' : ''; ?>>No</option>
205
- <option value="yes" label="yes" <?php echo ($recompress_yesno == 'yes') ? 'selected="selected"' : ''; ?>>Yes</option>
206
  </select>
207
- <br /><small>Normally only images requiring resizing will be re-compressed.</small>
208
  </td>
209
  </tr>
210
 
211
  <tr>
212
- <td valign="top">Maximum width and height (pixels):&nbsp;</td>
213
- <td valign="top">
214
- <input type="text" name="maxwidth" size="7" id="maxwidth" value="<?php echo $maxwidth; ?>" /> px-wide
215
- <br /><input type="text" name="maxheight" size="7" id="maxheight" value="<?php echo $maxheight; ?>" /> px-high
216
- <br /><small>Integer pixel value (e.g. 1200). </small>
217
- <br /><small>Set to zero (0) to prevent resizing in that dimension.</small>
218
- <br /><small>Recommended value: 1200</small>
 
 
 
 
219
  </td>
 
 
220
  </tr>
 
 
 
 
 
 
 
 
 
221
 
222
  <tr>
223
- <td valign="top">Compression quality (for JPEGs):&nbsp;</td>
224
  <td valign="top">
225
  <select id="quality" name="quality">
226
- <?php for($i=1; $i<=100; $i++) : ?>
227
- <option value="<?php echo $i; ?>" <?php if($quality == $i) : ?>selected<?php endif; ?>><?php echo $i; ?></option>
228
  <?php endfor; ?>
229
  </select>
230
- <br /><small>Integer between 0 (low quality, smallest files) and 100 (best quality, largest files)
231
- <br />Default value: 90</small>
 
232
  </td>
233
  </tr>
234
-
235
- <!--
236
  <tr>
237
- <td valign="top">Convert PNGs to JPEGs:&nbsp;</td>
238
- <td valign="top">
239
- <select id="convert-png" name="convertpng">
240
- <option value="no" <?php if($convert_png == 'no') : ?>selected<?php endif; ?>>No</option>
241
- <option value="yes" <?php if($convert_png == 'yes') : ?>selected<?php endif; ?>>Yes</option>
242
  </select>
243
- <br /><small>When a PNG is uploaded, it will automatically be converted to a JPEG
244
- <br />Selecting 'No' will <strong>not</strong> prevent PNGs from being resized</small>
245
  </td>
246
  </tr>
247
- -->
248
 
249
- <!--
 
 
 
 
 
 
 
 
 
 
 
250
  <tr>
251
- <td valign="top">Convert BMPs to JPEGs and resize:&nbsp;</td>
252
- <td valign="top">
253
- <select id="convert-bmp" name="convertbmp">
254
- <option value="no" <?php if($convert_bmp == 'no') : ?>selected<?php endif; ?>>No</option>
255
- <option value="yes" <?php if($convert_bmp == 'yes') : ?>selected<?php endif; ?>>Yes</option>
256
  </select>
257
- <br /><small>When a BMP is uploaded, it will automatically be converted to a JPEG
258
- <br />Selecting 'No' will prevent BMPs from being resized</small>
259
  </td>
260
  </tr>
261
- -->
 
 
 
 
 
 
 
 
 
 
262
  </table>
 
 
 
263
 
264
- <p class="submit" style="margin-top:20px;border-top:1px solid #eee;padding-top:20px;">
265
  <input type="hidden" id="convert-bmp" name="convertbmp" value="no" />
266
  <input type="hidden" name="action" value="update" />
267
  <input id="submit" name="jr_options_update" class="button button-primary" type="submit" value="Update Options">
@@ -274,74 +341,161 @@ function jr_uploadresize_options(){
274
 
275
 
276
 
 
 
277
  /**
278
  * This function will apply changes to the uploaded file
279
- * @param $array - contains file, url, type
280
  */
281
- function jr_uploadresize_resize($array){
282
 
283
  if(
284
- $array['type'] == 'image/jpeg' ||
285
- $array['type'] == 'image/gif' ||
286
- $array['type'] == 'image/png' ||
287
- $array['type'] == 'image/bmp'
 
288
  )
289
  {
290
 
291
  // Include the file to carry out the resizing
292
  require_once('class.resize.php');
293
 
294
- // Get resizing limits
295
- $max_width = get_option('jr_resizeupload_width');
296
- $max_height = get_option('jr_resizeupload_height');
297
 
298
- $quality = get_option('jr_resizeupload_quality');
 
 
 
 
299
 
300
- $recompress_yesno = get_option('jr_resizeupload_recompress_yesno');
301
- $recompress_yesno = ($recompress_yesno=='yes') ? true : false;
302
- $protect_image = ($recompress_yesno ? false : true); // opposite of recompress_yesno
 
 
 
 
 
 
303
 
304
- $convert_png = get_option('jr_resizeupload_convertpng_yesno');
305
- $convert_png = ($convert_png=='yes') ? true : false;
 
 
 
306
 
307
- $convert_bmp = get_option('jr_resizeupload_convertbmp_yesno');
308
- $convert_bmp = ($convert_bmp=='yes') ? true : false;
309
 
310
 
311
  // Get original image sizes
312
- $original_info = getimagesize($array['file']);
313
  $original_width = $original_info[0];
314
  $original_height = $original_info[1];
315
- $is_png = ($array['type'] == 'image/png') ? true : false;
316
- $is_bitmap = ($array['type'] == 'image/bmp') ? true : false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
 
318
-
319
 
 
320
  // Perform resizing operations if reduction is required
321
- if($original_width >= $max_width || $original_height >= $max_height) {
 
 
 
 
322
 
323
- //Resize by width
324
- if($original_width > $original_height && $max_width != 0) {
325
- $objResize = new RVJ_ImageResize($array['file'], $array['file'], 'W', $max_width, $protect_image, $quality);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
  }
327
-
328
- //Resize by height
329
- else if($max_height != 0) {
330
- $objResize = new RVJ_ImageResize($array['file'], $array['file'], 'H', $max_height, $protect_image, $quality);
331
- }
332
- }
333
 
 
 
 
 
 
 
 
 
 
 
 
334
 
 
335
  // Resizing is not required, but still need to re-compress to desired quality and filetype
336
- else if($recompress_yesno || ($is_png && $convert_png) || ($is_bitmap && $convert_bmp)) {
337
- $objResize = new RVJ_ImageResize($array['file'], $array['file'], 'P', 100, $protect_image, $quality);
 
 
 
 
 
 
 
338
  }
339
 
 
 
 
340
  else {
341
- // no resize/recompress
342
  }
 
 
343
 
344
  } // if(...)
345
 
346
- return $array;
347
- } // function jr_uploadresize_resize($array){
1
  <?php
2
  /*
3
  Plugin Name: Resize Image After Upload
4
+ Plugin URI: https://wordpress.org/plugins/resize-image-after-upload/
5
+ Description: Simple plugin to automatically resize uploaded images to within specified maximum width and height. Also has option to force recompression of JPEGs. Configuration options found under <a href="options-general.php?page=resize-after-upload">Settings > Resize Image Upload</a>
6
+ Author: iamphilrae
7
+ Version: 1.6
8
+ Author URI: http://www.philr.ae/
 
 
 
 
 
 
9
 
10
+ Copyright (C) 2014 JEPSONRAE Ltd
11
+
12
 
13
  Includes hints and code by:
14
  Huiz.net (www.huiz.net)
15
+ Jacob Wyke (www.redvodkajelly.com)
16
+ Paolo Tresso / Pixline (http://pixline.net)
 
 
17
 
18
  This program is free software; you can redistribute it and/or
19
  modify it under the terms of the GNU General Public License
30
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31
  */
32
 
33
+ $PLUGIN_VERSION = '1.6';
34
+ $DEBUG_LOGGER = false;
35
 
36
 
37
  // Default plugin values
43
  add_option('jr_resizeupload_quality', '90', '', 'yes');
44
  add_option('jr_resizeupload_resize_yesno', 'yes', '','yes');
45
  add_option('jr_resizeupload_recompress_yesno', 'no', '','yes');
46
+ add_option('jr_resizeupload_convertbmp_yesno', 'no', '', 'yes');
47
+ add_option('jr_resizeupload_convertpng_yesno', 'no', '', 'yes');
48
+ add_option('jr_resizeupload_convertgif_yesno', 'no', '', 'yes');
49
  }
50
 
51
 
54
  add_action('admin_menu', 'jr_uploadresize_options_page');
55
 
56
  // Hook the function to the upload handler
57
+ add_action('wp_handle_upload', 'jr_uploadresize_resize');
 
 
 
 
58
 
59
 
60
 
81
  */
82
  function jr_uploadresize_options(){
83
 
84
+ if(isset($_POST['jr_options_update'])) {
85
 
86
+ $resizing_enabled = trim(esc_sql($_POST['yesno']));
87
+ $force_jpeg_recompression = trim(esc_sql($_POST['recompress_yesno']));
 
 
 
 
 
88
 
89
+ $max_width = trim(esc_sql($_POST['maxwidth']));
90
+ $max_height = trim(esc_sql($_POST['maxheight']));
91
+ $compression_level = trim(esc_sql($_POST['quality']));
 
92
 
93
+ $convert_png_to_jpg = trim(esc_sql($_POST['convertpng']));
94
+ $convert_gif_to_jpg = trim(esc_sql($_POST['convertgif']));
95
+ $convert_bmp_to_jpg = trim(esc_sql($_POST['convertbmp']));
96
 
 
 
 
97
 
98
+ // If input is not an integer, use previous setting
99
+ $max_width = ($max_width == '') ? 0 : $max_width;
100
+ $max_width = (ctype_digit(strval($max_width)) == false) ? get_option('jr_resizeupload_width') : $max_width;
101
+ update_option('jr_resizeupload_width',$max_width);
102
+
103
+
104
+ $max_height = ($max_height == '') ? 0 : $max_height;
105
+ $max_height = (ctype_digit(strval($max_height)) == false) ? get_option('jr_resizeupload_height') : $max_height;
106
+ update_option('jr_resizeupload_height',$max_height);
107
+
108
+
109
+ $compression_level = ($compression_level == '') ? 1 : $compression_level;
110
+ $compression_level = (ctype_digit(strval($compression_level)) == false) ? get_option('jr_resizeupload_quality') : $compression_level;
111
+
112
+ if($compression_level <= 0) {
113
+ $compression_level = 1;
114
  }
115
+ else if($compression_level >= 100) {
116
+ $compression_level = 99;
117
  }
118
 
119
+ update_option('jr_resizeupload_quality',$compression_level);
120
+
121
+
122
 
 
 
 
123
 
124
+ if ($resizing_enabled == 'yes') {
125
+ update_option('jr_resizeupload_resize_yesno','yes'); }
 
126
  else {
127
+ update_option('jr_resizeupload_resize_yesno','no'); }
 
128
 
129
 
130
+ if ($force_jpeg_recompression == 'yes') {
131
+ update_option('jr_resizeupload_recompress_yesno','yes'); }
 
132
  else {
133
+ update_option('jr_resizeupload_recompress_yesno','no'); }
 
134
 
135
 
136
+ if ($convert_png_to_jpg == 'yes') {
137
+ update_option('jr_resizeupload_convertpng_yesno','yes'); }
 
138
  else {
139
+ update_option('jr_resizeupload_convertpng_yesno','no'); }
140
+
141
+ if ($convert_gif_to_jpg == 'yes') {
142
+ update_option('jr_resizeupload_convertgif_yesno','yes'); }
143
+ else {
144
+ update_option('jr_resizeupload_convertgif_yesno','no'); }
145
 
146
+ if ($convert_bmp_to_jpg == 'yes') {
147
+ update_option('jr_resizeupload_convertbmp_yesno','yes'); }
 
148
  else {
149
+ update_option('jr_resizeupload_convertbmp_yesno','no'); }
 
150
 
151
 
152
 
156
 
157
 
158
  // get options and show settings form
159
+ $resizing_enabled = get_option('jr_resizeupload_resize_yesno');
160
+ $force_jpeg_recompression = get_option('jr_resizeupload_recompress_yesno');
161
+ $compression_level = intval(get_option('jr_resizeupload_quality'));
162
+
163
+ $max_width = get_option('jr_resizeupload_width');
164
+ $max_height = get_option('jr_resizeupload_height');
165
+
166
+ $convert_png_to_jpg = get_option('jr_resizeupload_convertpng_yesno');
167
+ $convert_gif_to_jpg = get_option('jr_resizeupload_convertgif_yesno');
168
+ $convert_bmp_to_jpg = get_option('jr_resizeupload_convertbmp_yesno');
169
  ?>
170
+ <style type="text/css">
171
+ .resizeimage-button {
172
+ color: #FFF;
173
+ background: none repeat scroll 0% 0% #FC9A24;
174
+ border-radius: 3px;
175
+ display: inline-block;
176
+ border-bottom: 4px solid #EC8A14;
177
+ margin-right:5px;
178
+ line-height:1.05em;
179
+ text-align: center;
180
+ text-decoration: none;
181
+ padding: 9px 20px 8px;
182
+ font-size: 15px;
183
+ font-weight: bold;
184
+ text-shadow: 0 -1px 1px rgba(0,0,0,0.2);
185
+ }
186
 
187
+ .resizeimage-button:active,
188
+ .resizeimage-button:hover,
189
+ .resizeimage-button:focus {
190
+ background-color: #EC8A14;
191
+ color: #FFF;
192
+ }
193
 
194
+ .media-upload-form div.error, .wrap div.error, .wrap div.updated {
195
+ margin: 25px 0px 25px;
196
+ }
 
 
197
 
198
+ </style>
199
 
200
+ <div class="wrap">
201
+ <form method="post" accept-charset="utf-8">
202
 
203
+ <h2><img src="<?php echo plugins_url('icon-128x128.png', __FILE__ ); ?>" style="float:right; border:1px solid #ddd;margin:0 0 15px 15px;width:100px; height:100px;" />Resize Image After Upload</h2>
204
+
205
+ <div style="max-width:700px">
206
+ <p>This plugin automatically resizes uploaded images (JPEG, GIF, and PNG) to within a given maximum width and/or height to reduce server space usage. This may be necessary due to the fact that images from digital cameras and smartphones can now be over 10MB each due to higher megapixel counts.</p>
207
+
208
+ <p>In addition, the plugin can force re-compression of uploaded JPEG images, regardless of whether they are resized or not; and convert uploaded GIF and PNG images into JPEG format.</p>
209
+
210
+ <p><strong>Note:</strong> the resizing/recompression process will discard the original uploaded file including EXIF data.</p>
211
+
212
+ <p>This plugin is not intended to replace the WordPress <em>add_image_size()</em> function, but rather complement it. Use this plugin to ensure that no excessively large images are stored on your server, then use <em>add_image_size()</em> to create versions of the images suitable for positioning in your website theme.</p>
213
+
214
+ <p>This plugin uses standard PHP image resizing functions and will require a high amount of memory (RAM) to be allocated to PHP in your php.ini file (e.g 512MB).</p>
215
+
216
+ <h4 style="font-size: 15px;font-weight: bold;margin: 2em 0 0;">Like the plugin?</h4>
217
+
218
+ <p>This plugin was written and is maintained for free (as in free beer) by me, <a href="http://philr.ae" target="_blank">Phil Rae</a>. If you find it useful please consider donating some small change or bitcoins to my beer fund because beer is very seldom free. Thanks!</p>
219
+
220
+ <p style="padding-bottom:2em;" class="resizeimage-button-wrapper">
221
+
222
+ <a class="resizeimage-button" href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=3W4M254AA3KZG" target="_blank">Donate cash</a>
223
+
224
+ <a class="resizeimage-button coinbase-button" data-code="9584265cb76df0b1e99979163de143f5" data-button-style="custom_small" target="_blank" href="https://coinbase.com/checkouts/9584265cb76df0b1e99979163de143f5">Donate bitcoins</a>
225
+
226
+ </p>
227
+ </div>
228
+
229
+ <hr style="margin-top:20px; margin-bottom:0;">
230
+ <hr style="margin-top:1px; margin-bottom:40px;">
231
+
232
+ <h3>Re-sizing options</h3>
233
  <table class="form-table">
234
  <tr>
235
+ <th scope="row">Enable re-sizing</th>
236
  <td valign="top">
237
  <select name="yesno" id="yesno">
238
+ <option value="no" label="no" <?php echo ($resizing_enabled == 'no') ? 'selected="selected"' : ''; ?>>NO - do not resize images</option>
239
+ <option value="yes" label="yes" <?php echo ($resizing_enabled == 'yes') ? 'selected="selected"' : ''; ?>>YES - resize large images</option>
 
 
 
 
 
 
 
 
 
 
240
  </select>
 
241
  </td>
242
  </tr>
243
 
244
  <tr>
245
+ <th scope="row">Max image dimensions</th>
246
+
247
+ <td>
248
+ <fieldset><legend class="screen-reader-text"><span>Maximum width and height</span></legend>
249
+ <label for="maxwidth">Max width</label>
250
+ <input name="maxwidth" step="1" min="0" id="maxwidth" class="small-text" type="number" value="<?php echo $max_width; ?>">
251
+ &nbsp;&nbsp;&nbsp;<label for="maxheight">Max height</label>
252
+ <input name="maxheight" step="1" min="0" id="maxheight" class="small-text" type="number" value="<?php echo $max_height; ?>">
253
+ <p class="description">Set to zero or very high value to prevent resizing in that dimension.
254
+ <br />Recommended values: <code>1200</code></p>
255
+ </fieldset>
256
  </td>
257
+
258
+
259
  </tr>
260
+
261
+ </table>
262
+
263
+ <hr style="margin-top:20px; margin-bottom:30px;">
264
+
265
+ <h3>Compression options</h3>
266
+ <p style="max-width:700px">The following settings will only apply to uploaded JPEG images and images converted to JPEG format.</p>
267
+
268
+ <table class="form-table">
269
 
270
  <tr>
271
+ <th scope="row">JPEG compression level</th>
272
  <td valign="top">
273
  <select id="quality" name="quality">
274
+ <?php for($i=1; $i<=99; $i++) : ?>
275
+ <option value="<?php echo $i; ?>" <?php if($compression_level == $i) : ?>selected<?php endif; ?>><?php echo $i; ?></option>
276
  <?php endfor; ?>
277
  </select>
278
+ <p class="description"><code>1</code> = low quality (smallest files)
279
+ <br><code>99</code> = best quality (largest files)
280
+ <br>Recommended value: <code>90</code></p>
281
  </td>
282
  </tr>
283
+
 
284
  <tr>
285
+ <th scope="row">Force JPEG re-compression</th>
286
+ <td>
287
+ <select name="recompress_yesno" id="yesno">
288
+ <option value="no" label="no" <?php echo ($force_jpeg_recompression == 'no') ? 'selected="selected"' : ''; ?>>NO - only re-compress resized jpeg images</option>
289
+ <option value="yes" label="yes" <?php echo ($force_jpeg_recompression == 'yes') ? 'selected="selected"' : ''; ?>>YES - re-compress all uploaded jpeg images</option>
290
  </select>
 
 
291
  </td>
292
  </tr>
 
293
 
294
+ </table>
295
+
296
+ <?php /* DEFINED HERE FOR FUTURE RELEASE
297
+ <hr style="margin-top:20px; margin-bottom:20px;">
298
+
299
+ <h3>Image conversion options</h3>
300
+ <p style="max-width:700px">Photos saved as PNG and GIF images can be extremely large in file size due to their compression methods not being suited for photos. Enable these options below to automatically convert GIF and/or PNG images to JPEG.</p>
301
+
302
+ <p>When enabled, conversion will happen to all uploaded GIF/PNG images, not just ones that require resizing.</p>
303
+
304
+ <table class="form-table">
305
+
306
  <tr>
307
+ <th scope="row">Convert GIF to JPEG</th>
308
+ <td>
309
+ <select id="convert-gif" name="convertgif">
310
+ <option value="no" <?php if($convert_gif_to_jpg == 'no') : ?>selected<?php endif; ?>>NO - just resize uploaded gif images as normal</option>
311
+ <option value="yes" <?php if($convert_gif_to_jpg == 'yes') : ?>selected<?php endif; ?>>YES - convert all uploaded gif images to jpeg</option>
312
  </select>
 
 
313
  </td>
314
  </tr>
315
+
316
+ <tr>
317
+ <th scope="row">Convert PNG to JPEG</th>
318
+ <td>
319
+ <select id="convert-png" name="convertpng">
320
+ <option value="no" <?php if($convert_png_to_jpg == 'no') : ?>selected<?php endif; ?>>NO - just resize uploaded png images as normal</option>
321
+ <option value="yes" <?php if($convert_png_to_jpg == 'yes') : ?>selected<?php endif; ?>>YES - convert all uploaded png images to jpeg</option>
322
+ </select>
323
+ </td>
324
+ </tr>
325
+
326
  </table>
327
+ */ ?>
328
+
329
+ <hr style="margin-top:30px;">
330
 
331
+ <p class="submit" style="margin-top:10px;border-top:1px solid #eee;padding-top:20px;">
332
  <input type="hidden" id="convert-bmp" name="convertbmp" value="no" />
333
  <input type="hidden" name="action" value="update" />
334
  <input id="submit" name="jr_options_update" class="button button-primary" type="submit" value="Update Options">
341
 
342
 
343
 
344
+
345
+
346
  /**
347
  * This function will apply changes to the uploaded file
348
+ * @param $image_data - contains file, url, type
349
  */
350
+ function jr_uploadresize_resize($image_data){
351
 
352
  if(
353
+ $image_data['type'] == 'image/jpeg' ||
354
+ $image_data['type'] == 'image/jpg' ||
355
+ $image_data['type'] == 'image/gif' ||
356
+ $image_data['type'] == 'image/png' ||
357
+ $image_data['type'] == 'image/bmp'
358
  )
359
  {
360
 
361
  // Include the file to carry out the resizing
362
  require_once('class.resize.php');
363
 
 
 
 
364
 
365
+ $resizing_enabled = get_option('jr_resizeupload_resize_yesno');
366
+ $resizing_enabled = ($resizing_enabled=='yes') ? true : false;
367
+
368
+ $force_jpeg_recompression = get_option('jr_resizeupload_recompress_yesno');
369
+ $force_jpeg_recompression = ($force_jpeg_recompression=='yes') ? true : false;
370
 
371
+ $compression_level = get_option('jr_resizeupload_quality');
372
+
373
+
374
+ $max_width = get_option('jr_resizeupload_width')==0
375
+ ? 9999999999999 : get_option('jr_resizeupload_width');
376
+
377
+ $max_height = get_option('jr_resizeupload_height')==0
378
+ ? 9999999999999 : get_option('jr_resizeupload_height');
379
+
380
 
381
+ $convert_png_to_jpg = get_option('jr_resizeupload_convertpng_yesno');
382
+ $convert_png_to_jpg = ($convert_png_to_jpg=='yes') ? true : false;
383
+
384
+ $convert_gif_to_jpg = get_option('jr_resizeupload_convertgif_yesno');
385
+ $convert_gif_to_jpg = ($convert_gif_to_jpg=='yes') ? true : false;
386
 
387
+ $convert_bmp_to_jpg = get_option('jr_resizeupload_convertbmp_yesno');
388
+ $convert_bmp_to_jpg = ($convert_bmp_to_jpg=='yes') ? true : false;
389
 
390
 
391
  // Get original image sizes
392
+ $original_info = getimagesize($image_data['file']);
393
  $original_width = $original_info[0];
394
  $original_height = $original_info[1];
395
+
396
+ $original_is_jpg = false;
397
+ $original_is_png = false;
398
+ $original_is_gif = false;
399
+ $original_is_bmp = false;
400
+
401
+ switch($image_data['type']) {
402
+
403
+ case 'image/jpg' :
404
+ case 'image/jpeg' : $original_is_jpg = true; break;
405
+
406
+ case 'image/png' : $original_is_png = true; break;
407
+
408
+ case 'image/gif' : $original_is_gif = true; break;
409
+
410
+ case 'image/bmp' : $original_is_bmp = true; break;
411
+
412
+ }
413
+
414
+
415
+ if($DEBUG_LOGGER) error_log('LOG: '.print_r('Upload-start', true));
416
 
 
417
 
418
+ //
419
  // Perform resizing operations if reduction is required
420
+ if($resizing_enabled
421
+ && ($original_width > $max_width || $original_height > $max_height)
422
+ && !$original_is_bmp) { // can't currently resize bitmaps
423
+
424
+ $protext_image = true;
425
 
426
+ if($DEBUG_LOGGER) error_log('LOG: '.print_r('--resizing', true));
427
+
428
+ // Resize square images
429
+ if($original_width == $original_height) {
430
+
431
+ if($DEBUG_LOGGER) error_log('LOG: '.print_r('----square -by-width', true));
432
+
433
+ $resize_direction = 'W';
434
+ $resize_max = $max_width;
435
+
436
+
437
+ if($max_height < $max_width) {
438
+ $resize_direction = 'H';
439
+ $resize_max = $max_height;
440
+
441
+ if($DEBUG_LOGGER) error_log('LOG: '.print_r('----square -by-height', true));
442
+ }
443
+
444
+ $objResize = new RVJ_ImageResize(
445
+ $image_data['file'], $image_data['file'],
446
+ $resize_direction, $resize_max,
447
+ $protect_image, $compression_level
448
+ );
449
+ }
450
+
451
+
452
+ // Resize by width1
453
+ else if($original_width > $original_height
454
+ && $original_height <= $max_height) {
455
+
456
+ if($DEBUG_LOGGER) error_log('LOG: '.print_r('--by-width1', true));
457
+ $objResize = new RVJ_ImageResize($image_data['file'], $image_data['file'], 'W', $max_width, $protect_image, $compression_level);
458
  }
459
+
460
+ // Resize by width2
461
+ else if($original_height > $original_width
462
+ && $original_height <= $max_height) {
 
 
463
 
464
+ if($DEBUG_LOGGER) error_log('LOG: '.print_r('--by-width2', true));
465
+ $objResize = new RVJ_ImageResize($image_data['file'], $image_data['file'], 'W', $max_width, $protect_image, $compression_level);
466
+ }
467
+
468
+ // Resize by height
469
+ else {
470
+ if($DEBUG_LOGGER) error_log('LOG: '.print_r('--by-height', true));
471
+ $objResize = new RVJ_ImageResize($image_data['file'], $image_data['file'], 'H', $max_height, $protect_image, $compression_level);
472
+ }
473
+ }
474
+
475
 
476
+ //
477
  // Resizing is not required, but still need to re-compress to desired quality and filetype
478
+ else if(($original_is_jpg && $force_jpeg_recompression)
479
+ || ($original_is_png && $convert_png_to_jpg)
480
+ || ($original_is_gif && $convert_gif_to_jpg)
481
+ || ($original_is_bmp && $convert_bmp_to_jpg)) {
482
+
483
+ if($DEBUG_LOGGER) error_log('LOG: '.print_r('--recompression/conversion', true));
484
+
485
+ $protext_image = false;
486
+ $objResize = new RVJ_ImageResize($image_data['file'], $image_data['file'], 'P', 100, $protect_image, $compression_level);
487
  }
488
 
489
+
490
+ //
491
+ // No resizing, recompression, or conversion required
492
  else {
493
+ if($DEBUG_LOGGER) error_log('LOG: '.print_r('--no-action-required', true));
494
  }
495
+
496
+ if($DEBUG_LOGGER) error_log('LOG: '.print_r('--end', true));
497
 
498
  } // if(...)
499
 
500
+ return $image_data;
501
+ } // function jr_uploadresize_resize($image_data){
screenshot-1.jpg CHANGED
Binary file