| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('WPO_VERSION')) die('No direct access allowed'); |
| 4 |
use \WebPConvert\Convert\ConverterFactory; |
| 5 |
|
| 6 |
require_once(WPO_PLUGIN_MAIN_PATH . 'vendor/autoload.php'); |
| 7 |
require_once(WPO_PLUGIN_MAIN_PATH . 'webp/class-wpo-webp-test-run.php'); |
| 8 |
|
| 9 |
if (!class_exists('WPO_WebP_Convert')) : |
| 10 |
|
| 11 |
class WPO_WebP_Convert { |
| 12 |
|
| 13 |
public $converters = null; |
| 14 |
|
| 15 |
public function __construct() { |
| 16 |
$this->converters = WP_Optimize()->get_options()->get_option('webp_converters'); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Converts uploaded image to webp format |
| 21 |
* |
| 22 |
* @param string $source - path of the source file |
| 23 |
*/ |
| 24 |
public function convert($source) { |
| 25 |
if (count($this->converters) < 1) return false; |
| 26 |
|
| 27 |
$destination = $this->get_destination_path($source); |
| 28 |
$this->check_converters_and_do_conversion($source, $destination); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Returns the destination full path |
| 33 |
* |
| 34 |
* @param string $source - path of the source file |
| 35 |
* |
| 36 |
* @return string $destination - path of destination file |
| 37 |
*/ |
| 38 |
protected function get_destination_path($source) { |
| 39 |
$path_parts = pathinfo($source); |
| 40 |
$destination = $path_parts['dirname'] . '/'. basename($source) . '.webp'; |
| 41 |
return $destination; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Loop through available converters and do the conversion |
| 46 |
* |
| 47 |
* @param string $source - path of source file |
| 48 |
* @param string $destination - path of destination file |
| 49 |
*/ |
| 50 |
protected function check_converters_and_do_conversion($source, $destination) { |
| 51 |
foreach ($this->converters as $converter) { |
| 52 |
$converter_instance = ConverterFactory::makeConverter( |
| 53 |
$converter, |
| 54 |
$source, |
| 55 |
$destination |
| 56 |
); |
| 57 |
$converter_instance->doConvert(); |
| 58 |
break; |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
endif; |
| 63 |
|