Converter for Media – Optimize images | Convert WebP & AVIF - Version 1.2.6

Version Description

(2020-05-28) = * [Fixed] Removal of WebP files larger than original during upload

Download this release

Release Info

Developer mateuszgbiorczyk
Plugin Icon 128x128 Converter for Media – Optimize images | Convert WebP & AVIF
Version 1.2.6
Comparing to
See all releases

Code changes from version 1.2.5 to 1.2.6

app/Convert/Gd.php CHANGED
@@ -113,13 +113,14 @@
113
  }
114
 
115
  if (filesize($output) % 2 === 1) file_put_contents($output, "\0", FILE_APPEND);
 
 
116
  return [
117
  'success' => true,
118
  'data' => [
119
- 'path' => $output,
120
  'size' => [
121
  'before' => filesize($path),
122
- 'after' => filesize($output),
123
  ],
124
  ],
125
  ];
113
  }
114
 
115
  if (filesize($output) % 2 === 1) file_put_contents($output, "\0", FILE_APPEND);
116
+ do_action('webpc_convert_after', $output, $path);
117
+
118
  return [
119
  'success' => true,
120
  'data' => [
 
121
  'size' => [
122
  'before' => filesize($path),
123
+ 'after' => filesize((file_exists($output)) ? $output : $path),
124
  ],
125
  ],
126
  ];
app/Convert/Imagick.php CHANGED
@@ -80,14 +80,14 @@
80
  if (!$success) {
81
  throw new \Exception('Error occurred while converting image.');
82
  }
 
83
 
84
  return [
85
  'success' => true,
86
  'data' => [
87
- 'path' => $output,
88
  'size' => [
89
  'before' => filesize($path),
90
- 'after' => filesize($output),
91
  ],
92
  ],
93
  ];
80
  if (!$success) {
81
  throw new \Exception('Error occurred while converting image.');
82
  }
83
+ do_action('webpc_convert_after', $output, $path);
84
 
85
  return [
86
  'success' => true,
87
  'data' => [
 
88
  'size' => [
89
  'before' => filesize($path),
90
+ 'after' => filesize((file_exists($output)) ? $output : $path),
91
  ],
92
  ],
93
  ];
app/Convert/Size.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace WebpConverter\Convert;
4
+
5
+ class Size
6
+ {
7
+ public function __construct()
8
+ {
9
+ add_action('webpc_convert_after', [$this, 'removeImageIfIsLarger'], 10, 2);
10
+ }
11
+
12
+ /* ---
13
+ Functions
14
+ --- */
15
+
16
+ public function removeImageIfIsLarger($webpPath, $originalPath)
17
+ {
18
+ if ((!$settings = apply_filters('webpc_get_values', []))
19
+ || !in_array('only_smaller', $settings['features'])
20
+ || (!file_exists($webpPath) || !file_exists($originalPath))
21
+ || (filesize($webpPath) < filesize($originalPath))) return;
22
+
23
+ unlink($webpPath);
24
+ }
25
+ }
app/Convert/_Core.php ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ namespace WebpConverter\Convert;
4
+
5
+ class _Core
6
+ {
7
+ public function __construct()
8
+ {
9
+ new Size();
10
+ }
11
+ }
app/Regenerate/Regenerate.php CHANGED
@@ -23,8 +23,6 @@
23
 
24
  foreach ($paths as $path) {
25
  $response = $convert->convertImage($path, $settings['quality']);
26
- if ($response['success'] && (in_array('only_smaller', $settings['features'])
27
- && !$this->checkFileSize($response['data']))) continue;
28
 
29
  if ($response['success'] !== true) {
30
  $errors[] = $response['message'];
@@ -42,11 +40,4 @@
42
  ],
43
  ];
44
  }
45
-
46
- private function checkFileSize($data)
47
- {
48
- if ($data['size']['after'] < $data['size']['before']) return true;
49
- unlink($data['path']);
50
- return false;
51
- }
52
  }
23
 
24
  foreach ($paths as $path) {
25
  $response = $convert->convertImage($path, $settings['quality']);
 
 
26
 
27
  if ($response['success'] !== true) {
28
  $errors[] = $response['message'];
40
  ],
41
  ];
42
  }
 
 
 
 
 
 
 
43
  }
app/Settings/Errors.php CHANGED
@@ -26,7 +26,7 @@
26
  'path_duplicated' => ($this->ifPathsAreDifferent() !== true),
27
  'rest_api' => ($this->ifRestApiIsEnabled() !== true),
28
  'methods' => ($this->ifMethodsAreAvailable() !== true),
29
- 'bypassing_apache' => ($this->ifBypassingApacheIsActive() !== true),
30
  ];
31
 
32
  $this->list = array_keys(array_filter($list));
@@ -76,9 +76,17 @@
76
 
77
  private function ifBypassingApacheIsActive()
78
  {
79
- $filePng = @file_get_contents(WEBPC_URL . 'public/img/icon-before.png');
80
- $filePng2 = @file_get_contents(WEBPC_URL . 'public/img/icon-before.png2');
 
 
 
 
 
81
 
82
- return (strlen($filePng) >= strlen($filePng2));
 
 
 
83
  }
84
  }
26
  'path_duplicated' => ($this->ifPathsAreDifferent() !== true),
27
  'rest_api' => ($this->ifRestApiIsEnabled() !== true),
28
  'methods' => ($this->ifMethodsAreAvailable() !== true),
29
+ 'bypassing_apache' => ($this->ifBypassingApacheIsActive() === true),
30
  ];
31
 
32
  $this->list = array_keys(array_filter($list));
76
 
77
  private function ifBypassingApacheIsActive()
78
  {
79
+ $ctx = stream_context_create([
80
+ 'http' => [
81
+ 'timeout' => 1,
82
+ ],
83
+ ]);
84
+ $filePng = @file_get_contents(WEBPC_URL . 'public/img/icon-before.png', false, $ctx);
85
+ if ($filePng === false) return false;
86
 
87
+ $filePng2 = @file_get_contents(WEBPC_URL . 'public/img/icon-before.png2', false, $ctx);
88
+ if ($filePng2 === false) return false;
89
+
90
+ return (strlen($filePng) < strlen($filePng2));
91
  }
92
  }
app/WebpConverter.php CHANGED
@@ -8,6 +8,7 @@
8
  {
9
  new Action\_Core();
10
  new Admin\_Core();
 
11
  new Media\_Core();
12
  new Regenerate\_Core();
13
  new Settings\_Core();
8
  {
9
  new Action\_Core();
10
  new Admin\_Core();
11
+ new Convert\_Core();
12
  new Media\_Core();
13
  new Regenerate\_Core();
14
  new Settings\_Core();
readme.txt CHANGED
@@ -442,12 +442,15 @@ This is all very important to us and allows us to do even better things for you!
442
 
443
  == Changelog ==
444
 
 
 
 
445
  = 1.2.5 (2020-05-10) =
446
  * `[Removed]` Link to plugin settings on Network Admin Screen for WordPress Multisite
447
  * `[Fixed]` Path in RewriteRule for WordPress Multisite
448
  * `[Changed]` Error messages in administration panel
449
  * `[Added]` Support for `disable_functions` setting for using `set_time_limit` function
450
- * `[Added]` Support for blocked function `file_get_contents`
451
 
452
  = 1.2.4 (2020-04-24) =
453
  * `[Changed]` Error messages in administration panel
442
 
443
  == Changelog ==
444
 
445
+ = 1.2.6 (2020-05-28) =
446
+ * `[Fixed]` Removal of WebP files larger than original during upload
447
+
448
  = 1.2.5 (2020-05-10) =
449
  * `[Removed]` Link to plugin settings on Network Admin Screen for WordPress Multisite
450
  * `[Fixed]` Path in RewriteRule for WordPress Multisite
451
  * `[Changed]` Error messages in administration panel
452
  * `[Added]` Support for `disable_functions` setting for using `set_time_limit` function
453
+ * `[Added]` Support for blocked function `file_get_contents`
454
 
455
  = 1.2.4 (2020-04-24) =
456
  * `[Changed]` Error messages in administration panel
webp-converter-for-media.php CHANGED
@@ -3,13 +3,13 @@
3
  /*
4
  Plugin Name: WebP Converter for Media
5
  Description: Speed up your website by serving WebP images instead of standard formats JPEG, PNG and GIF.
6
- Version: 1.2.5
7
  Author: Mateusz Gbiorczyk
8
  Author URI: https://gbiorczyk.pl/
9
  Text Domain: webp-converter
10
  */
11
 
12
- define('WEBPC_VERSION', '1.2.5');
13
  define('WEBPC_FILE', __FILE__);
14
  define('WEBPC_NAME', plugin_basename(__FILE__));
15
  define('WEBPC_PATH', plugin_dir_path(__FILE__));
3
  /*
4
  Plugin Name: WebP Converter for Media
5
  Description: Speed up your website by serving WebP images instead of standard formats JPEG, PNG and GIF.
6
+ Version: 1.2.6
7
  Author: Mateusz Gbiorczyk
8
  Author URI: https://gbiorczyk.pl/
9
  Text Domain: webp-converter
10
  */
11
 
12
+ define('WEBPC_VERSION', '1.2.6');
13
  define('WEBPC_FILE', __FILE__);
14
  define('WEBPC_NAME', plugin_basename(__FILE__));
15
  define('WEBPC_PATH', plugin_dir_path(__FILE__));