14 abstract class WP_Image_Editor { |
14 abstract class WP_Image_Editor { |
15 protected $file = null; |
15 protected $file = null; |
16 protected $size = null; |
16 protected $size = null; |
17 protected $mime_type = null; |
17 protected $mime_type = null; |
18 protected $default_mime_type = 'image/jpeg'; |
18 protected $default_mime_type = 'image/jpeg'; |
19 protected $quality = 90; |
19 protected $quality = false; |
|
20 protected $default_quality = 90; |
20 |
21 |
21 /** |
22 /** |
22 * Each instance handles a single file. |
23 * Each instance handles a single file. |
23 */ |
24 */ |
24 public function __construct( $file ) { |
25 public function __construct( $file ) { |
80 abstract public function save( $destfilename = null, $mime_type = null ); |
81 abstract public function save( $destfilename = null, $mime_type = null ); |
81 |
82 |
82 /** |
83 /** |
83 * Resizes current image. |
84 * Resizes current image. |
84 * |
85 * |
85 * @since 3.5.0 |
86 * At minimum, either a height or width must be provided. |
86 * @access public |
87 * If one of the two is set to null, the resize will |
87 * @abstract |
88 * maintain aspect ratio according to the provided dimension. |
88 * |
89 * |
89 * @param int $max_w |
90 * @since 3.5.0 |
90 * @param int $max_h |
91 * @access public |
91 * @param boolean $crop |
92 * @abstract |
|
93 * |
|
94 * @param int|null $max_w Image width. |
|
95 * @param int|null $max_h Image height. |
|
96 * @param boolean $crop |
92 * @return boolean|WP_Error |
97 * @return boolean|WP_Error |
93 */ |
98 */ |
94 abstract public function resize( $max_w, $max_h, $crop = false ); |
99 abstract public function resize( $max_w, $max_h, $crop = false ); |
95 |
100 |
96 /** |
101 /** |
187 * @since 3.5.0 |
191 * @since 3.5.0 |
188 * @access protected |
192 * @access protected |
189 * |
193 * |
190 * @param int $width |
194 * @param int $width |
191 * @param int $height |
195 * @param int $height |
|
196 * @return true |
192 */ |
197 */ |
193 protected function update_size( $width = null, $height = null ) { |
198 protected function update_size( $width = null, $height = null ) { |
194 $this->size = array( |
199 $this->size = array( |
195 'width' => (int) $width, |
200 'width' => (int) $width, |
196 'height' => (int) $height |
201 'height' => (int) $height |
197 ); |
202 ); |
198 return true; |
203 return true; |
199 } |
204 } |
200 |
205 |
201 /** |
206 /** |
|
207 * Gets the Image Compression quality on a 1-100% scale. |
|
208 * |
|
209 * @since 4.0.0 |
|
210 * @access public |
|
211 * |
|
212 * @return int $quality Compression Quality. Range: [1,100] |
|
213 */ |
|
214 public function get_quality() { |
|
215 if ( ! $this->quality ) { |
|
216 $this->set_quality(); |
|
217 } |
|
218 |
|
219 return $this->quality; |
|
220 } |
|
221 |
|
222 /** |
202 * Sets Image Compression quality on a 1-100% scale. |
223 * Sets Image Compression quality on a 1-100% scale. |
203 * |
224 * |
204 * @since 3.5.0 |
225 * @since 3.5.0 |
205 * @access public |
226 * @access public |
206 * |
227 * |
207 * @param int $quality Compression Quality. Range: [1,100] |
228 * @param int $quality Compression Quality. Range: [1,100] |
208 * @return boolean |
229 * @return boolean|WP_Error True if set successfully; WP_Error on failure. |
209 */ |
230 */ |
210 public function set_quality( $quality ) { |
231 public function set_quality( $quality = null ) { |
211 /** |
232 if ( null === $quality ) { |
212 * Filter the default quality setting. |
233 /** |
213 * |
234 * Filter the default image compression quality setting. |
214 * @since 3.5.0 |
235 * |
215 * |
236 * Applies only during initial editor instantiation, or when set_quality() is run |
216 * @param int $quality Quality level between 0 (low) and 100 (high). |
237 * manually without the `$quality` argument. |
217 */ |
238 * |
218 $this->quality = apply_filters( 'wp_editor_set_quality', $quality ); |
239 * set_quality() has priority over the filter. |
219 |
240 * |
220 return ( (bool) $this->quality ); |
241 * @since 3.5.0 |
|
242 * |
|
243 * @param int $quality Quality level between 1 (low) and 100 (high). |
|
244 * @param string $mime_type Image mime type. |
|
245 */ |
|
246 $quality = apply_filters( 'wp_editor_set_quality', $this->default_quality, $this->mime_type ); |
|
247 |
|
248 if ( 'image/jpeg' == $this->mime_type ) { |
|
249 /** |
|
250 * Filter the JPEG compression quality for backward-compatibility. |
|
251 * |
|
252 * Applies only during initial editor instantiation, or when set_quality() is run |
|
253 * manually without the `$quality` argument. |
|
254 * |
|
255 * set_quality() has priority over the filter. |
|
256 * |
|
257 * The filter is evaluated under two contexts: 'image_resize', and 'edit_image', |
|
258 * (when a JPEG image is saved to file). |
|
259 * |
|
260 * @since 2.5.0 |
|
261 * |
|
262 * @param int $quality Quality level between 0 (low) and 100 (high) of the JPEG. |
|
263 * @param string $context Context of the filter. |
|
264 */ |
|
265 $quality = apply_filters( 'jpeg_quality', $quality, 'image_resize' ); |
|
266 } |
|
267 |
|
268 if ( $quality < 0 || $quality > 100 ) { |
|
269 $quality = $this->default_quality; |
|
270 } |
|
271 } |
|
272 |
|
273 // Allow 0, but squash to 1 due to identical images in GD, and for backwards compatibility. |
|
274 if ( 0 === $quality ) { |
|
275 $quality = 1; |
|
276 } |
|
277 |
|
278 if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) { |
|
279 $this->quality = $quality; |
|
280 return true; |
|
281 } else { |
|
282 return new WP_Error( 'invalid_image_quality', __('Attempted to set image quality outside of the range [1,100].') ); |
|
283 } |
221 } |
284 } |
222 |
285 |
223 /** |
286 /** |
224 * Returns preferred mime-type and extension based on provided |
287 * Returns preferred mime-type and extension based on provided |
225 * file's extension and mime, or current file's extension and mime. |
288 * file's extension and mime, or current file's extension and mime. |
234 * @param string $filename |
297 * @param string $filename |
235 * @param string $mime_type |
298 * @param string $mime_type |
236 * @return array { filename|null, extension, mime-type } |
299 * @return array { filename|null, extension, mime-type } |
237 */ |
300 */ |
238 protected function get_output_format( $filename = null, $mime_type = null ) { |
301 protected function get_output_format( $filename = null, $mime_type = null ) { |
239 $new_ext = $file_ext = null; |
302 $new_ext = null; |
240 $file_mime = null; |
|
241 |
303 |
242 // By default, assume specified type takes priority |
304 // By default, assume specified type takes priority |
243 if ( $mime_type ) { |
305 if ( $mime_type ) { |
244 $new_ext = $this->get_extension( $mime_type ); |
306 $new_ext = $this->get_extension( $mime_type ); |
245 } |
307 } |