| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Classes\Image; |
| 4 |
|
| 5 |
use ImageOptimization\Modules\Settings\Classes\Settings; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Image_Conversion { |
| 12 |
protected ?string $convert_to_format; |
| 13 |
protected array $options; |
| 14 |
|
| 15 |
public function is_enabled(): bool { |
| 16 |
return Image_Conversion_Option::ORIGINAL !== $this->convert_to_format; |
| 17 |
} |
| 18 |
|
| 19 |
public function get_current_conversion_option(): string { |
| 20 |
return $this->convert_to_format; |
| 21 |
} |
| 22 |
|
| 23 |
public function get_current_file_extension(): ?string { |
| 24 |
if ( ! $this->is_enabled() ) { |
| 25 |
return null; |
| 26 |
} |
| 27 |
|
| 28 |
return $this->options[ $this->convert_to_format ]['extension']; |
| 29 |
} |
| 30 |
|
| 31 |
public function get_current_mime_type(): ?string { |
| 32 |
if ( ! $this->is_enabled() ) { |
| 33 |
return null; |
| 34 |
} |
| 35 |
|
| 36 |
return $this->options[ $this->convert_to_format ]['mime_type']; |
| 37 |
} |
| 38 |
|
| 39 |
public function __construct() { |
| 40 |
$this->convert_to_format = Settings::get( Settings::CONVERT_TO_FORMAT_OPTION_NAME ); |
| 41 |
|
| 42 |
$this->options = [ |
| 43 |
Image_Conversion_Option::WEBP => [ |
| 44 |
'extension' => 'webp', |
| 45 |
'mime_type' => 'image/webp', |
| 46 |
], |
| 47 |
Image_Conversion_Option::AVIF => [ |
| 48 |
'extension' => 'avif', |
| 49 |
'mime_type' => 'image/avif', |
| 50 |
], |
| 51 |
]; |
| 52 |
} |
| 53 |
} |
| 54 |
|