image_downsize

Definition:
function image_downsize($id, $size = 'medium') {}

Scale an image to fit a particular size (such as ‘thumb’ or ‘medium’).
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.

Parameters

  • int $id: Attachment ID for image.
  • array|string $size: Optional, default is ‘medium’. Size of image, either array or string.

Return values

returns:False on failure, array on success.

Defined filters

  • image_downsize
    apply_filters('image_downsize', false, $id, $size)

Source code

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



	if ( !wp_attachment_is_image($id) )

		return false;



	$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);



	// plugins can use this to provide resize services

	if ( $out = apply_filters('image_downsize', false, $id, $size) )

		return $out;



	// 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;



}

1985

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: