10 * Base image editor class from which implementations extend |
10 * Base image editor class from which implementations extend |
11 * |
11 * |
12 * @since 3.5.0 |
12 * @since 3.5.0 |
13 */ |
13 */ |
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 = false; |
19 protected $quality = false; |
20 protected $default_quality = 82; |
20 protected $default_quality = 82; |
21 |
21 |
22 /** |
22 /** |
23 * Each instance handles a single file. |
23 * Each instance handles a single file. |
24 * |
24 * |
25 * @param string $file Path to the file to load. |
25 * @param string $file Path to the file to load. |
269 |
267 |
270 if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) { |
268 if ( ( $quality >= 1 ) && ( $quality <= 100 ) ) { |
271 $this->quality = $quality; |
269 $this->quality = $quality; |
272 return true; |
270 return true; |
273 } else { |
271 } else { |
274 return new WP_Error( 'invalid_image_quality', __('Attempted to set image quality outside of the range [1,100].') ); |
272 return new WP_Error( 'invalid_image_quality', __( 'Attempted to set image quality outside of the range [1,100].' ) ); |
275 } |
273 } |
276 } |
274 } |
277 |
275 |
278 /** |
276 /** |
279 * Returns preferred mime-type and extension based on provided |
277 * Returns preferred mime-type and extension based on provided |
296 if ( $mime_type ) { |
294 if ( $mime_type ) { |
297 $new_ext = $this->get_extension( $mime_type ); |
295 $new_ext = $this->get_extension( $mime_type ); |
298 } |
296 } |
299 |
297 |
300 if ( $filename ) { |
298 if ( $filename ) { |
301 $file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ); |
299 $file_ext = strtolower( pathinfo( $filename, PATHINFO_EXTENSION ) ); |
302 $file_mime = $this->get_mime_type( $file_ext ); |
300 $file_mime = $this->get_mime_type( $file_ext ); |
303 } |
301 } else { |
304 else { |
|
305 // If no file specified, grab editor's current extension and mime-type. |
302 // If no file specified, grab editor's current extension and mime-type. |
306 $file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) ); |
303 $file_ext = strtolower( pathinfo( $this->file, PATHINFO_EXTENSION ) ); |
307 $file_mime = $this->mime_type; |
304 $file_mime = $this->mime_type; |
308 } |
305 } |
309 |
306 |
310 // Check to see if specified mime-type is the same as type implied by |
307 // Check to see if specified mime-type is the same as type implied by |
311 // file extension. If so, prefer extension from file. |
308 // file extension. If so, prefer extension from file. |
312 if ( ! $mime_type || ( $file_mime == $mime_type ) ) { |
309 if ( ! $mime_type || ( $file_mime == $mime_type ) ) { |
313 $mime_type = $file_mime; |
310 $mime_type = $file_mime; |
314 $new_ext = $file_ext; |
311 $new_ext = $file_ext; |
315 } |
312 } |
316 |
313 |
317 // Double-check that the mime-type selected is supported by the editor. |
314 // Double-check that the mime-type selected is supported by the editor. |
318 // If not, choose a default instead. |
315 // If not, choose a default instead. |
319 if ( ! $this->supports_mime_type( $mime_type ) ) { |
316 if ( ! $this->supports_mime_type( $mime_type ) ) { |
325 * @since 3.5.0 |
322 * @since 3.5.0 |
326 * |
323 * |
327 * @param string $mime_type Mime type string. |
324 * @param string $mime_type Mime type string. |
328 */ |
325 */ |
329 $mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type ); |
326 $mime_type = apply_filters( 'image_editor_default_mime_type', $this->default_mime_type ); |
330 $new_ext = $this->get_extension( $mime_type ); |
327 $new_ext = $this->get_extension( $mime_type ); |
331 } |
328 } |
332 |
329 |
333 if ( $filename ) { |
330 if ( $filename ) { |
334 $dir = pathinfo( $filename, PATHINFO_DIRNAME ); |
331 $dir = pathinfo( $filename, PATHINFO_DIRNAME ); |
335 $ext = pathinfo( $filename, PATHINFO_EXTENSION ); |
332 $ext = pathinfo( $filename, PATHINFO_EXTENSION ); |
350 * @param string $extension |
347 * @param string $extension |
351 * @return string filename |
348 * @return string filename |
352 */ |
349 */ |
353 public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) { |
350 public function generate_filename( $suffix = null, $dest_path = null, $extension = null ) { |
354 // $suffix will be appended to the destination filename, just before the extension |
351 // $suffix will be appended to the destination filename, just before the extension |
355 if ( ! $suffix ) |
352 if ( ! $suffix ) { |
356 $suffix = $this->get_suffix(); |
353 $suffix = $this->get_suffix(); |
357 |
354 } |
358 $dir = pathinfo( $this->file, PATHINFO_DIRNAME ); |
355 |
359 $ext = pathinfo( $this->file, PATHINFO_EXTENSION ); |
356 $dir = pathinfo( $this->file, PATHINFO_DIRNAME ); |
360 |
357 $ext = pathinfo( $this->file, PATHINFO_EXTENSION ); |
361 $name = wp_basename( $this->file, ".$ext" ); |
358 |
|
359 $name = wp_basename( $this->file, ".$ext" ); |
362 $new_ext = strtolower( $extension ? $extension : $ext ); |
360 $new_ext = strtolower( $extension ? $extension : $ext ); |
363 |
361 |
364 if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) ) |
362 if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) ) { |
365 $dir = $_dest_path; |
363 $dir = $_dest_path; |
|
364 } |
366 |
365 |
367 return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}"; |
366 return trailingslashit( $dir ) . "{$name}-{$suffix}.{$new_ext}"; |
368 } |
367 } |
369 |
368 |
370 /** |
369 /** |
424 * Returns first matched mime-type from extension, |
426 * Returns first matched mime-type from extension, |
425 * as mapped from wp_get_mime_types() |
427 * as mapped from wp_get_mime_types() |
426 * |
428 * |
427 * @since 3.5.0 |
429 * @since 3.5.0 |
428 * |
430 * |
429 * @static |
|
430 * |
|
431 * @param string $extension |
431 * @param string $extension |
432 * @return string|false |
432 * @return string|false |
433 */ |
433 */ |
434 protected static function get_mime_type( $extension = null ) { |
434 protected static function get_mime_type( $extension = null ) { |
435 if ( ! $extension ) |
435 if ( ! $extension ) { |
436 return false; |
436 return false; |
|
437 } |
437 |
438 |
438 $mime_types = wp_get_mime_types(); |
439 $mime_types = wp_get_mime_types(); |
439 $extensions = array_keys( $mime_types ); |
440 $extensions = array_keys( $mime_types ); |
440 |
441 |
441 foreach ( $extensions as $_extension ) { |
442 foreach ( $extensions as $_extension ) { |
442 if ( preg_match( "/{$extension}/i", $_extension ) ) { |
443 if ( preg_match( "/{$extension}/i", $_extension ) ) { |
443 return $mime_types[$_extension]; |
444 return $mime_types[ $_extension ]; |
444 } |
445 } |
445 } |
446 } |
446 |
447 |
447 return false; |
448 return false; |
448 } |
449 } |
451 * Returns first matched extension from Mime-type, |
452 * Returns first matched extension from Mime-type, |
452 * as mapped from wp_get_mime_types() |
453 * as mapped from wp_get_mime_types() |
453 * |
454 * |
454 * @since 3.5.0 |
455 * @since 3.5.0 |
455 * |
456 * |
456 * @static |
|
457 * |
|
458 * @param string $mime_type |
457 * @param string $mime_type |
459 * @return string|false |
458 * @return string|false |
460 */ |
459 */ |
461 protected static function get_extension( $mime_type = null ) { |
460 protected static function get_extension( $mime_type = null ) { |
462 $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types() ) ); |
461 $extensions = explode( '|', array_search( $mime_type, wp_get_mime_types() ) ); |
463 |
462 |
464 if ( empty( $extensions[0] ) ) |
463 if ( empty( $extensions[0] ) ) { |
465 return false; |
464 return false; |
|
465 } |
466 |
466 |
467 return $extensions[0]; |
467 return $extensions[0]; |
468 } |
468 } |
469 } |
469 } |
470 |
470 |