PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 3.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v3.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / webp / class-wpo-webp-utils.php

class-wpo-webp-utils.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 3.7.0, at webp/class-wpo-webp-utils.php

88 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('WPO_VERSION')) die('No direct access allowed');
4
5 use \WebPConvert\Convert\ConverterFactory;
6
7 require_once WPO_PLUGIN_MAIN_PATH . 'vendor/autoload.php';
8
9 if (!class_exists('WPO_WebP_Utils')) :
10
11 class WPO_WebP_Utils {
12
13 /**
14 * Determines whether we can do webp conversion or not
15 *
16 * @return bool
17 */
18 public static function can_do_webp_conversion() {
19 $webp_conversion = WP_Optimize()->get_options()->get_option('webp_conversion', false);
20 $webp_converters = WP_Optimize()->get_options()->get_option('webp_converters', false);
21 return $webp_conversion && !empty($webp_converters);
22 }
23
24 /**
25 * Convert given image file to webp format
26 *
27 * @param string $source Path of image file
28 *
29 * @return void
30 */
31 public static function do_webp_conversion($source) {
32 $webp_converter = new WPO_WebP_Convert();
33 $webp_converter->convert($source);
34 }
35
36 /**
37 * Converts an image to WebP format using a specified converter.
38 *
39 * This method acts as a wrapper for the `WebPConvert\Convert\Converters\AbstractConverter::doConvert()` method
40 * from the `webp-convert` library.
41 *
42 * @param string $converter The converter to be used for the conversion process.
43 * @param string $source The path to the source image file.
44 * @param string $destination The path to the destination WebP image file.
45 */
46 public static function perform_webp_conversion($converter, $source, $destination) {
47 $converter_instance = ConverterFactory::makeConverter(
48 $converter,
49 $source,
50 $destination
51 );
52
53 set_error_handler(array(__CLASS__, 'handle_webp_conversion_warnings'), E_WARNING);
54
55 $converter_instance->doConvert();
56
57 restore_error_handler();
58 }
59
60 /**
61 * Custom error handler for handling PHP warnings during the WebP conversion process.
62 *
63 * @param int $errno The level of the error raised, as an integer.
64 * @param string $errstr The error message.
65 *
66 * @return bool
67 */
68 public static function handle_webp_conversion_warnings($errno, $errstr) {
69 $patterns = array(
70 '/unlink\(.+\): No such file or directory/',
71 '/rename\(.+\): No such file or directory/',
72 '/filesize\(\): stat failed for/',
73 );
74
75 foreach ($patterns as $pattern) {
76 if (preg_match($pattern, $errstr)) {
77 // Suppress the warning by returning true
78 return true;
79 }
80 }
81
82 // For other PHP warnings, use the default PHP error handler by returning false
83 return false;
84 }
85 }
86
87 endif;
88