1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
---|
2 | /** |
---|
3 | * The Image Class. |
---|
4 | * |
---|
5 | * @package automattic/jetpack-image-cdn |
---|
6 | */ |
---|
7 | |
---|
8 | namespace Automattic\Jetpack\Image_CDN; |
---|
9 | |
---|
10 | /** |
---|
11 | * Represents a resizable image, exposing properties necessary for properly generating srcset. |
---|
12 | */ |
---|
13 | class Image_CDN_Image { |
---|
14 | |
---|
15 | /** |
---|
16 | * Attachment's Filename. |
---|
17 | * |
---|
18 | * @var string |
---|
19 | */ |
---|
20 | public $filename; |
---|
21 | |
---|
22 | /** |
---|
23 | * Attachment's mime-type, WP_Error on failure when recalculating the dimensions. |
---|
24 | * |
---|
25 | * @var string|WP_Error |
---|
26 | */ |
---|
27 | private $mime_type; |
---|
28 | |
---|
29 | /** |
---|
30 | * Image original width. |
---|
31 | * |
---|
32 | * @var int |
---|
33 | */ |
---|
34 | private $original_width; |
---|
35 | |
---|
36 | /** |
---|
37 | * Image original height. |
---|
38 | * |
---|
39 | * @var int |
---|
40 | */ |
---|
41 | private $original_height; |
---|
42 | |
---|
43 | /** |
---|
44 | * Current attachment's width. |
---|
45 | * |
---|
46 | * @var int |
---|
47 | */ |
---|
48 | private $width; |
---|
49 | |
---|
50 | /** |
---|
51 | * Current attachment's height. |
---|
52 | * |
---|
53 | * @var int |
---|
54 | */ |
---|
55 | private $height; |
---|
56 | |
---|
57 | /** |
---|
58 | * Whether the attachment has been resized yet, or not. |
---|
59 | * |
---|
60 | * @var bool |
---|
61 | */ |
---|
62 | private $is_resized = false; |
---|
63 | |
---|
64 | /** |
---|
65 | * Constructs the image object. |
---|
66 | * |
---|
67 | * The $data array should provide at least |
---|
68 | * file : string Image file path |
---|
69 | * width : int Image width |
---|
70 | * height : int Image height |
---|
71 | * |
---|
72 | * @param array $data Array of attachment metadata, typically value of _wp_attachment_metadata postmeta. |
---|
73 | * @param string|\WP_Error $mime_type Typically value returned from get_post_mime_type function. |
---|
74 | */ |
---|
75 | public function __construct( $data, $mime_type ) { |
---|
76 | $this->filename = $data['file']; |
---|
77 | $this->original_width = $data['width']; |
---|
78 | $this->original_height = $data['height']; |
---|
79 | $this->width = $this->original_width; |
---|
80 | $this->height = $this->original_height; |
---|
81 | $this->mime_type = $mime_type; |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * Resizes the image to given size. |
---|
86 | * |
---|
87 | * @param array $size_data Array of width, height, and crop properties of a size. |
---|
88 | * |
---|
89 | * @return bool|\WP_Error True if resize was successful, WP_Error on failure. |
---|
90 | */ |
---|
91 | public function resize( $size_data ) { |
---|
92 | |
---|
93 | $dimensions = $this->image_resize_dimensions( $size_data['width'], $size_data['height'], $size_data['crop'] ); |
---|
94 | |
---|
95 | if ( true === is_wp_error( $dimensions ) ) { |
---|
96 | return $dimensions; // Returns \WP_Error. |
---|
97 | } |
---|
98 | |
---|
99 | if ( true === is_wp_error( $this->mime_type ) ) { |
---|
100 | return $this->mime_type; // Returns \WP_Error. |
---|
101 | } |
---|
102 | |
---|
103 | $this->set_width_height( $dimensions ); |
---|
104 | |
---|
105 | $this->is_resized = true; |
---|
106 | |
---|
107 | return true; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Generates size data for usage in $metadata['sizes'];. |
---|
112 | * |
---|
113 | * @param array $size_data Array of width, height, and crop properties of a size. |
---|
114 | * |
---|
115 | * @return array|\WP_Error An array containing file, width, height, and mime-type keys and it's values. WP_Error on failure. |
---|
116 | */ |
---|
117 | public function get_size( $size_data ) { |
---|
118 | |
---|
119 | $is_resized = $this->resize( $size_data ); |
---|
120 | |
---|
121 | if ( true === is_wp_error( $is_resized ) ) { |
---|
122 | return $is_resized; |
---|
123 | } |
---|
124 | |
---|
125 | return array( |
---|
126 | 'file' => $this->get_filename(), |
---|
127 | 'width' => $this->get_width(), |
---|
128 | 'height' => $this->get_height(), |
---|
129 | 'mime-type' => $this->get_mime_type(), |
---|
130 | ); |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Resets the image to it's original dimensions. |
---|
135 | * |
---|
136 | * @return bool True on successful reset to original dimensions. |
---|
137 | */ |
---|
138 | public function reset_to_original() { |
---|
139 | $this->width = $this->original_width; |
---|
140 | $this->height = $this->original_height; |
---|
141 | $this->is_resized = false; |
---|
142 | |
---|
143 | return true; |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Return the basename filename. If the image has been resized, including |
---|
148 | * the resizing params for Jetpack CDN. |
---|
149 | * |
---|
150 | * @return string Basename of the filename. |
---|
151 | */ |
---|
152 | public function get_filename() { |
---|
153 | return wp_basename( $this->get_raw_filename() ); |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * Return the absolute filename. If the image has been resized, including |
---|
158 | * the resizing params for Jetpack CDN. |
---|
159 | * |
---|
160 | * @return string Filename. |
---|
161 | */ |
---|
162 | public function get_raw_filename() { |
---|
163 | return $this->is_resized() ? $this->get_resized_filename() : $this->filename; |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * Returns current image width. Either original, or after resize. |
---|
168 | * |
---|
169 | * @return int |
---|
170 | */ |
---|
171 | public function get_width() { |
---|
172 | return (int) $this->width; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * Returns current image height. Either original, or after resize. |
---|
177 | * |
---|
178 | * @return int |
---|
179 | */ |
---|
180 | public function get_height() { |
---|
181 | return (int) $this->height; |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * Returns image mime type. |
---|
186 | * |
---|
187 | * @return string|\WP_Error Image's mime type or WP_Error if it was not determined. |
---|
188 | */ |
---|
189 | public function get_mime_type() { |
---|
190 | return $this->mime_type; |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * Checks the resize status of the image. |
---|
195 | * |
---|
196 | * @return bool If the image has been resized. |
---|
197 | */ |
---|
198 | public function is_resized() { |
---|
199 | return ( true === $this->is_resized ); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * Get filename with proper args for the Photon service. |
---|
204 | * |
---|
205 | * @return string Filename with query args for Photon service |
---|
206 | */ |
---|
207 | protected function get_resized_filename() { |
---|
208 | $query_args = array( |
---|
209 | 'resize' => implode( |
---|
210 | ',', |
---|
211 | array( |
---|
212 | $this->get_width(), |
---|
213 | $this->get_height(), |
---|
214 | ) |
---|
215 | ), |
---|
216 | ); |
---|
217 | |
---|
218 | return add_query_arg( $query_args, $this->filename ); |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * Get resize dimensions used for the Jetpack CDN service. |
---|
223 | * |
---|
224 | * Converts the list of values returned from `image_resize_dimensions()` to |
---|
225 | * associative array for the sake of more readable code no relying on index |
---|
226 | * nor `list`. |
---|
227 | * |
---|
228 | * @param int $max_width Maximum width. |
---|
229 | * @param int $max_height Maximum height. |
---|
230 | * @param bool|array $crop Cropping parameters. |
---|
231 | * |
---|
232 | * @return array|\WP_Error Array of dimensions matching the parameters to imagecopyresampled. WP_Error on failure. |
---|
233 | */ |
---|
234 | protected function image_resize_dimensions( $max_width, $max_height, $crop ) { |
---|
235 | $dimensions = image_resize_dimensions( $this->original_width, $this->original_height, $max_width, $max_height, $crop ); |
---|
236 | if ( ! $dimensions ) { |
---|
237 | return new \WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions', 'jetpack-image-cdn' ), $this->filename ); |
---|
238 | } |
---|
239 | |
---|
240 | return array_combine( |
---|
241 | array( |
---|
242 | 'dst_x', |
---|
243 | 'dst_y', |
---|
244 | 'src_x', |
---|
245 | 'src_y', |
---|
246 | 'dst_w', |
---|
247 | 'dst_h', |
---|
248 | 'src_w', |
---|
249 | 'src_h', |
---|
250 | ), |
---|
251 | $dimensions |
---|
252 | ); |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * Sets proper width and height from dimensions. |
---|
257 | * |
---|
258 | * @param array $dimensions an array of image dimensions. |
---|
259 | * @return void |
---|
260 | */ |
---|
261 | protected function set_width_height( $dimensions ) { |
---|
262 | $this->width = (int) $dimensions['dst_w']; |
---|
263 | $this->height = (int) $dimensions['dst_h']; |
---|
264 | } |
---|
265 | } |
---|