返回介绍

image_downsize()

发布于 2017-09-11 00:59:36 字数 6369 浏览 1132 评论 0 收藏 0

image_downsize( int $id,  array|string $size = 'medium' )

Scale an image to fit a particular size (such as ‘thumb’ or ‘medium’).


description

Array with image url, width, height, and whether is intermediate size, in that order is returned on success is returned. $is_intermediate is true if $url is a resized image, false if it is the original.

The URL might be the original image, or it might be a resized version. This function won’t create a new resized copy, it will just return an already resized one if it exists.

A plugin may use the ‘image_downsize’ filter to hook into and offer image resizing services for images. The hook must return an array with the same elements that are returned in the function. The first element being the URL to the new image that was resized.


参数

$id

(int) (Required) Attachment ID for image.

$size

(array|string) (Optional) Image size to scale to. Accepts any valid image size, or an array of width and height values in pixels (in that order).

Default value: 'medium'


返回值

(false|array) Array containing the image URL, width, height, and boolean for whether the image is an intermediate size. False on failure.


源代码

File: wp-includes/media.php

function image_downsize( $id, $size = 'medium' ) {
	$is_image = wp_attachment_is_image( $id );

	/**
	 * Filters whether to preempt the output of image_downsize().
	 *
	 * Passing a truthy value to the filter will effectively short-circuit
	 * down-sizing the image, returning that value as output instead.
	 *
	 * @since 2.5.0
	 *
	 * @param bool         $downsize Whether to short-circuit the image downsize. Default false.
	 * @param int          $id       Attachment ID for image.
	 * @param array|string $size     Size of image. Image size or array of width and height values (in that order).
	 *                               Default 'medium'.
	 */
	if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) {
		return $out;
	}

	$img_url = wp_get_attachment_url($id);
	$meta = wp_get_attachment_metadata($id);
	$width = $height = 0;
	$is_intermediate = false;
	$img_url_basename = wp_basename($img_url);

	// If the file isn't an image, attempt to replace its URL with a rendered image from its meta.
	// Otherwise, a non-image type could be returned.
	if ( ! $is_image ) {
		if ( ! empty( $meta['sizes'] ) ) {
			$img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url );
			$img_url_basename = $meta['sizes']['full']['file'];
			$width = $meta['sizes']['full']['width'];
			$height = $meta['sizes']['full']['height'];
		} else {
			return false;
		}
	}

	// try for a new style intermediate size
	if ( $intermediate = image_get_intermediate_size($id, $size) ) {
		$img_url = str_replace($img_url_basename, $intermediate['file'], $img_url);
		$width = $intermediate['width'];
		$height = $intermediate['height'];
		$is_intermediate = true;
	}
	elseif ( $size == 'thumbnail' ) {
		// fall back to the old thumbnail
		if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) {
			$img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url);
			$width = $info[0];
			$height = $info[1];
			$is_intermediate = true;
		}
	}
	if ( !$width && !$height && isset( $meta['width'], $meta['height'] ) ) {
		// any other type: use the real image
		$width = $meta['width'];
		$height = $meta['height'];
	}

	if ( $img_url) {
		// we have the actual image size, but might need to further constrain it if content_width is narrower
		list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );

		return array( $img_url, $width, $height, $is_intermediate );
	}
	return false;

}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: wp_basename()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/media.php: image_get_intermediate_size()
  • wp-includes/media.php: image_constrain_size_for_editor()
  • wp-includes/media.php: image_downsize
  • wp-includes/post.php: wp_attachment_is_image()
  • wp-includes/post.php: wp_get_attachment_url()
  • wp-includes/post.php: wp_get_attachment_metadata()
  • wp-includes/post.php: wp_get_attachment_thumb_file()
  • Show 4 more uses Hide more uses

Used By

  • wp-admin/includes/media.php: image_size_input_fields()
  • wp-includes/media.php: get_image_tag()
  • wp-includes/media.php: wp_get_attachment_image_src()
  • wp-includes/post.php: wp_get_attachment_thumb_url()
  • wp-includes/class-wp-xmlrpc-server.php: wp_xmlrpc_server::_prepare_media_item()

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 Codex

    Add a function to select medium attachment images

    Maybe you already know wp_get_attachment_thumb_url();. Now I’m showing how to do the same thing to return the url for the medium sized attachment.

    
    function wp_get_attachment_medium_url( $id )
    {
        $medium_array = image_downsize( $id, 'medium' );
        $medium_path = $medium_array[0];
    
        return $medium_path;
    }
    

    $id is the ID of the attachment. This can be really useful for plugins like WP-Choose-Thumb. With this function you can get the medium sized previews.

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

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

发布评论

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