Resize Image After Upload - Version 1.7

Version Description

This is a major under-the-hood release to change core workings of the plugin. The plugin still functions as normal, but the way in which it resizes images has now changed to use standard WordPress libraries. This means that should your server have better image processing libraries than the GD default (e.g. ImageMagick), then the resizing method should make use of them. This should improve the output of your resized images!

  • [Update] Plugin completely re-engineered to use WP_Image_Editor when resizing images.
Download this release

Release Info

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

Code changes from version 1.6.2 to 1.7

Files changed (3) hide show
  1. class.resize.php +0 -382
  2. readme.txt +8 -3
  3. resize-image-after-upload.php +83 -130
class.resize.php DELETED
@@ -1,382 +0,0 @@
1
- <?php
2
- /*
3
- * PHP Image Resize Class
4
- *
5
- * Class to deal with resizing images using PHP.
6
- * Will resize any JPG, GIF or PNG file.
7
- *
8
- * Written By Jacob Wyke - jacob@redvodkajelly.com - www.redvodkajelly.com
9
- * Edited by A. Huizinga 2009, 22 Jan: changed used of function imagecopyresized into imagecopyresampled
10
- *
11
- * LICENSE
12
- * Feel free to use this as you wish, just give me credit where credits due and drop me an email telling me what your using it for so I can check out all the cool ways its been used.
13
- *
14
- * USAGE
15
- * To use this class simply call it with the following details:
16
- *
17
- * Path to original image,
18
- * Path to save new image,
19
- * Resize type,
20
- * Resize Data
21
- *
22
- * The resize type can be one of four:
23
- *
24
- * W = Width
25
- * H = Height
26
- * P = Percentage
27
- * C = Custom
28
- *
29
- * All of these take integers except Custom that takes an array of two integers - for width and height.
30
- *
31
- * $objResize = new RVJ_ImageResize("myImage.png", "myThumb.png", "W", "400");
32
- * $objResize = new RVJ_ImageResize("myImage.jpg", "myThumb.jpg", "H", "150");
33
- * $objResize = new RVJ_ImageResize("myImage.gif", "myThumb.gif", "P", "50");
34
- * $objResize = new RVJ_ImageResize("myImage.png", "myThumb.png", "C", array("400", "300"));
35
- *
36
- * When resizing by width, height and percentage, the image will keep its original ratio. Custom will simply resizes the image to whatever values you want - without keeping the original ratio.
37
- *
38
- * The class can handle jpg, png and gif images.
39
- *
40
- * The class will always save the image that it resizes, however you can also have it display the image:
41
- *
42
- * $objResize->showImage($resize->im2);
43
- *
44
- * The class holds the original image in the variable "im" and the new image in "im2". Therefore the code above will show the newly created image.
45
- *
46
- * You can get information about the image by doing the following:
47
- *
48
- * print_r($objResize->findResourceDetails($objResize->resOriginalImage));
49
- * print_r($objResize->findResourceDetails($objResize->resResizedImage));
50
- *
51
- * This will be useful if you wish to retrieve any details about the images.
52
- *
53
- * By default the class will stop you from enlarging your images (or else they will look grainy) and if you want to do this you must turn off the protection mode by passing a 5th parameter
54
- *
55
- * $objResize = new RVJ_ImageResize("myImage.gif", "myEnlargedImage.gif", "P", "200", false);
56
- *
57
- */
58
-
59
- class RVJ_ImageResize {
60
-
61
- var $strOriginalImagePath;
62
- var $strResizedImagePath;
63
- var $arrOriginalDetails;
64
- var $arrResizedDetails;
65
- var $resOriginalImage;
66
- var $resResizedImage;
67
- var $numQuality = 90;
68
- var $boolProtect = true;
69
-
70
- /*
71
- *
72
- * @Method: __constructor
73
- * @Parameters: 5
74
- * @Param-1: strPath - String - The path to the image
75
- * @Param-2: strSavePath - String - The path to save the new image to
76
- * @Param-3: strType - String - The type of resize you want to perform
77
- * @Param-4: value - Number/Array - The resize dimensions
78
- * @Param-5: boolProect - Boolen - Protects the image so that it doesnt resize an image if its already smaller
79
- * @Param-6: numQuality - Number - The quality of compression if output is a JPEG
80
- * @Description: Calls the RVJ_Pagination method so its php 4 compatible
81
- *
82
- */
83
-
84
- function __constructor($strPath, $strSavePath, $strType = "W", $value = "150", $boolProtect = true, $numQuality = 95){
85
- $this->RVJ_ImageResize($strPath, $strSavePath, $strType, $value, $boolProtect, $numQuality);
86
- }
87
-
88
- /*
89
- *
90
- * @Method: RVJ_ImageResize
91
- * @Parameters: 5
92
- * @Param-1: strPath - String - The path to the image
93
- * @Param-2: strSavePath - String - The path to save the new image to
94
- * @Param-3: strType - String - The type of resize you want to perform
95
- * @Param-4: value - Number/Array - The resize dimensions
96
- * @Param-5: boolProect - Boolen - Protects the image so that it doesnt resize an image if its already smaller
97
- * @Param-6: numQuality - Number - The quality of compression if output is a JPEG
98
- * @Description: Calls the RVJ_Pagination method so its php 4 compatible
99
- *
100
- */
101
-
102
- function RVJ_ImageResize($strPath, $strSavePath, $strType = "W", $value = "150", $boolProtect = true, $numQuality = 95){
103
- //save the image/path details
104
- $this->strOriginalImagePath = $strPath;
105
- $this->strResizedImagePath = $strSavePath;
106
- $this->numQuality = $numQuality;
107
- $this->boolProtect = $boolProtect;
108
-
109
- //get the image dimensions
110
- $this->arrOriginalDetails = getimagesize($this->strOriginalImagePath);
111
- $this->arrResizedDetails = $this->arrOriginalDetails;
112
-
113
- //create an image resouce to work with
114
- $this->resOriginalImage = $this->createImage($this->strOriginalImagePath);
115
-
116
- //select the image resize type
117
- switch(strtoupper($strType)){
118
- case "P":
119
- $this->resizeToPercent($value);
120
- break;
121
- case "H":
122
- $this->resizeToHeight($value);
123
- break;
124
- case "C":
125
- $this->resizeToCustom($value);
126
- break;
127
- case "W":
128
- default:
129
- $this->resizeToWidth($value);
130
- break;
131
- }
132
- }
133
-
134
- /*
135
- *
136
- * @Method: findResourceDetails
137
- * @Parameters: 1
138
- * @Param-1: resImage - Resource - The image resource you want details on
139
- * @Description: Returns an array of details about the resource identifier that you pass it
140
- *
141
- */
142
-
143
- function findResourceDetails($resImage){
144
- //check to see what image is being requested
145
- if($resImage==$this->resResizedImage){
146
- //return new image details
147
- return $this->arrResizedDetails;
148
- }else{
149
- //return original image details
150
- return $this->arrOriginalDetails;
151
- }
152
- }
153
-
154
- /*
155
- *
156
- * @Method: updateNewDetails
157
- * @Parameters: 0
158
- * @Description: Updates the width and height values of the resized details array
159
- *
160
- */
161
-
162
- function updateNewDetails(){
163
- $this->arrResizedDetails[0] = imagesx($this->resResizedImage);
164
- $this->arrResizedDetails[1] = imagesy($this->resResizedImage);
165
- }
166
-
167
- /*
168
- *
169
- * @Method: createImage
170
- * @Parameters: 1
171
- * @Param-1: strImagePath - String - The path to the image
172
- * @Description: Created an image resource of the image path passed to it
173
- *
174
- */
175
-
176
- function createImage($strImagePath){
177
- //get the image details
178
- $arrDetails = $this->findResourceDetails($strImagePath);
179
-
180
- //choose the correct function for the image type
181
- switch($arrDetails['mime']){
182
- case "image/jpeg":
183
- return imagecreatefromjpeg($strImagePath);
184
- break;
185
- case "image/png":
186
- return imagecreatefrompng($strImagePath);
187
- break;
188
- case "image/gif":
189
- return imagecreatefromgif($strImagePath);
190
- break;
191
- }
192
- }
193
-
194
- /*
195
- *
196
- * @Method: saveImage
197
- * @Parameters: 1
198
- * @Param-1: numQuality - Number - The quality to save the image at
199
- * @Description: Saves the resize image
200
- *
201
- */
202
-
203
- function saveImage($numQuality = 90){
204
- switch($this->arrResizedDetails['mime']){
205
- case "image/jpeg":
206
- imagejpeg($this->resResizedImage, $this->strResizedImagePath, $numQuality);
207
- break;
208
- case "image/png":
209
- // imagepng = [0-9] (not [0-100])
210
- imagepng($this->resResizedImage, $this->strResizedImagePath, 7);
211
- break;
212
- case "image/gif":
213
- imagegif($this->resResizedImage, $this->strResizedImagePath);
214
- break;
215
- }
216
- }
217
-
218
- /*
219
- *
220
- * @Method: showImage
221
- * @Parameters: 1
222
- * @Param-1: resImage - Resource - The resource of the image you want to display
223
- * @Description: Displays the image resouce on the screen
224
- *
225
- */
226
-
227
- function showImage($resImage){
228
- //get the image details
229
- $arrDetails = $this->findResourceDetails($resImage);
230
-
231
- //set the correct header for the image we are displaying
232
- header("Content-type: ".$arrDetails['mime']);
233
- switch($arrDetails['mime']){
234
- case "image/jpeg":
235
- return imagejpeg($resImage);
236
- break;
237
- case "image/png":
238
- return imagepng($resImage);
239
- break;
240
- case "image/gif":
241
- return imagegif($resImage);
242
- break;
243
- }
244
- }
245
-
246
- /*
247
- *
248
- * @Method: destroyImage
249
- * @Parameters: 1
250
- * @Param-1: resImage - Resource - The image resource you want to destroy
251
- * @Description: Destroys the image resource and so cleans things up
252
- *
253
- */
254
-
255
- function destroyImage($resImage){
256
- imagedestroy($resImage);
257
- }
258
-
259
- /*
260
- *
261
- * @Method: _resize
262
- * @Parameters: 2
263
- * @Param-1: numWidth - Number - The width of the image in pixels
264
- * @Param-2: numHeight - Number - The height of the image in pixes
265
- * @Param-3: numQuality - Number - The quality of compression if output is a JPEG
266
- * @Description: Resizes the image by creatin a new canvas and copying the image over onto it. DONT CALL THIS METHOD DIRECTLY - USE THE METHODS BELOW
267
- *
268
- */
269
-
270
- function _resize($numWidth, $numHeight, $numQuality=95){
271
- //check for image protection
272
- if($this->_imageProtect($numWidth, $numHeight)){
273
-
274
- // GIF image
275
- if($this->arrOriginalDetails['mime']=="image/gif"){
276
- $this->resResizedImage = imagecreate($numWidth, $numHeight);
277
- }
278
-
279
- // JPG image
280
- else if($this->arrOriginalDetails['mime']=="image/jpeg"){
281
- $this->resResizedImage = imagecreatetruecolor($numWidth, $numHeight);
282
- }
283
-
284
- // PNG image
285
- else if($this->arrOriginalDetails['mime']=="image/png"){
286
- $this->resResizedImage = imagecreatetruecolor($numWidth, $numHeight);
287
- imagecolortransparent($this->resResizedImage, imagecolorallocate($this->resResizedImage, 0, 0, 0));
288
- imagealphablending($this->resResizedImage, false);
289
- imagesavealpha($this->resResizedImage, true);
290
- }
291
- //update the image size details
292
- $this->updateNewDetails();
293
- //do the actual image resize
294
- if (function_exists('imagecopyresampled')) {
295
- imagecopyresampled($this->resResizedImage, $this->resOriginalImage, 0, 0, 0, 0, $numWidth, $numHeight, $this->arrOriginalDetails[0], $this->arrOriginalDetails[1]);
296
- } else {
297
- imagecopyresized($this->resResizedImage, $this->resOriginalImage, 0, 0, 0, 0, $numWidth, $numHeight, $this->arrOriginalDetails[0], $this->arrOriginalDetails[1]);
298
- }
299
-
300
- //saves the image
301
- $this->saveImage($numQuality);
302
- }
303
- }
304
-
305
- /*
306
- *
307
- * @Method: _imageProtect
308
- * @Parameters: 2
309
- * @Param-1: numWidth - Number - The width of the image in pixels
310
- * @Param-2: numHeight - Number - The height of the image in pixes
311
- * @Description: Checks to see if we should allow the resize to take place or not depending on the size the image will be resized to
312
- *
313
- */
314
-
315
- function _imageProtect($numWidth, $numHeight){
316
- if($this->boolProtect AND ($numWidth >= $this->arrOriginalDetails[0] OR $numHeight >= $this->arrOriginalDetails[1])){
317
- return 0;
318
- }
319
- return 1;
320
- }
321
-
322
- /*
323
- *
324
- * @Method: resizeToWidth
325
- * @Parameters: 1
326
- * @Param-1: numWidth - Number - The width to resize to in pixels
327
- * @Description: Works out the height value to go with the width value passed, then calls the resize method.
328
- *
329
- */
330
-
331
- function resizeToWidth($numWidth){
332
- $numHeight=(int)(($numWidth*$this->arrOriginalDetails[1])/$this->arrOriginalDetails[0]);
333
- $this->_resize($numWidth, $numHeight, $this->numQuality);
334
- }
335
-
336
- /*
337
- *
338
- * @Method: resizeToHeight
339
- * @Parameters: 1
340
- * @Param-1: numHeight - Number - The height to resize to in pixels
341
- * @Description: Works out the width value to go with the height value passed, then calls the resize method.
342
- *
343
- */
344
-
345
- function resizeToHeight($numHeight){
346
- $numWidth=(int)(($numHeight*$this->arrOriginalDetails[0])/$this->arrOriginalDetails[1]);
347
- $this->_resize($numWidth, $numHeight, $this->numQuality);
348
- }
349
-
350
- /*
351
- *
352
- * @Method: resizeToPercent
353
- * @Parameters: 1
354
- * @Param-1: numPercent - Number - The percentage you want to resize to
355
- * @Description: Works out the width and height value to go with the percent value passed, then calls the resize method.
356
- *
357
- */
358
-
359
- function resizeToPercent($numPercent){
360
- $numWidth = (int)(($this->arrOriginalDetails[0]/100)*$numPercent);
361
- $numHeight = (int)(($this->arrOriginalDetails[1]/100)*$numPercent);
362
- $this->_resize($numWidth, $numHeight, $this->numQuality);
363
- }
364
-
365
- /*
366
- *
367
- * @Method: resizeToCustom
368
- * @Parameters: 1
369
- * @Param-1: size - Number/Array - Either a number of array of numbers for the width and height in pixels
370
- * @Description: Checks to see if array was passed and calls the resize method with the correct values.
371
- *
372
- */
373
-
374
- function resizeToCustom($size){
375
- if(!is_array($size)){
376
- $this->_resize((int)$size, (int)$size, $this->numQuality);
377
- }else{
378
- $this->_resize((int)$size[0], (int)$size[1], $this->numQuality);
379
- }
380
- }
381
- }
382
- ?>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_i
4
  Tags: image, processing, plugin, resize, upload, resizing, optimization, optimize, optimise, optimisation, downsize
5
  Requires at least: 3.5
6
  Tested up to: 4.1
7
- Stable tag: 1.6.2
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
 
@@ -24,8 +24,8 @@ This plugin uses standard PHP image resizing functions and will require a high a
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
 
@@ -33,6 +33,11 @@ This plugin uses standard PHP image resizing functions and will require a high a
33
 
34
  == Changelog ==
35
 
 
 
 
 
 
36
  = 1.6.2 =
37
  Minor maintenance release:
38
 
4
  Tags: image, processing, plugin, resize, upload, resizing, optimization, optimize, optimise, optimisation, downsize
5
  Requires at least: 3.5
6
  Tested up to: 4.1
7
+ Stable tag: 1.7
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
 
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 max-width/max-height settings under 'Settings > Resize Image Upload'.
28
+ 4. Once active, just upload images as normal and it will just work!
29
 
30
  == Screenshots ==
31
 
33
 
34
  == Changelog ==
35
 
36
+ = 1.7 =
37
+ This is a major under-the-hood release to change core workings of the plugin. The plugin still functions as normal, but the way in which it resizes images has now changed to use standard WordPress libraries. This means that should your server have better image processing libraries than the GD default (e.g. ImageMagick), then the resizing method should make use of them. This should improve the output of your resized images!
38
+
39
+ * [Update] Plugin completely re-engineered to use WP_Image_Editor when resizing images.
40
+
41
  = 1.6.2 =
42
  Minor maintenance release:
43
 
resize-image-after-upload.php CHANGED
@@ -4,17 +4,11 @@ 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.2
8
  Author URI: http://www.philr.ae/
9
 
10
  Copyright (C) 2015 iamphilrae
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
20
  as published by the Free Software Foundation; either version 2
@@ -30,7 +24,7 @@ along with this program; if not, write to the Free Software
30
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31
  */
32
 
33
- $PLUGIN_VERSION = '1.6.2';
34
  $DEBUG_LOGGER = false;
35
 
36
 
@@ -109,11 +103,11 @@ function jr_uploadresize_options(){
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);
@@ -271,12 +265,12 @@ function jr_uploadresize_options(){
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>
@@ -349,167 +343,126 @@ function jr_uploadresize_options(){
349
  */
350
  function jr_uploadresize_resize($image_data){
351
 
352
- global $DEBUG_LOGGER;
353
-
354
- if(
355
- $image_data['type'] == 'image/jpeg' ||
356
- $image_data['type'] == 'image/jpg' ||
357
- $image_data['type'] == 'image/gif' ||
358
- $image_data['type'] == 'image/png' ||
359
- $image_data['type'] == 'image/bmp'
360
- )
361
- {
362
-
363
- // Include the file to carry out the resizing
364
- require_once('class.resize.php');
365
 
 
366
 
367
- $resizing_enabled = get_option('jr_resizeupload_resize_yesno');
368
- $resizing_enabled = ($resizing_enabled=='yes') ? true : false;
369
 
370
- $force_jpeg_recompression = get_option('jr_resizeupload_recompress_yesno');
371
- $force_jpeg_recompression = ($force_jpeg_recompression=='yes') ? true : false;
372
-
373
- $compression_level = get_option('jr_resizeupload_quality');
374
-
375
-
376
- $max_width = get_option('jr_resizeupload_width')==0
377
- ? false : get_option('jr_resizeupload_width');
378
 
379
- $max_height = get_option('jr_resizeupload_height')==0
380
- ? false : get_option('jr_resizeupload_height');
381
 
 
382
 
383
- $convert_png_to_jpg = get_option('jr_resizeupload_convertpng_yesno');
384
- $convert_png_to_jpg = ($convert_png_to_jpg=='yes') ? true : false;
385
 
386
- $convert_gif_to_jpg = get_option('jr_resizeupload_convertgif_yesno');
387
- $convert_gif_to_jpg = ($convert_gif_to_jpg=='yes') ? true : false;
388
 
389
- $convert_bmp_to_jpg = get_option('jr_resizeupload_convertbmp_yesno');
390
- $convert_bmp_to_jpg = ($convert_bmp_to_jpg=='yes') ? true : false;
391
 
392
 
393
- // Get original image sizes
394
- $original_info = getimagesize($image_data['file']);
395
- $original_width = $original_info[0];
396
- $original_height = $original_info[1];
397
 
398
- $original_is_jpg = false;
399
- $original_is_png = false;
400
- $original_is_gif = false;
401
- $original_is_bmp = false;
402
 
403
- switch($image_data['type']) {
 
404
 
405
- case 'image/jpg' :
406
- case 'image/jpeg' : $original_is_jpg = true; break;
407
 
408
- case 'image/png' : $original_is_png = true; break;
409
 
410
- case 'image/gif' : $original_is_gif = true; break;
411
 
412
- case 'image/bmp' : $original_is_bmp = true; break;
413
 
 
 
414
  }
415
 
416
-
417
- if($DEBUG_LOGGER) error_log('LOG: '.print_r('upload-start', true));
 
418
 
419
 
420
- //
421
- // Perform resizing operations if reduction is required
422
- if(
423
- $resizing_enabled
424
- && ($max_width || $max_height) // resize in at least one dimension
425
- && (
426
- ($max_width && ($original_width > $max_width)) // width possibly needs resizing
427
- ||
428
- ($max_height && ($original_height > $max_height)) // height possibly needs resizing
429
- )
430
- && !$original_is_bmp) // plugin does not work with bitmaps
431
- {
432
-
433
- if($DEBUG_LOGGER) error_log('LOG: '.print_r('--resizing', true));
434
-
435
- $protect_image = true;
436
- $resize_direction = null;
437
- $resize_max = null;
438
 
 
439
 
440
- // Width is unlimited, limit by height
441
- if((!$max_width && $max_height)
442
- && ($original_height > $max_height)) {
443
 
444
- $resize_direction = 'H';
445
- $resize_max = $max_height;
446
- if($DEBUG_LOGGER) error_log('LOG: '.print_r(' >--by-height (width-unlimited)', true));
447
- }
448
 
449
- // Height is unlimited, limit by width
450
- else if((!$max_height && $max_width)
451
- && ($original_width > $max_width)) {
452
 
453
- $resize_direction = 'W';
454
- $resize_max = $max_width;
455
 
456
- if($DEBUG_LOGGER) error_log('LOG: '.print_r(' >--by-width (height-unlimited)', true));
457
- }
458
 
459
- // Both dimensions require limiting, figure out which
460
- else {
 
461
 
462
- if(($original_height/$max_height) > ($original_width/$max_width)) {
463
- $resize_direction = 'H';
464
- $resize_max = $max_height;
465
- if($DEBUG_LOGGER) error_log('LOG: '.print_r(' >--by-height', true));
466
  }
467
-
468
  else {
469
- $resize_direction = 'W';
470
- $resize_max = $max_width;
471
- if($DEBUG_LOGGER) error_log('LOG: '.print_r(' >--by-width', true));
472
  }
473
  }
 
 
 
474
 
475
- if(!is_null($resize_direction) && !is_null($resize_max)) {
476
-
477
- $objResize = new RVJ_ImageResize(
478
- $image_data['file'], $image_data['file'],
479
- $resize_direction, $resize_max,
480
- $protect_image, $compression_level
481
- );
482
 
 
 
483
 
484
- if($DEBUG_LOGGER) error_log('LOG: '.print_r(' >--resize-done', true));
 
 
 
 
485
  }
486
- }
487
 
488
 
489
- //
490
- // Resizing is not required, but still need to re-compress to desired quality and filetype
491
- else if(($original_is_jpg && $force_jpeg_recompression)
492
- || ($original_is_png && $convert_png_to_jpg)
493
- || ($original_is_gif && $convert_gif_to_jpg)
494
- || ($original_is_bmp && $convert_bmp_to_jpg)) {
495
 
496
- $protect_image = false;
 
 
 
 
 
 
 
 
497
 
498
- $objResize = new RVJ_ImageResize($image_data['file'], $image_data['file'], 'P', 100, $protect_image, $compression_level);
 
 
499
 
500
- if($DEBUG_LOGGER) error_log('LOG: '.print_r('--recompression/conversion', true));
501
- }
502
 
503
 
504
- //
505
- // No resizing, recompression, or conversion required
506
- else {
507
- if($DEBUG_LOGGER) error_log('LOG: '.print_r('--no-action-required', true));
508
- }
509
 
510
- if($DEBUG_LOGGER) error_log('LOG: '.print_r("end\n", true));
511
 
512
- } // if(...)
 
 
 
 
 
513
 
514
- return $image_data;
515
- } // function jr_uploadresize_resize($image_data){
 
 
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.7
8
  Author URI: http://www.philr.ae/
9
 
10
  Copyright (C) 2015 iamphilrae
11
 
 
 
 
 
 
 
12
  This program is free software; you can redistribute it and/or
13
  modify it under the terms of the GNU General Public License
14
  as published by the Free Software Foundation; either version 2
24
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25
  */
26
 
27
+ $PLUGIN_VERSION = '1.7';
28
  $DEBUG_LOGGER = false;
29
 
30
 
103
  $compression_level = ($compression_level == '') ? 1 : $compression_level;
104
  $compression_level = (ctype_digit(strval($compression_level)) == false) ? get_option('jr_resizeupload_quality') : $compression_level;
105
 
106
+ if($compression_level < 1) {
107
  $compression_level = 1;
108
  }
109
+ else if($compression_level > 100) {
110
+ $compression_level = 100;
111
  }
112
 
113
  update_option('jr_resizeupload_quality',$compression_level);
265
  <th scope="row">JPEG compression level</th>
266
  <td valign="top">
267
  <select id="quality" name="quality">
268
+ <?php for($i=1; $i<=100; $i++) : ?>
269
  <option value="<?php echo $i; ?>" <?php if($compression_level == $i) : ?>selected<?php endif; ?>><?php echo $i; ?></option>
270
  <?php endfor; ?>
271
  </select>
272
  <p class="description"><code>1</code> = low quality (smallest files)
273
+ <br><code>100</code> = best quality (largest files)
274
  <br>Recommended value: <code>90</code></p>
275
  </td>
276
  </tr>
343
  */
344
  function jr_uploadresize_resize($image_data){
345
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
 
347
+ jr_error_log("**-start--resize-image-upload");
348
 
 
 
349
 
350
+ $resizing_enabled = get_option('jr_resizeupload_resize_yesno');
351
+ $resizing_enabled = ($resizing_enabled=='yes') ? true : false;
 
 
 
 
 
 
352
 
353
+ $force_jpeg_recompression = get_option('jr_resizeupload_recompress_yesno');
354
+ $force_jpeg_recompression = ($force_jpeg_recompression=='yes') ? true : false;
355
 
356
+ $compression_level = get_option('jr_resizeupload_quality');
357
 
 
 
358
 
359
+ $max_width = get_option('jr_resizeupload_width')==0 ? false : get_option('jr_resizeupload_width');
 
360
 
361
+ $max_height = get_option('jr_resizeupload_height')==0 ? false : get_option('jr_resizeupload_height');
 
362
 
363
 
364
+ $convert_png_to_jpg = get_option('jr_resizeupload_convertpng_yesno');
365
+ $convert_png_to_jpg = ($convert_png_to_jpg=='yes') ? true : false;
 
 
366
 
367
+ $convert_gif_to_jpg = get_option('jr_resizeupload_convertgif_yesno');
368
+ $convert_gif_to_jpg = ($convert_gif_to_jpg=='yes') ? true : false;
 
 
369
 
370
+ $convert_bmp_to_jpg = get_option('jr_resizeupload_convertbmp_yesno');
371
+ $convert_bmp_to_jpg = ($convert_bmp_to_jpg=='yes') ? true : false;
372
 
 
 
373
 
 
374
 
375
+ //---------- In with the old v1.6.2, new v1.7 (WP_Image_Editor) ------------
376
 
377
+ if($resizing_enabled || $force_jpeg_recompression) {
378
 
379
+ if(empty($image_data['file']) || empty($image_data['type'])) {
380
+ return $image_data;
381
  }
382
 
383
+ jr_error_log("--filename-( ".$image_data['file']." )");
384
+ $image_editor = wp_get_image_editor($image_data['file']);
385
+ $image_type = $image_data['type'];
386
 
387
 
388
+ if(is_wp_error($image_editor)) {
389
+ jr_error_log("--wp-error-reported");
390
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
391
 
392
+ else {
393
 
394
+ $to_save = false;
395
+ $resized = false;
 
396
 
 
 
 
 
397
 
398
+ // Perform resizing if required
399
+ if($resizing_enabled) {
 
400
 
401
+ jr_error_log("--resizing-enabled");
402
+ $sizes = $image_editor->get_size();
403
 
404
+ if((isset($sizes['width']) && $sizes['width'] > $max_width)
405
+ || (isset($sizes['height']) && $sizes['height'] > $max_height)) {
406
 
407
+ $image_editor->resize($max_width, $max_height, false);
408
+ $resized = true;
409
+ $to_save = true;
410
 
411
+ $sizes = $image_editor->get_size();
412
+ jr_error_log("--new-size--".$sizes['width']."x".$sizes['height']);
 
 
413
  }
 
414
  else {
415
+ jr_error_log("--no-resizing-needed");
 
 
416
  }
417
  }
418
+ else {
419
+ jr_error_log("--no-resizing-requested");
420
+ }
421
 
 
 
 
 
 
 
 
422
 
423
+ // Regardless of resizing, image must be saved if recompressing
424
+ if($force_jpeg_recompression && ($image_type=='image/jpg' || $image_type=='image/jpeg')) {
425
 
426
+ $to_save = true;
427
+ jr_error_log("--compression-level--q-".$compression_level);
428
+ }
429
+ elseif(!$resized) {
430
+ jr_error_log("--no-forced-recompression");
431
  }
 
432
 
433
 
434
+ // Only save image if it has been resized or need recompressing
435
+ if($to_save) {
 
 
 
 
436
 
437
+ $image_editor->set_quality($compression_level);
438
+ $saved_image = $image_editor->save($image_data['file']);
439
+ jr_error_log("--image-saved");
440
+ }
441
+ else {
442
+ jr_error_log("--no-changes-to-save");
443
+ }
444
+ }
445
+ } // if($resizing_enabled || $force_jpeg_recompression)
446
 
447
+ else {
448
+ jr_error_log("--no-action-required");
449
+ }
450
 
451
+ jr_error_log("**-end--resize-image-upload\n");
 
452
 
453
 
454
+ return $image_data;
455
+ } // function jr_uploadresize_resize($image_data){
 
 
 
456
 
 
457
 
458
+ /**
459
+ * Simple debug logging function. Will only output to the log file
460
+ * if 'debugging' is turned on.
461
+ */
462
+ function jr_error_log($message) {
463
+ global $DEBUG_LOGGER;
464
 
465
+ if($DEBUG_LOGGER) {
466
+ error_log(print_r($message, true));
467
+ }
468
+ }