PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.0.0
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.0.0
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 / wp-image-meta.php

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

231 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimizer\Classes\Image;
4
5 use ImageOptimizer\Classes\File_Utils;
6 use ImageOptimizer\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 * Get the file size of the image.
59 *
60 * @param string $image_size The size of the image.
61 * @return int|null The file size of the image or null if the size does not exist.
62 */
63 public function get_file_size( string $image_size ): ?int {
64 if ( Image::SIZE_FULL === $image_size ) {
65 return $this->image_meta['filesize'];
66 }
67
68 if ( ! isset( $this->image_meta['sizes'][ $image_size ]['filesize'] ) ) {
69 return null;
70 }
71
72 return $this->image_meta['sizes'][ $image_size ]['filesize'];
73 }
74
75 /**
76 * Set the file size of the image.
77 *
78 * @param string $image_size The size of the image.
79 * @param int $file_size The file size to set.
80 * @return WP_Image_Meta The current instance.
81 */
82 public function set_file_size( string $image_size, int $file_size ): WP_Image_Meta {
83 if ( Image::SIZE_FULL === $image_size ) {
84 $this->image_meta['filesize'] = $file_size;
85 return $this;
86 }
87
88 $this->maybe_add_new_size( $image_size );
89
90 $this->image_meta['sizes'][ $image_size ]['filesize'] = $file_size;
91
92 return $this;
93 }
94
95 /**
96 * Set the file path of the image.
97 *
98 * @param string $image_size The size of the image.
99 * @param string $full_path The full path to set.
100 * @return WP_Image_Meta The current instance.
101 */
102 public function set_file_path( string $image_size, string $full_path ): WP_Image_Meta {
103 if ( Image::SIZE_FULL === $image_size ) {
104 $this->image_meta['file'] = File_Utils::get_relative_upload_path( $full_path );
105 return $this;
106 }
107
108 $this->maybe_add_new_size( $image_size );
109
110 $this->image_meta['sizes'][ $image_size ]['file'] = File_Utils::get_basename( $full_path );
111
112 return $this;
113 }
114
115 /**
116 * Set the width of the image.
117 *
118 * @param string $image_size The size of the image.
119 * @param int $width The width to set.
120 * @return WP_Image_Meta The current instance.
121 */
122 public function set_width( string $image_size, int $width ): WP_Image_Meta {
123 if ( Image::SIZE_FULL === $image_size ) {
124 $this->image_meta['width'] = $width;
125 return $this;
126 }
127
128 $this->maybe_add_new_size( $image_size );
129
130 $this->image_meta['sizes'][ $image_size ]['width'] = $width;
131
132 return $this;
133 }
134
135 /**
136 * Set the height of the image.
137 *
138 * @param string $image_size The size of the image.
139 * @param int $height The height to set.
140 * @return WP_Image_Meta The current instance.
141 */
142 public function set_height( string $image_size, int $height ): WP_Image_Meta {
143 if ( Image::SIZE_FULL === $image_size ) {
144 $this->image_meta['height'] = $height;
145 return $this;
146 }
147
148 $this->maybe_add_new_size( $image_size );
149
150 $this->image_meta['sizes'][ $image_size ]['height'] = $height;
151
152 return $this;
153 }
154
155 /**
156 * Set the mime type of the image.
157 *
158 * @param string $image_size The size of the image.
159 * @param string $mime_type The mime type to set.
160 * @return WP_Image_Meta The current instance.
161 */
162 public function set_mime_type( string $image_size, string $mime_type ): WP_Image_Meta {
163 if ( Image::SIZE_FULL === $image_size ) {
164 // WP doesn't store it in meta for the original image
165 return $this;
166 }
167
168 $this->maybe_add_new_size( $image_size );
169
170 $this->image_meta['sizes'][ $image_size ]['mime-type'] = $mime_type;
171
172 return $this;
173 }
174
175 /**
176 * Add a new size to the image if it does not exist.
177 *
178 * @param string $image_size The size of the image.
179 */
180 private function maybe_add_new_size( $image_size ): void {
181 if ( ! isset( $this->image_meta['sizes'][ $image_size ] ) ) {
182 $this->image_meta['sizes'][ $image_size ] = [];
183 }
184 }
185
186 /**
187 * Save the image metadata.
188 *
189 * @return WP_Image_Meta The current instance.
190 */
191 public function save(): WP_Image_Meta {
192 wp_update_attachment_metadata( $this->image_id, $this->image_meta );
193
194 return $this;
195 }
196
197 /**
198 * Query the image metadata.
199 *
200 * @throws Invalid_Image_Exception
201 */
202 private function query_meta(): void {
203 $meta = wp_get_attachment_metadata( $this->image_id );
204
205 if ( ! $meta ) {
206 throw new Invalid_Image_Exception( 'Invalid WP image meta' );
207 }
208
209 // Handle unsupported formats that WP doesn't create thumbnails for
210 if ( ! isset( $meta['sizes'] ) ) {
211 $meta['sizes'] = [];
212 }
213
214 $this->image_meta = $meta;
215 }
216
217 /**
218 * WP_Image_Meta constructor.
219 *
220 * @param int $image_id The ID of the image.
221 * @param Image|null $image The image object.
222 * @throws Invalid_Image_Exception If the image metadata is invalid.
223 */
224 public function __construct( int $image_id, ?Image $image = null ) {
225 $this->image_id = $image_id;
226 $this->image = $image ?? new Image( $image_id );
227
228 $this->query_meta();
229 }
230 }
231