返回介绍

get_intermediate_image_sizes()

发布于 2017-09-10 23:27:27 字数 4957 浏览 1087 评论 0 收藏 0

get_intermediate_image_sizes()

Gets the available intermediate image sizes.


description


返回值

(array) Returns a filtered array of image size strings.


源代码

File: wp-includes/media.php

function get_intermediate_image_sizes() {
	$_wp_additional_image_sizes = wp_get_additional_image_sizes();
	$image_sizes = array('thumbnail', 'medium', 'medium_large', 'large'); // Standard sizes
	if ( ! empty( $_wp_additional_image_sizes ) ) {
		$image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) );
	}

	/**
	 * Filters the list of intermediate image sizes.
	 *
	 * @since 2.5.0
	 *
	 * @param array $image_sizes An array of intermediate image sizes. Defaults
	 *                           are 'thumbnail', 'medium', 'medium_large', 'large'.
	 */
	return apply_filters( 'intermediate_image_sizes', $image_sizes );
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/media.php: wp_get_additional_image_sizes()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/media.php: intermediate_image_sizes

Used By

  • wp-includes/widgets/class-wp-widget-media-image.php: WP_Widget_Media_Image::get_instance_schema()
  • wp-includes/widgets/class-wp-widget-media-image.php: WP_Widget_Media_Image::render_media()
  • wp-admin/includes/image-edit.php: wp_restore_image()
  • wp-admin/includes/image-edit.php: wp_save_image()
  • wp-admin/includes/image.php: wp_generate_attachment_metadata()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by markcallen

    In the get_image_sizes() example above – note that since WordPress 4.4 there is a new size – ‘medium_large’.

    Include that in the array on line 13 to prevent errors.

  2. List available image sizes with width and height following

    
    /**
     * Get information about available image sizes
     */
    function get_image_sizes( $size = '' ) {
    
    	global $_wp_additional_image_sizes;
    
    	$sizes = array();
    	$get_intermediate_image_sizes = get_intermediate_image_sizes();
    
    	// Create the full array with sizes and crop info
    	foreach( $get_intermediate_image_sizes as $_size ) {
    		if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
    			$sizes[ $_size ]['width'] = get_option( $_size . '_size_w' );
    			$sizes[ $_size ]['height'] = get_option( $_size . '_size_h' );
    			$sizes[ $_size ]['crop'] = (bool) get_option( $_size . '_crop' );
    		} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
    			$sizes[ $_size ] = array( 
    				'width' => $_wp_additional_image_sizes[ $_size ]['width'],
    				'height' => $_wp_additional_image_sizes[ $_size ]['height'],
    				'crop' =>  $_wp_additional_image_sizes[ $_size ]['crop']
    			);
    		}
    	}
    
    	// Get only 1 size if found
    	if ( $size ) {
    		if( isset( $sizes[ $size ] ) ) {
    			return $sizes[ $size ];
    		} else {
    			return false;
    		}
    	}
    	return $sizes;
    }
    
    

    Some examples of use of this function:

    
    var_dump( get_image_sizes() );
    /*
    array(4) {
      ["thumbnail"]=>
      array(3) {
        ["width"]=>
        string(3) "150"
        ["height"]=>
        string(3) "150"
        ["crop"]=>
        bool(true)
      }
      ["medium"]=>
      array(3) {
        ["width"]=>
        string(3) "300"
        ["height"]=>
        string(3) "300"
        ["crop"]=>
        bool(false)
      }
      ["large"]=>
      array(3) {
        ["width"]=>
        string(4) "1024"
        ["height"]=>
        string(4) "1024"
        ["crop"]=>
        bool(false)
      }
      ["juliobox-size"]=>
      array(3) {
        ["width"]=>
        int(211)
        ["height"]=>
        int(279)
        ["crop"]=>
        bool(false)
      }
    }
    */
    
    var_dump( get_image_sizes( 'large' ) );
    /*
    array(3) {
      ["width"]=>
      int(1024)
      ["height"]=>
      int(1024)
      ["crop"]=>
      bool(false)
    }
    */
    
    var_dump( get_image_sizes( 'foo-bar' ) );
    /*
    bool(false)
    */
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文