wp_get_image_editor()创建一个图片编辑器,实现旋转/改变大小/另存

目录

描述

This function is the main function that you use when you want to get a reference to an image, to edit a local image on the server. It returns a WP_Image_Editor instance and loads a file into it. With that you can manipulate an image by calling methods on it. For more information see: WP_Image_Editor.

wp_get_image_editor() 描述

用法

<?php wp_get_image_editor$path$args ); ?> 

wp_get_image_editor() 用法

参数

$path

(string) (必填) Path to file to load. The path parameter should be a local file (such as /var/www/wordpress/some/directory/image). In some cases using a url as the parameter will work, but depending on the server setup and firewalls, fetching the url of the image may or may not work!!

默认值: None

$args

(array) (可选) Additional data. Accepts mime_type, methods

默认值: Empty

wp_get_image_editor() 参数

示例

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$image = wp_get_image_editor( 'cool_image.jpg' );
if ( ! is_wp_error( $image ) ) {
    $image->rotate( 90 );
    $image->resize( 300, 300, true );
    $image->save( 'new_image.jpg' );
}

wp_get_image_editor() 示例

历史

  • 添加于 版本: 3.5.0

wp_get_image_editor() 历史

源文件

wp_get_image_editor() 函数的代码位于 wp-includes/media.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Returns a WP_Image_Editor instance and loads file into it.
 *
 * @since 3.5.0
 *
 * @param string $path Path to the file to load.
 * @param array  $args Optional. Additional arguments for retrieving the image editor.
 *                     Default empty array.
 * @return WP_Image_Editor|WP_Error The WP_Image_Editor object if successful, an WP_Error
 *                                  object otherwise.
 */
function wp_get_image_editor( $path, $args = array() ) {
	$args['path'] = $path;

	if ( ! isset( $args['mime_type'] ) ) {
		$file_info = wp_check_filetype( $args['path'] );

		// If $file_info['type'] is false, then we let the editor attempt to
		// figure out the file type, rather than forcing a failure based on extension.
		if ( isset( $file_info ) && $file_info['type'] )
			$args['mime_type'] = $file_info['type'];
	}

	$implementation = _wp_image_editor_choose( $args );

	if ( $implementation ) {
		$editor = new $implementation( $path );
		$loaded = $editor->load();

		if ( is_wp_error( $loaded ) )
			return $loaded;

		return $editor;
	}

	return new WP_Error( 'image_no_editor', __('No editor could be selected.') );
}

wp_get_image_editor() 源文件