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