wp/wp-includes/class-wp-image-editor.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    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.
    32 	 * Checks to see if current environment supports the editor chosen.
    32 	 * Checks to see if current environment supports the editor chosen.
    33 	 * Must be overridden in a sub-class.
    33 	 * Must be overridden in a sub-class.
    34 	 *
    34 	 *
    35 	 * @since 3.5.0
    35 	 * @since 3.5.0
    36 	 *
    36 	 *
    37 	 * @static
       
    38 	 * @abstract
    37 	 * @abstract
    39 	 *
    38 	 *
    40 	 * @param array $args
    39 	 * @param array $args
    41 	 * @return bool
    40 	 * @return bool
    42 	 */
    41 	 */
    48 	 * Checks to see if editor supports the mime-type specified.
    47 	 * Checks to see if editor supports the mime-type specified.
    49 	 * Must be overridden in a sub-class.
    48 	 * Must be overridden in a sub-class.
    50 	 *
    49 	 *
    51 	 * @since 3.5.0
    50 	 * @since 3.5.0
    52 	 *
    51 	 *
    53 	 * @static
       
    54 	 * @abstract
    52 	 * @abstract
    55 	 *
    53 	 *
    56 	 * @param string $mime_type
    54 	 * @param string $mime_type
    57 	 * @return bool
    55 	 * @return bool
    58 	 */
    56 	 */
   189 	 * @param int $height
   187 	 * @param int $height
   190 	 * @return true
   188 	 * @return true
   191 	 */
   189 	 */
   192 	protected function update_size( $width = null, $height = null ) {
   190 	protected function update_size( $width = null, $height = null ) {
   193 		$this->size = array(
   191 		$this->size = array(
   194 			'width' => (int) $width,
   192 			'width'  => (int) $width,
   195 			'height' => (int) $height
   193 			'height' => (int) $height,
   196 		);
   194 		);
   197 		return true;
   195 		return true;
   198 	}
   196 	}
   199 
   197 
   200 	/**
   198 	/**
   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 	/**
   373 	 * @since 3.5.0
   372 	 * @since 3.5.0
   374 	 *
   373 	 *
   375 	 * @return false|string suffix
   374 	 * @return false|string suffix
   376 	 */
   375 	 */
   377 	public function get_suffix() {
   376 	public function get_suffix() {
   378 		if ( ! $this->get_size() )
   377 		if ( ! $this->get_size() ) {
   379 			return false;
   378 			return false;
       
   379 		}
   380 
   380 
   381 		return "{$this->size['width']}x{$this->size['height']}";
   381 		return "{$this->size['width']}x{$this->size['height']}";
   382 	}
   382 	}
   383 
   383 
   384 	/**
   384 	/**
   404 		if ( $result && $stream ) {
   404 		if ( $result && $stream ) {
   405 			$contents = ob_get_contents();
   405 			$contents = ob_get_contents();
   406 
   406 
   407 			$fp = fopen( $filename, 'w' );
   407 			$fp = fopen( $filename, 'w' );
   408 
   408 
   409 			if ( ! $fp )
   409 			if ( ! $fp ) {
       
   410 				ob_end_clean();
   410 				return false;
   411 				return false;
       
   412 			}
   411 
   413 
   412 			fwrite( $fp, $contents );
   414 			fwrite( $fp, $contents );
   413 			fclose( $fp );
   415 			fclose( $fp );
   414 		}
   416 		}
   415 
   417 
   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