| @@ -1,10 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace ImageOptimizer\Classes\Image; | |
| 3 | +namespace ImageOptimization\Classes\Image; | |
| 4 | 4 | |
| 5 | -use ImageOptimizer\Classes\File_Utils; | |
| 6 | -use ImageOptimizer\Classes\Image\Exceptions\Invalid_Image_Exception; | |
| 5 | +use ImageOptimization\Classes\File_Utils; | |
| 6 | +use ImageOptimization\Classes\Image\Exceptions\Invalid_Image_Exception; | |
| 7 | 7 | |
| 8 | 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | 9 | exit; // Exit if accessed directly. |
| 10 | 10 | } |
| @@ -54,8 +54,30 @@ | ||
| 54 | 54 | return [ Image::SIZE_FULL, ...array_keys( $this->image_meta['sizes'] ) ]; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | + * Returns keys of sizes that have the same dimensions as the one provided. | |
| 59 | + * | |
| 60 | + * @param string $image_size The size of the image. | |
| 61 | + * | |
| 62 | + * @return array | |
| 63 | + */ | |
| 64 | + public function get_size_duplicates( string $image_size ): array { | |
| 65 | + $duplicates = []; | |
| 66 | + | |
| 67 | + $width = $this->get_width( $image_size ); | |
| 68 | + $height = $this->get_height( $image_size ); | |
| 69 | + | |
| 70 | + foreach ( $this->image_meta['sizes'] as $size_key => $size_data ) { | |
| 71 | + if ( $size_data['width'] === $width && $size_data['height'] === $height && $image_size !== $size_key ) { | |
| 72 | + $duplicates[] = $size_key; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + | |
| 76 | + return array_unique( $duplicates ); | |
| 77 | + } | |
| 78 | + | |
| 79 | + /** | |
| 58 | 80 | * Get the file size of the image. |
| 59 | 81 | * |
| 60 | 82 | * @param string $image_size The size of the image. |
| 61 | 83 | * @return int|null The file size of the image or null if the size does not exist. |
| @@ -61,9 +83,9 @@ | ||
| 61 | 83 | * @return int|null The file size of the image or null if the size does not exist. |
| 62 | 84 | */ |
| 63 | 85 | public function get_file_size( string $image_size ): ?int { |
| 64 | 86 | if ( Image::SIZE_FULL === $image_size ) { |
| 65 | - return $this->image_meta['filesize']; | |
| 87 | + return $this->image_meta['filesize'] ?? null; | |
| 66 | 88 | } |
| 67 | 89 | |
| 68 | 90 | if ( ! isset( $this->image_meta['sizes'][ $image_size ]['filesize'] ) ) { |
| 69 | 91 | return null; |
| @@ -112,8 +134,27 @@ | ||
| 112 | 134 | return $this; |
| 113 | 135 | } |
| 114 | 136 | |
| 115 | 137 | /** |
| 138 | + * Get the width of the image. | |
| 139 | + * | |
| 140 | + * @param string $image_size The size of the image. | |
| 141 | + * | |
| 142 | + * @return int|null | |
| 143 | + */ | |
| 144 | + public function get_width( string $image_size ): ?int { | |
| 145 | + if ( Image::SIZE_FULL === $image_size ) { | |
| 146 | + return $this->image_meta['width']; | |
| 147 | + } | |
| 148 | + | |
| 149 | + if ( ! isset( $this->image_meta['sizes'][ $image_size ]['width'] ) ) { | |
| 150 | + return null; | |
| 151 | + } | |
| 152 | + | |
| 153 | + return $this->image_meta['sizes'][ $image_size ]['width']; | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 116 | 157 | * Set the width of the image. |
| 117 | 158 | * |
| 118 | 159 | * @param string $image_size The size of the image. |
| 119 | 160 | * @param int $width The width to set. |
| @@ -129,8 +170,27 @@ | ||
| 129 | 170 | |
| 130 | 171 | $this->image_meta['sizes'][ $image_size ]['width'] = $width; |
| 131 | 172 | |
| 132 | 173 | return $this; |
| 174 | + } | |
| 175 | + | |
| 176 | + /** | |
| 177 | + * Get the height of the image. | |
| 178 | + * | |
| 179 | + * @param string $image_size The size of the image. | |
| 180 | + * | |
| 181 | + * @return int|null | |
| 182 | + */ | |
| 183 | + public function get_height( string $image_size ): ?int { | |
| 184 | + if ( Image::SIZE_FULL === $image_size ) { | |
| 185 | + return $this->image_meta['height']; | |
| 186 | + } | |
| 187 | + | |
| 188 | + if ( ! isset( $this->image_meta['sizes'][ $image_size ]['height'] ) ) { | |
| 189 | + return null; | |
| 190 | + } | |
| 191 | + | |
| 192 | + return $this->image_meta['sizes'][ $image_size ]['height']; | |
| 133 | 193 | } |
| 134 | 194 | |
| 135 | 195 | /** |
| 136 | 196 | * Set the height of the image. |