| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Classes\Image; |
| 4 |
|
| 5 |
use ImageOptimization\Classes\File_Utils; |
| 6 |
use ImageOptimization\Classes\Image\Exceptions\Invalid_Image_Exception; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Class WP_Image_Meta |
| 14 |
* |
| 15 |
* This class is used to manage the metadata of an image in WordPress. |
| 16 |
*/ |
| 17 |
class WP_Image_Meta { |
| 18 |
private int $image_id; |
| 19 |
private array $image_meta; |
| 20 |
private Image $image; |
| 21 |
|
| 22 |
/** |
| 23 |
* Get the size data of the image. |
| 24 |
* |
| 25 |
* @param string $image_size The size of the image. |
| 26 |
* @return array|null The size data of the image or null if the size does not exist. |
| 27 |
*/ |
| 28 |
public function get_size_data( string $image_size ): ?array { |
| 29 |
if ( Image::SIZE_FULL === $image_size ) { |
| 30 |
$output = []; |
| 31 |
|
| 32 |
$output['file'] = $this->image_meta['file']; |
| 33 |
$output['filesize'] = $this->image_meta['filesize']; |
| 34 |
$output['width'] = $this->image_meta['width']; |
| 35 |
$output['height'] = $this->image_meta['height']; |
| 36 |
$output['mime-type'] = $this->image->get_attachment_object()->post_mime_type; |
| 37 |
|
| 38 |
return $output; |
| 39 |
} |
| 40 |
|
| 41 |
if ( ! isset( $this->image_meta['sizes'][ $image_size ] ) ) { |
| 42 |
return null; |
| 43 |
} |
| 44 |
|
| 45 |
return $this->image_meta['sizes'][ $image_size ]; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get the keys of the image sizes. |
| 50 |
* |
| 51 |
* @return array The keys of the image sizes. |
| 52 |
*/ |
| 53 |
public function get_size_keys(): array { |
| 54 |
return [ Image::SIZE_FULL, ...array_keys( $this->image_meta['sizes'] ) ]; |
| 55 |
} |
| 56 |
|
| 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 |
/** |
| 80 |
* Get the file size of the image. |
| 81 |
* |
| 82 |
* @param string $image_size The size of the image. |
| 83 |
* @return int|null The file size of the image or null if the size does not exist. |
| 84 |
*/ |
| 85 |
public function get_file_size( string $image_size ): ?int { |
| 86 |
if ( Image::SIZE_FULL === $image_size ) { |
| 87 |
return $this->image_meta['filesize'] ?? null; |
| 88 |
} |
| 89 |
|
| 90 |
if ( ! isset( $this->image_meta['sizes'][ $image_size ]['filesize'] ) ) { |
| 91 |
return null; |
| 92 |
} |
| 93 |
|
| 94 |
return $this->image_meta['sizes'][ $image_size ]['filesize']; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Set the file size of the image. |
| 99 |
* |
| 100 |
* @param string $image_size The size of the image. |
| 101 |
* @param int $file_size The file size to set. |
| 102 |
* @return WP_Image_Meta The current instance. |
| 103 |
*/ |
| 104 |
public function set_file_size( string $image_size, int $file_size ): WP_Image_Meta { |
| 105 |
if ( Image::SIZE_FULL === $image_size ) { |
| 106 |
$this->image_meta['filesize'] = $file_size; |
| 107 |
return $this; |
| 108 |
} |
| 109 |
|
| 110 |
$this->maybe_add_new_size( $image_size ); |
| 111 |
|
| 112 |
$this->image_meta['sizes'][ $image_size ]['filesize'] = $file_size; |
| 113 |
|
| 114 |
return $this; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Set the file path of the image. |
| 119 |
* |
| 120 |
* @param string $image_size The size of the image. |
| 121 |
* @param string $full_path The full path to set. |
| 122 |
* @return WP_Image_Meta The current instance. |
| 123 |
*/ |
| 124 |
public function set_file_path( string $image_size, string $full_path ): WP_Image_Meta { |
| 125 |
if ( Image::SIZE_FULL === $image_size ) { |
| 126 |
$this->image_meta['file'] = File_Utils::get_relative_upload_path( $full_path ); |
| 127 |
return $this; |
| 128 |
} |
| 129 |
|
| 130 |
$this->maybe_add_new_size( $image_size ); |
| 131 |
|
| 132 |
$this->image_meta['sizes'][ $image_size ]['file'] = File_Utils::get_basename( $full_path ); |
| 133 |
|
| 134 |
return $this; |
| 135 |
} |
| 136 |
|
| 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 |
/** |
| 157 |
* Set the width of the image. |
| 158 |
* |
| 159 |
* @param string $image_size The size of the image. |
| 160 |
* @param int $width The width to set. |
| 161 |
* @return WP_Image_Meta The current instance. |
| 162 |
*/ |
| 163 |
public function set_width( string $image_size, int $width ): WP_Image_Meta { |
| 164 |
if ( Image::SIZE_FULL === $image_size ) { |
| 165 |
$this->image_meta['width'] = $width; |
| 166 |
return $this; |
| 167 |
} |
| 168 |
|
| 169 |
$this->maybe_add_new_size( $image_size ); |
| 170 |
|
| 171 |
$this->image_meta['sizes'][ $image_size ]['width'] = $width; |
| 172 |
|
| 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']; |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* Set the height of the image. |
| 197 |
* |
| 198 |
* @param string $image_size The size of the image. |
| 199 |
* @param int $height The height to set. |
| 200 |
* @return WP_Image_Meta The current instance. |
| 201 |
*/ |
| 202 |
public function set_height( string $image_size, int $height ): WP_Image_Meta { |
| 203 |
if ( Image::SIZE_FULL === $image_size ) { |
| 204 |
$this->image_meta['height'] = $height; |
| 205 |
return $this; |
| 206 |
} |
| 207 |
|
| 208 |
$this->maybe_add_new_size( $image_size ); |
| 209 |
|
| 210 |
$this->image_meta['sizes'][ $image_size ]['height'] = $height; |
| 211 |
|
| 212 |
return $this; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Set the mime type of the image. |
| 217 |
* |
| 218 |
* @param string $image_size The size of the image. |
| 219 |
* @param string $mime_type The mime type to set. |
| 220 |
* @return WP_Image_Meta The current instance. |
| 221 |
*/ |
| 222 |
public function set_mime_type( string $image_size, string $mime_type ): WP_Image_Meta { |
| 223 |
if ( Image::SIZE_FULL === $image_size ) { |
| 224 |
// WP doesn't store it in meta for the original image |
| 225 |
return $this; |
| 226 |
} |
| 227 |
|
| 228 |
$this->maybe_add_new_size( $image_size ); |
| 229 |
|
| 230 |
$this->image_meta['sizes'][ $image_size ]['mime-type'] = $mime_type; |
| 231 |
|
| 232 |
return $this; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Add a new size to the image if it does not exist. |
| 237 |
* |
| 238 |
* @param string $image_size The size of the image. |
| 239 |
*/ |
| 240 |
private function maybe_add_new_size( $image_size ): void { |
| 241 |
if ( ! isset( $this->image_meta['sizes'][ $image_size ] ) ) { |
| 242 |
$this->image_meta['sizes'][ $image_size ] = []; |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
/** |
| 247 |
* Save the image metadata. |
| 248 |
* |
| 249 |
* @return WP_Image_Meta The current instance. |
| 250 |
*/ |
| 251 |
public function save(): WP_Image_Meta { |
| 252 |
wp_update_attachment_metadata( $this->image_id, $this->image_meta ); |
| 253 |
|
| 254 |
return $this; |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Query the image metadata. |
| 259 |
* |
| 260 |
* @throws Invalid_Image_Exception |
| 261 |
*/ |
| 262 |
private function query_meta(): void { |
| 263 |
$meta = wp_get_attachment_metadata( $this->image_id ); |
| 264 |
|
| 265 |
if ( ! $meta ) { |
| 266 |
throw new Invalid_Image_Exception( 'Invalid WP image meta' ); |
| 267 |
} |
| 268 |
|
| 269 |
// Handle unsupported formats that WP doesn't create thumbnails for |
| 270 |
if ( ! isset( $meta['sizes'] ) ) { |
| 271 |
$meta['sizes'] = []; |
| 272 |
} |
| 273 |
|
| 274 |
$this->image_meta = $meta; |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* WP_Image_Meta constructor. |
| 279 |
* |
| 280 |
* @param int $image_id The ID of the image. |
| 281 |
* @param Image|null $image The image object. |
| 282 |
* @throws Invalid_Image_Exception If the image metadata is invalid. |
| 283 |
*/ |
| 284 |
public function __construct( int $image_id, ?Image $image = null ) { |
| 285 |
$this->image_id = $image_id; |
| 286 |
$this->image = $image ?? new Image( $image_id ); |
| 287 |
|
| 288 |
$this->query_meta(); |
| 289 |
} |
| 290 |
} |
| 291 |
|