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