image_get_intermediate_size

Definition:
function image_get_intermediate_size($post_id, $size='thumbnail') {}

Retrieve the image’s intermediate size (resized) path, width, and height.
The $size parameter can be an array with the width and height respectively. If the size matches the ‘sizes’ metadata array for width and height, then it will be used. If there is no direct match, then the nearest image size larger than the specified size will be used. If nothing is found, then the function will break out and return false.

Parameters

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

Return values

returns:False on failure or array of file path, width, and height on success.

Source code

function image_get_intermediate_size($post_id, $size='thumbnail') {

	if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) )

		return false;



	// get the best one for a specified set of dimensions

	if ( is_array($size) && !empty($imagedata['sizes']) ) {

		foreach ( $imagedata['sizes'] as $_size => $data ) {

			// already cropped to width or height; so use this size

			if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {

				$file = $data['file'];

				list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );

				return compact( 'file', 'width', 'height' );

			}

			// add to lookup table: area => size

			$areas[$data['width'] * $data['height']] = $_size;

		}

		if ( !$size || !empty($areas) ) {

			// find for the smallest image not smaller than the desired size

			ksort($areas);

			foreach ( $areas as $_size ) {

				$data = $imagedata['sizes'][$_size];

				if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) {

					// Skip images with unexpectedly divergent aspect ratios (crops)

					// First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop

					$maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false );

					// If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size

					if ( 'thumbnail' != $_size && ( !$maybe_cropped || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] ) || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] ) ) )

						continue;

					// If we're still here, then we're going to use this size

					$file = $data['file'];

					list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );

					return compact( 'file', 'width', 'height' );

				}

			}

		}

	}



	if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )

		return false;



	$data = $imagedata['sizes'][$size];

	// include the full filesystem path of the intermediate file

	if ( empty($data['path']) && !empty($data['file']) ) {

		$file_url = wp_get_attachment_url($post_id);

		$data['path'] = path_join( dirname($imagedata['file']), $data['file'] );

		$data['url'] = path_join( dirname($file_url), $data['file'] );

	}

	return $data;

}

1989

image_edit_apply_changes

Definition:
function image_edit_apply_changes($img, $changes) {}

Parameters

  • $img
  • $changes

Defined filters

  • image_edit_before_change
    apply_filters('image_edit_before_change', $img, $changes)

Source code

function image_edit_apply_changes($img, $changes) {



	if ( !is_array($changes) )

		return $img;



	// expand change operations

	foreach ( $changes as $key => $obj ) {

		if ( isset($obj->r) ) {

			$obj->type = 'rotate';

			$obj->angle = $obj->r;

			unset($obj->r);

		} elseif ( isset($obj->f) ) {

			$obj->type = 'flip';

			$obj->axis = $obj->f;

			unset($obj->f);

		} elseif ( isset($obj->c) ) {

			$obj->type = 'crop';

			$obj->sel = $obj->c;

			unset($obj->c);

		}

		$changes[$key] = $obj;

	}



	// combine operations

	if ( count($changes) > 1 ) {

		$filtered = array($changes[0]);

		for ( $i = 0, $j = 1; $j < count($changes); $j++ ) {

			$combined = false;

			if ( $filtered[$i]->type == $changes[$j]->type ) {

				switch ( $filtered[$i]->type ) {

					case 'rotate':

						$filtered[$i]->angle += $changes[$j]->angle;

						$combined = true;

						break;

					case 'flip':

						$filtered[$i]->axis ^= $changes[$j]->axis;

						$combined = true;

						break;

				}

			}

			if ( !$combined )

				$filtered[++$i] = $changes[$j];

		}

		$changes = $filtered;

		unset($filtered);

	}



	// image resource before applying the changes

	$img = apply_filters('image_edit_before_change', $img, $changes);



	foreach ( $changes as $operation ) {

		switch ( $operation->type ) {

			case 'rotate':

				if ( $operation->angle != 0 )

					$img = _rotate_image_resource($img, $operation->angle);

				break;

			case 'flip':

				if ( $operation->axis != 0 )

					$img = _flip_image_resource($img, ($operation->axis & 1) != 0, ($operation->axis & 2) != 0);

				break;

			case 'crop':

				$sel = $operation->sel;

				$scale = 1 / _image_get_preview_ratio( imagesx($img), imagesy($img) ); // discard preview scaling

				$img = _crop_image_resource($img, $sel->x * $scale, $sel->y * $scale, $sel->w * $scale, $sel->h * $scale);

				break;

		}

	}



	return $img;

}

1987

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

image_constrain_size_for_editor

Definition:
function image_constrain_size_for_editor($width, $height, $size = 'medium') {}

Scale down the default size of an image.
This is so that the image is a better fit for the editor and theme.

Parameters

  • int $width: Width of the image
  • int $height: Height of the image
  • string|array $size: Size of what the result image should be.

Return values

returns:Width and height of what the result image should resize to.

Defined filters

  • editor_max_image_size
    apply_filters( 'editor_max_image_size', array( $max_width, $max_height )

Source code

function image_constrain_size_for_editor($width, $height, $size = 'medium') {

	global $content_width, $_wp_additional_image_sizes;



	if ( is_array($size) ) {

		$max_width = $size[0];

		$max_height = $size[1];

	}

	elseif ( $size == 'thumb' || $size == 'thumbnail' ) {

		$max_width = intval(get_option('thumbnail_size_w'));

		$max_height = intval(get_option('thumbnail_size_h'));

		// last chance thumbnail size defaults

		if ( !$max_width && !$max_height ) {

			$max_width = 128;

			$max_height = 96;

		}

	}

	elseif ( $size == 'medium' ) {

		$max_width = intval(get_option('medium_size_w'));

		$max_height = intval(get_option('medium_size_h'));

		// if no width is set, default to the theme content width if available

	}

	elseif ( $size == 'large' ) {

		// we're inserting a large size image into the editor.  if it's a really

		// big image we'll scale it down to fit reasonably within the editor

		// itself, and within the theme's content width if it's known.  the user

		// can resize it in the editor if they wish.

		$max_width = intval(get_option('large_size_w'));

		$max_height = intval(get_option('large_size_h'));

		if ( intval($content_width) > 0 )

			$max_width = min( intval($content_width), $max_width );

	} elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {

		$max_width = intval( $_wp_additional_image_sizes[$size]['width'] );

		$max_height = intval( $_wp_additional_image_sizes[$size]['height'] );

		if ( intval($content_width) > 0 && is_admin() ) // Only in admin. Assume that theme authors know what they're doing.

			$max_width = min( intval($content_width), $max_width );

	}

	// $size == 'full' has no constraint

	else {

		$max_width = $width;

		$max_height = $height;

	}



	list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size );



	return wp_constrain_dimensions( $width, $height, $max_width, $max_height );

}

1983

image_attachment_fields_to_save

Definition:
function image_attachment_fields_to_save($post, $attachment) {}

Parameters

  • unknown_type $post
  • unknown_type $attachment

Source code

function image_attachment_fields_to_save($post, $attachment) {

	if ( substr($post['post_mime_type'], 0, 5) == 'image' ) {

		if ( strlen(trim($post['post_title'])) == 0 ) {

			$post['post_title'] = preg_replace('/\.\w+$/', '', basename($post['guid']));

			$post['errors']['post_title']['errors'][] = __('Empty Title filled from filename.');

		}

	}



	return $post;

}

1981