PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.3
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.3
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / classes / image / image-dimensions.php

image-dimensions.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.3, at classes/image/image-dimensions.php

54 lines 1.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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