| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Classes\Image; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\Logger; |
| 6 |
|
| 7 |
use Imagick; |
| 8 |
use stdClass; |
| 9 |
use Throwable; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
class Image_Dimensions { |
| 16 |
/** |
| 17 |
* @param string $file_path |
| 18 |
* |
| 19 |
* @return stdClass{width: int, height: int} |
| 20 |
*/ |
| 21 |
public static function get_by_path( string $file_path ): stdClass { |
| 22 |
$dimensions = wp_getimagesize( $file_path ); |
| 23 |
$output = new stdClass(); |
| 24 |
|
| 25 |
$output->width = 0; |
| 26 |
$output->height = 0; |
| 27 |
|
| 28 |
if ( $dimensions ) { |
| 29 |
$output->width = $dimensions[0]; |
| 30 |
$output->height = $dimensions[1]; |
| 31 |
|
| 32 |
return $output; |
| 33 |
} |
| 34 |
|
| 35 |
if ( class_exists( 'Imagick' ) ) { |
| 36 |
try { |
| 37 |
$im = new Imagick( $file_path ); |
| 38 |
$image_geometry = $im->getImageGeometry(); |
| 39 |
$im->clear(); |
| 40 |
|
| 41 |
$output->width = $image_geometry['width']; |
| 42 |
$output->height = $image_geometry['height']; |
| 43 |
} catch ( Throwable $t ) { |
| 44 |
Logger::log( |
| 45 |
Logger::LEVEL_ERROR, |
| 46 |
'AVIF image dimensions calculation error: ' . $t->getMessage() |
| 47 |
); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
return $output; |
| 52 |
} |
| 53 |
} |
| 54 |
|