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

Version Description

(2020-06-11) = * [Changed] Moving converted WebP files to /uploads-webpc/uploads directory from /uploads-webpc directory (required manual configuration change for Nginx and WordPress Multisite) * [Changed] Validation when converting images

Download this release

Release Info

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

Code changes from version 1.2.6 to 1.2.7

app/Admin/Activation.php CHANGED
@@ -32,7 +32,7 @@
32
  public function createDirectoryForUploadsWebp()
33
  {
34
  $path = apply_filters('webpc_uploads_webp', '');
35
- if (is_writable(dirname($path))) mkdir($path);
36
  }
37
 
38
  public function addDefaultOptions()
32
  public function createDirectoryForUploadsWebp()
33
  {
34
  $path = apply_filters('webpc_uploads_webp', '');
35
+ if (!file_exists($path) && is_writable(dirname($path))) mkdir($path);
36
  }
37
 
38
  public function addDefaultOptions()
app/Admin/Update.php CHANGED
@@ -18,22 +18,26 @@
18
 
19
  public function runActionsAfterUpdate()
20
  {
21
- $version = get_option($this->optionVersion, '0.0.0');
22
  if ($version === WEBPC_VERSION) return;
23
 
24
- $this->saveOptionValue($this->optionSettings, $this->updateSettingsForOldVersions($version));
25
- $this->saveOptionValue($this->optionVersion, WEBPC_VERSION);
 
 
26
 
27
  do_action('webpc_rewrite_htaccess', true);
28
  flush_rewrite_rules(true);
 
29
  }
30
 
31
  private function updateSettingsForOldVersions($version)
32
  {
33
  $settings = apply_filters('webpc_get_values', []);
34
 
35
- if (version_compare($version, '1.1.2', '>')) return $settings;
36
- $settings['features'][] = 'only_smaller';
 
37
 
38
  $settings['features'] = array_unique($settings['features']);
39
  return $settings;
@@ -44,4 +48,24 @@
44
  if (get_option($optionKey, false) !== false) update_option($optionKey, $value);
45
  else add_option($optionKey, $value);
46
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  }
18
 
19
  public function runActionsAfterUpdate()
20
  {
21
+ $version = get_option($this->optionVersion, null);
22
  if ($version === WEBPC_VERSION) return;
23
 
24
+ if ($version !== null) {
25
+ $this->saveOptionValue($this->optionSettings, $this->updateSettingsForOldVersions($version));
26
+ $this->moveFilesToUploadsSubdirectory($this->optionVersion);
27
+ }
28
 
29
  do_action('webpc_rewrite_htaccess', true);
30
  flush_rewrite_rules(true);
31
+ $this->saveOptionValue($this->optionVersion, WEBPC_VERSION);
32
  }
33
 
34
  private function updateSettingsForOldVersions($version)
35
  {
36
  $settings = apply_filters('webpc_get_values', []);
37
 
38
+ if (version_compare($version, '1.1.2', '<=')) {
39
+ $settings['features'][] = 'only_smaller';
40
+ }
41
 
42
  $settings['features'] = array_unique($settings['features']);
43
  return $settings;
48
  if (get_option($optionKey, false) !== false) update_option($optionKey, $value);
49
  else add_option($optionKey, $value);
50
  }
51
+
52
+ private function moveFilesToUploadsSubdirectory($version)
53
+ {
54
+ if (version_compare($version, '1.2.7', '>')) return;
55
+
56
+ $webpRoot = apply_filters('webpc_uploads_webp', '');
57
+ if (!is_writable($webpRoot)) return;
58
+
59
+ $pathParts = explode('/', apply_filters('webpc_uploads_dir', ''));
60
+ $oldPaths = scandir(apply_filters('webpc_uploads_webp', ''));
61
+ for ($i = 1; $i <= count($pathParts); $i++) {
62
+ $dirPath = $webpRoot . '/' . implode('/', array_slice($pathParts, 0, $i));
63
+ if (!file_exists($dirPath)) mkdir($dirPath);
64
+ }
65
+
66
+ foreach ($oldPaths as $path) {
67
+ if (in_array($path, ['.', '..', '.htaccess', $pathParts[0]])) continue;
68
+ rename($webpRoot . '/'. $path, $dirPath . '/'. $path);
69
+ }
70
+ }
71
  }
app/Convert/Directory.php CHANGED
@@ -10,9 +10,12 @@
10
 
11
  public function getPath($path, $createDirectory = false)
12
  {
13
- $pathSource = apply_filters('webpc_uploads_path', '', true);
14
- $pathOutput = apply_filters('webpc_uploads_webp', '', true);
15
- $newPath = str_replace("/${pathSource}/", "/${pathOutput}/", $path) . '.webp';
 
 
 
16
  if (!$createDirectory) return $newPath;
17
 
18
  if (!$paths = $this->checkDirectories($newPath)) return $newPath;
10
 
11
  public function getPath($path, $createDirectory = false)
12
  {
13
+ $webpRoot = apply_filters('webpc_uploads_webp', '');
14
+ $uploadsRoot = dirname($webpRoot);
15
+ $outputPath = str_replace(realpath($uploadsRoot), '', realpath($path));
16
+ $outputPath = trim($outputPath, '\/');
17
+
18
+ $newPath = sprintf('%s/%s.webp', $webpRoot, $outputPath);
19
  if (!$createDirectory) return $newPath;
20
 
21
  if (!$paths = $this->checkDirectories($newPath)) return $newPath;
app/Convert/Gd.php CHANGED
@@ -13,9 +13,8 @@
13
  (new Server())->setSettings();
14
 
15
  try {
16
- if (!is_writable($path)) {
17
- throw new \Exception(sprintf('File "%s" does not exist.', $path));
18
- }
19
 
20
  $response = $this->createImage($path);
21
  if (!$response['success']) throw new \Exception($response['message']);
13
  (new Server())->setSettings();
14
 
15
  try {
16
+ $status = (new Server())->checkIfFileExists($path);
17
+ if ($status !== true) throw new \Exception($status);
 
18
 
19
  $response = $this->createImage($path);
20
  if (!$response['success']) throw new \Exception($response['message']);
app/Convert/Imagick.php CHANGED
@@ -13,9 +13,8 @@
13
  (new Server())->setSettings();
14
 
15
  try {
16
- if (!is_writable($path)) {
17
- throw new \Exception(sprintf('File "%s" does not exist.', $path));
18
- }
19
 
20
  $response = $this->createImage($path);
21
  if (!$response['success']) throw new \Exception($response['message']);
13
  (new Server())->setSettings();
14
 
15
  try {
16
+ $status = (new Server())->checkIfFileExists($path);
17
+ if ($status !== true) throw new \Exception($status);
 
18
 
19
  $response = $this->createImage($path);
20
  if (!$response['success']) throw new \Exception($response['message']);
app/Convert/Server.php CHANGED
@@ -16,4 +16,11 @@
16
  set_time_limit(120);
17
  }
18
  }
 
 
 
 
 
 
 
19
  }
16
  set_time_limit(120);
17
  }
18
  }
19
+
20
+ public function checkIfFileExists($path)
21
+ {
22
+ if (is_readable($path)) return true;
23
+ else if (!file_exists($path)) return sprintf('File "%s" does not exist. Please check file path using.', $path);
24
+ else return sprintf('File "%s" is unreadable. Please check file permissions.', $path);
25
+ }
26
  }
app/Media/Htaccess.php CHANGED
@@ -21,7 +21,7 @@
21
 
22
  $values = apply_filters('webpc_get_values', []);
23
  $rows = [
24
- $this->getModRewriteRules($values),
25
  ];
26
 
27
  $content = $this->addCommentsToRules($rows);
@@ -45,13 +45,14 @@
45
  $this->saveRewritesInHtaccesss($path, $content);
46
  }
47
 
48
- private function getModRewriteRules($settings)
49
  {
50
  $content = '';
51
  if (!$settings['extensions']) return $content;
52
 
53
  $path = apply_filters('webpc_uploads_prefix', '/');
54
  $path .= apply_filters('webpc_uploads_webp', '', true);
 
55
 
56
  $content .= '<IfModule mod_rewrite.c>' . PHP_EOL;
57
  $content .= ' RewriteEngine On' . PHP_EOL;
21
 
22
  $values = apply_filters('webpc_get_values', []);
23
  $rows = [
24
+ $this->getModRewriteRules($values, apply_filters('webpc_uploads_dir', '')),
25
  ];
26
 
27
  $content = $this->addCommentsToRules($rows);
45
  $this->saveRewritesInHtaccesss($path, $content);
46
  }
47
 
48
+ private function getModRewriteRules($settings, $outputPath = null)
49
  {
50
  $content = '';
51
  if (!$settings['extensions']) return $content;
52
 
53
  $path = apply_filters('webpc_uploads_prefix', '/');
54
  $path .= apply_filters('webpc_uploads_webp', '', true);
55
+ if ($outputPath !== null) $path .= '/' . $outputPath;
56
 
57
  $content .= '<IfModule mod_rewrite.c>' . PHP_EOL;
58
  $content .= ' RewriteEngine On' . PHP_EOL;
app/Settings/Paths.php CHANGED
@@ -4,25 +4,26 @@
4
 
5
  class Paths
6
  {
7
- private $pathSource = 'wp-content/uploads';
8
- private $pathOutput = 'wp-content/uploads-webpc';
9
 
10
  public function __construct()
11
  {
12
- add_filter('webpc_uploads_path', [$this, 'getSourcePath'], 0, 2);
13
- add_filter('webpc_uploads_webp', [$this, 'getOutputPath'], 0, 2);
14
- add_filter('webpc_uploads_path', [$this, 'parsePath'], 100, 2);
15
- add_filter('webpc_uploads_webp', [$this, 'parsePath'], 100, 2);
16
- add_filter('webpc_uploads_prefix', [$this, 'getPrefixPath'], 0);
 
17
  }
18
 
19
  /* ---
20
  Functions
21
  --- */
22
 
23
- public function getSourcePath($value, $skipRoot = false)
24
  {
25
- return $this->pathSource;
26
  }
27
 
28
  public function getOutputPath($value, $skipRoot = false)
@@ -47,4 +48,16 @@
47
 
48
  return str_replace('//', '/', $diffPath);
49
  }
 
 
 
 
 
 
 
 
 
 
 
 
50
  }
4
 
5
  class Paths
6
  {
7
+ private $pathUploads = 'wp-content/uploads';
8
+ private $pathOutput = 'wp-content/uploads-webpc';
9
 
10
  public function __construct()
11
  {
12
+ add_filter('webpc_uploads_path', [$this, 'getUploadsPath'], 0, 2);
13
+ add_filter('webpc_uploads_webp', [$this, 'getOutputPath'], 0, 2);
14
+ add_filter('webpc_uploads_path', [$this, 'parsePath'], 100, 2);
15
+ add_filter('webpc_uploads_webp', [$this, 'parsePath'], 100, 2);
16
+ add_filter('webpc_uploads_prefix', [$this, 'getPrefixPath'], 0);
17
+ add_filter('webpc_uploads_dir', [$this, 'getUploadsDir'], 0);
18
  }
19
 
20
  /* ---
21
  Functions
22
  --- */
23
 
24
+ public function getUploadsPath($value, $skipRoot = false)
25
  {
26
+ return $this->pathUploads;
27
  }
28
 
29
  public function getOutputPath($value, $skipRoot = false)
48
 
49
  return str_replace('//', '/', $diffPath);
50
  }
51
+
52
+ public function getUploadsDir($value)
53
+ {
54
+ $uploadsDir = apply_filters('webpc_uploads_path', '');
55
+ $parentDir = dirname(apply_filters('webpc_uploads_webp', ''));
56
+ if ((!$uploadsDir = realpath($uploadsDir)) || (!$parentDir = realpath($parentDir))) {
57
+ return $value;
58
+ }
59
+
60
+ $path = str_replace($parentDir, '', $uploadsDir);
61
+ return trim($path, '\/');
62
+ }
63
  }
readme.txt CHANGED
@@ -32,6 +32,7 @@ This will be a profit both for your users who will not have to download so much
32
  - **The plugin does not change image URLs, so there are no problems with saving the HTML code of website to the cache and time of its generation does not increase.** It does not matter if the image display as an `img` HTML tag or you use `background-image`. It works always!
33
  - The name of the loaded image does not contain the WebP extension. Only the source of the loaded file changes to a WebP file. As a result, you always have one URL to a file. Regardless of whether the browser supports WebP or not.
34
  - Image URLs are modified using the module `mod_rewrite` on the server, i.e. the same, and thanks to this we can use friendly links in WordPress. Additionally, the MIME type of the sent file is modified to `image/webp`.
 
35
  - The final result is that your users download less than half of the data, and the website itself loads faster!
36
 
37
  #### WebP images are the future!
@@ -110,7 +111,7 @@ Send a screenshot from console if an error occurred while converting images. Of
110
 
111
  **9.** Used Wordpress version.
112
 
113
- **10.** A list of all the plugins you use. Have you tried checking the plugin operation by turning off all others? If not, please try whenever possible.
114
 
115
  Please remember to include the answers for all 10 questions by adding a thread. It is much easier and accelerate the solution of your problem.
116
 
@@ -126,6 +127,18 @@ When contacting the administrator, give him all the information available in the
126
 
127
  We want to solve as many similar problems as possible automatically. This eliminates the need to wait for our response and you can try to solve the problem alone. This is very important for us.
128
 
 
 
 
 
 
 
 
 
 
 
 
 
129
  = What are requirements of plugin? =
130
 
131
  Practically every hosting meets these requirements. You must use PHP at least 7.0 and have the `GD` or `Imagick` extension installed. **The extension must support `WebP format`.**
@@ -207,9 +220,9 @@ When checking the operation of the plugin, e.g. in Dev Tools, pay attention to t
207
 
208
  = Where are converted images stored? =
209
 
210
- All WebP images are stored in the `/wp-content/uploads-webpc/` directory. Inside the directory there is the same structure as in the original `uploads` directory. The files have original extensions in the name along with the new `.webp`.
211
 
212
- In case the location of the original file is as follows: `/wp-content/uploads/2019/06/example.jpg` then its converted version will be in the following location: `/wp-content/uploads-webpc/2019/06/example.jpg.webp`.
213
 
214
  Original images are not changed. If you remove plugins, only WebP files will be deleted. Your images are not changed in any way.
215
 
@@ -376,7 +389,7 @@ and add below code in this file:
376
  `server {`
377
  ` # ...
378
 
379
- location ~ /wp-content/uploads/(?<path>.+)\.(?<ext>jpe?g|png|gif)$ {
380
  if ($http_accept !~* "image/webp") {
381
  break;
382
  }
@@ -390,7 +403,7 @@ and add below code in this file:
390
 
391
  Yes, with one exception. In this mode it is not possible to automatically generate the contents of .htaccess files.
392
 
393
- Please manually paste the following code **at the beginning of .htaccess file** in the `/wp-content/uploads` directory:
394
 
395
  `# BEGIN WebP Converter`
396
  `# ! --- DO NOT EDIT PREVIOUS LINE --- !`
@@ -442,6 +455,10 @@ This is all very important to us and allows us to do even better things for you!
442
 
443
  == Changelog ==
444
 
 
 
 
 
445
  = 1.2.6 (2020-05-28) =
446
  * `[Fixed]` Removal of WebP files larger than original during upload
447
 
32
  - **The plugin does not change image URLs, so there are no problems with saving the HTML code of website to the cache and time of its generation does not increase.** It does not matter if the image display as an `img` HTML tag or you use `background-image`. It works always!
33
  - The name of the loaded image does not contain the WebP extension. Only the source of the loaded file changes to a WebP file. As a result, you always have one URL to a file. Regardless of whether the browser supports WebP or not.
34
  - Image URLs are modified using the module `mod_rewrite` on the server, i.e. the same, and thanks to this we can use friendly links in WordPress. Additionally, the MIME type of the sent file is modified to `image/webp`.
35
+ - When you try to save the image to disk *(e.g. by clicking Save as...)* the original image will be saved. WebP is only used for browser loading.
36
  - The final result is that your users download less than half of the data, and the website itself loads faster!
37
 
38
  #### WebP images are the future!
111
 
112
  **9.** Used Wordpress version.
113
 
114
+ **10.** A list of all the plugins you use. Have you tried checking the plugin operation by turning off all others and activating the default theme? If not, please try whenever possible. **This is very important because other plugins or themes can cause problems. Therefore, we recommend disabling all necessary plugins and enabling the default theme.**
115
 
116
  Please remember to include the answers for all 10 questions by adding a thread. It is much easier and accelerate the solution of your problem.
117
 
127
 
128
  We want to solve as many similar problems as possible automatically. This eliminates the need to wait for our response and you can try to solve the problem alone. This is very important for us.
129
 
130
+ = Error while converting? =
131
+
132
+ You can get several types of errors when converting. First of all, carefully read their content. For the most part, you can solve this problem yourself. Try to do this or contact the server administrator.
133
+
134
+ If you get an error: `File "%s" does not exist. Please check file path.` means that the [file_exists()](https://www.php.net/manual/en/function.file-exists.php) function in PHP returned `false` using the file path given in the error message. Check this path and make sure it is correct.
135
+
136
+ If you get an error: `File "%s" is unreadable. Please check file permissions.` means that the [is_readable()](https://www.php.net/manual/en/function.is-readable.php) function in PHP returned `false` using the file path given in the error message. Check the permissions for the file and the directory in which the file is located.
137
+
138
+ If you get an error: `"%s" is not a valid image file.` means that the file is damaged in some way. Download the file to disk, save it again using any graphics program and add again to the page. If the error applies to individual images then you can ignore it - just the original images will load, not WebP.
139
+
140
+ Remember that it happens that other plugins can cause problems with accessing files or the REST API. Please try to disable all other plugins and set the default theme to make sure that it is not one of them that causes these types of problems.
141
+
142
  = What are requirements of plugin? =
143
 
144
  Practically every hosting meets these requirements. You must use PHP at least 7.0 and have the `GD` or `Imagick` extension installed. **The extension must support `WebP format`.**
220
 
221
  = Where are converted images stored? =
222
 
223
+ All WebP images are stored in the `/wp-content/uploads-webpc/` directory. Inside the directory there is the same structure as in the original `/wp-content` directory. The files have original extensions in the name along with the new `.webp`.
224
 
225
+ In case the location of the original file is as follows: `/wp-content/uploads/2019/06/example.jpg` then its converted version will be in the following location: `/wp-content/uploads-webpc/uploads/2019/06/example.jpg.webp`.
226
 
227
  Original images are not changed. If you remove plugins, only WebP files will be deleted. Your images are not changed in any way.
228
 
389
  `server {`
390
  ` # ...
391
 
392
+ location ~ /wp-content/(?<path>.+)\.(?<ext>jpe?g|png|gif)$ {
393
  if ($http_accept !~* "image/webp") {
394
  break;
395
  }
403
 
404
  Yes, with one exception. In this mode it is not possible to automatically generate the contents of .htaccess files.
405
 
406
+ Please manually paste the following code **at the beginning of .htaccess file** in the `/wp-content` directory:
407
 
408
  `# BEGIN WebP Converter`
409
  `# ! --- DO NOT EDIT PREVIOUS LINE --- !`
455
 
456
  == Changelog ==
457
 
458
+ = 1.2.7 (2020-06-11) =
459
+ * `[Changed]` Moving converted WebP files to `/uploads-webpc/uploads` directory from `/uploads-webpc` directory *(**required manual configuration change for Nginx and WordPress Multisite**)*
460
+ * `[Changed]` Validation when converting images
461
+
462
  = 1.2.6 (2020-05-28) =
463
  * `[Fixed]` Removal of WebP files larger than original during upload
464
 
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.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__));
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.7
7
  Author: Mateusz Gbiorczyk
8
  Author URI: https://gbiorczyk.pl/
9
  Text Domain: webp-converter
10
  */
11
 
12
+ define('WEBPC_VERSION', '1.2.7');
13
  define('WEBPC_FILE', __FILE__);
14
  define('WEBPC_NAME', plugin_basename(__FILE__));
15
  define('WEBPC_PATH', plugin_dir_path(__FILE__));