Edit File by line
/home/zeestwma/richards.../wp-inclu...
File: media.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WordPress API for media display.
[2] Fix | Delete
*
[3] Fix | Delete
* @package WordPress
[4] Fix | Delete
* @subpackage Media
[5] Fix | Delete
*/
[6] Fix | Delete
[7] Fix | Delete
// Don't load directly.
[8] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[9] Fix | Delete
die( '-1' );
[10] Fix | Delete
}
[11] Fix | Delete
[12] Fix | Delete
/**
[13] Fix | Delete
* Retrieves additional image sizes.
[14] Fix | Delete
*
[15] Fix | Delete
* @since 4.7.0
[16] Fix | Delete
*
[17] Fix | Delete
* @global array $_wp_additional_image_sizes
[18] Fix | Delete
*
[19] Fix | Delete
* @return array Additional images size data.
[20] Fix | Delete
*/
[21] Fix | Delete
function wp_get_additional_image_sizes() {
[22] Fix | Delete
global $_wp_additional_image_sizes;
[23] Fix | Delete
[24] Fix | Delete
if ( ! $_wp_additional_image_sizes ) {
[25] Fix | Delete
$_wp_additional_image_sizes = array();
[26] Fix | Delete
}
[27] Fix | Delete
[28] Fix | Delete
return $_wp_additional_image_sizes;
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
/**
[32] Fix | Delete
* Scales down the default size of an image.
[33] Fix | Delete
*
[34] Fix | Delete
* This is so that the image is a better fit for the editor and theme.
[35] Fix | Delete
*
[36] Fix | Delete
* The `$size` parameter accepts either an array or a string. The supported string
[37] Fix | Delete
* values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at
[38] Fix | Delete
* 128 width and 96 height in pixels. Also supported for the string value is
[39] Fix | Delete
* 'medium', 'medium_large' and 'full'. The 'full' isn't actually supported, but any value other
[40] Fix | Delete
* than the supported will result in the content_width size or 500 if that is
[41] Fix | Delete
* not set.
[42] Fix | Delete
*
[43] Fix | Delete
* Finally, there is a filter named {@see 'editor_max_image_size'}, that will be
[44] Fix | Delete
* called on the calculated array for width and height, respectively.
[45] Fix | Delete
*
[46] Fix | Delete
* @since 2.5.0
[47] Fix | Delete
*
[48] Fix | Delete
* @global int $content_width
[49] Fix | Delete
*
[50] Fix | Delete
* @param int $width Width of the image in pixels.
[51] Fix | Delete
* @param int $height Height of the image in pixels.
[52] Fix | Delete
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
[53] Fix | Delete
* of width and height values in pixels (in that order). Default 'medium'.
[54] Fix | Delete
* @param string $context Optional. Could be 'display' (like in a theme) or 'edit'
[55] Fix | Delete
* (like inserting into an editor). Default null.
[56] Fix | Delete
* @return int[] {
[57] Fix | Delete
* An array of width and height values.
[58] Fix | Delete
*
[59] Fix | Delete
* @type int $0 The maximum width in pixels.
[60] Fix | Delete
* @type int $1 The maximum height in pixels.
[61] Fix | Delete
* }
[62] Fix | Delete
*/
[63] Fix | Delete
function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) {
[64] Fix | Delete
global $content_width;
[65] Fix | Delete
[66] Fix | Delete
$_wp_additional_image_sizes = wp_get_additional_image_sizes();
[67] Fix | Delete
[68] Fix | Delete
if ( ! $context ) {
[69] Fix | Delete
$context = is_admin() ? 'edit' : 'display';
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
if ( is_array( $size ) ) {
[73] Fix | Delete
$max_width = $size[0];
[74] Fix | Delete
$max_height = $size[1];
[75] Fix | Delete
} elseif ( 'thumb' === $size || 'thumbnail' === $size ) {
[76] Fix | Delete
$max_width = (int) get_option( 'thumbnail_size_w' );
[77] Fix | Delete
$max_height = (int) get_option( 'thumbnail_size_h' );
[78] Fix | Delete
// Last chance thumbnail size defaults.
[79] Fix | Delete
if ( ! $max_width && ! $max_height ) {
[80] Fix | Delete
$max_width = 128;
[81] Fix | Delete
$max_height = 96;
[82] Fix | Delete
}
[83] Fix | Delete
} elseif ( 'medium' === $size ) {
[84] Fix | Delete
$max_width = (int) get_option( 'medium_size_w' );
[85] Fix | Delete
$max_height = (int) get_option( 'medium_size_h' );
[86] Fix | Delete
[87] Fix | Delete
} elseif ( 'medium_large' === $size ) {
[88] Fix | Delete
$max_width = (int) get_option( 'medium_large_size_w' );
[89] Fix | Delete
$max_height = (int) get_option( 'medium_large_size_h' );
[90] Fix | Delete
[91] Fix | Delete
if ( (int) $content_width > 0 ) {
[92] Fix | Delete
$max_width = min( (int) $content_width, $max_width );
[93] Fix | Delete
}
[94] Fix | Delete
} elseif ( 'large' === $size ) {
[95] Fix | Delete
/*
[96] Fix | Delete
* We're inserting a large size image into the editor. If it's a really
[97] Fix | Delete
* big image we'll scale it down to fit reasonably within the editor
[98] Fix | Delete
* itself, and within the theme's content width if it's known. The user
[99] Fix | Delete
* can resize it in the editor if they wish.
[100] Fix | Delete
*/
[101] Fix | Delete
$max_width = (int) get_option( 'large_size_w' );
[102] Fix | Delete
$max_height = (int) get_option( 'large_size_h' );
[103] Fix | Delete
[104] Fix | Delete
if ( (int) $content_width > 0 ) {
[105] Fix | Delete
$max_width = min( (int) $content_width, $max_width );
[106] Fix | Delete
}
[107] Fix | Delete
} elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ), true ) ) {
[108] Fix | Delete
$max_width = (int) $_wp_additional_image_sizes[ $size ]['width'];
[109] Fix | Delete
$max_height = (int) $_wp_additional_image_sizes[ $size ]['height'];
[110] Fix | Delete
// Only in admin. Assume that theme authors know what they're doing.
[111] Fix | Delete
if ( (int) $content_width > 0 && 'edit' === $context ) {
[112] Fix | Delete
$max_width = min( (int) $content_width, $max_width );
[113] Fix | Delete
}
[114] Fix | Delete
} else { // $size === 'full' has no constraint.
[115] Fix | Delete
$max_width = $width;
[116] Fix | Delete
$max_height = $height;
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
/**
[120] Fix | Delete
* Filters the maximum image size dimensions for the editor.
[121] Fix | Delete
*
[122] Fix | Delete
* @since 2.5.0
[123] Fix | Delete
*
[124] Fix | Delete
* @param int[] $max_image_size {
[125] Fix | Delete
* An array of width and height values.
[126] Fix | Delete
*
[127] Fix | Delete
* @type int $0 The maximum width in pixels.
[128] Fix | Delete
* @type int $1 The maximum height in pixels.
[129] Fix | Delete
* }
[130] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[131] Fix | Delete
* an array of width and height values in pixels (in that order).
[132] Fix | Delete
* @param string $context The context the image is being resized for.
[133] Fix | Delete
* Possible values are 'display' (like in a theme)
[134] Fix | Delete
* or 'edit' (like inserting into an editor).
[135] Fix | Delete
*/
[136] Fix | Delete
list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context );
[137] Fix | Delete
[138] Fix | Delete
return wp_constrain_dimensions( $width, $height, $max_width, $max_height );
[139] Fix | Delete
}
[140] Fix | Delete
[141] Fix | Delete
/**
[142] Fix | Delete
* Retrieves width and height attributes using given width and height values.
[143] Fix | Delete
*
[144] Fix | Delete
* Both attributes are required in the sense that both parameters must have a
[145] Fix | Delete
* value, but are optional in that if you set them to false or null, then they
[146] Fix | Delete
* will not be added to the returned string.
[147] Fix | Delete
*
[148] Fix | Delete
* You can set the value using a string, but it will only take numeric values.
[149] Fix | Delete
* If you wish to put 'px' after the numbers, then it will be stripped out of
[150] Fix | Delete
* the return.
[151] Fix | Delete
*
[152] Fix | Delete
* @since 2.5.0
[153] Fix | Delete
*
[154] Fix | Delete
* @param int|string $width Image width in pixels.
[155] Fix | Delete
* @param int|string $height Image height in pixels.
[156] Fix | Delete
* @return string HTML attributes for width and, or height.
[157] Fix | Delete
*/
[158] Fix | Delete
function image_hwstring( $width, $height ) {
[159] Fix | Delete
$out = '';
[160] Fix | Delete
if ( $width ) {
[161] Fix | Delete
$out .= 'width="' . (int) $width . '" ';
[162] Fix | Delete
}
[163] Fix | Delete
if ( $height ) {
[164] Fix | Delete
$out .= 'height="' . (int) $height . '" ';
[165] Fix | Delete
}
[166] Fix | Delete
return $out;
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
/**
[170] Fix | Delete
* Scales an image to fit a particular size (such as 'thumb' or 'medium').
[171] Fix | Delete
*
[172] Fix | Delete
* The URL might be the original image, or it might be a resized version. This
[173] Fix | Delete
* function won't create a new resized copy, it will just return an already
[174] Fix | Delete
* resized one if it exists.
[175] Fix | Delete
*
[176] Fix | Delete
* A plugin may use the {@see 'image_downsize'} filter to hook into and offer image
[177] Fix | Delete
* resizing services for images. The hook must return an array with the same
[178] Fix | Delete
* elements that are normally returned from the function.
[179] Fix | Delete
*
[180] Fix | Delete
* @since 2.5.0
[181] Fix | Delete
*
[182] Fix | Delete
* @param int $id Attachment ID for image.
[183] Fix | Delete
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
[184] Fix | Delete
* of width and height values in pixels (in that order). Default 'medium'.
[185] Fix | Delete
* @return array|false {
[186] Fix | Delete
* Array of image data, or boolean false if no image is available.
[187] Fix | Delete
*
[188] Fix | Delete
* @type string $0 Image source URL.
[189] Fix | Delete
* @type int $1 Image width in pixels.
[190] Fix | Delete
* @type int $2 Image height in pixels.
[191] Fix | Delete
* @type bool $3 Whether the image is a resized image.
[192] Fix | Delete
* }
[193] Fix | Delete
*/
[194] Fix | Delete
function image_downsize( $id, $size = 'medium' ) {
[195] Fix | Delete
$is_image = wp_attachment_is_image( $id );
[196] Fix | Delete
[197] Fix | Delete
/**
[198] Fix | Delete
* Filters whether to preempt the output of image_downsize().
[199] Fix | Delete
*
[200] Fix | Delete
* Returning a truthy value from the filter will effectively short-circuit
[201] Fix | Delete
* down-sizing the image, returning that value instead.
[202] Fix | Delete
*
[203] Fix | Delete
* @since 2.5.0
[204] Fix | Delete
*
[205] Fix | Delete
* @param bool|array $downsize Whether to short-circuit the image downsize.
[206] Fix | Delete
* @param int $id Attachment ID for image.
[207] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[208] Fix | Delete
* an array of width and height values in pixels (in that order).
[209] Fix | Delete
*/
[210] Fix | Delete
$out = apply_filters( 'image_downsize', false, $id, $size );
[211] Fix | Delete
[212] Fix | Delete
if ( $out ) {
[213] Fix | Delete
return $out;
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
$img_url = wp_get_attachment_url( $id );
[217] Fix | Delete
$meta = wp_get_attachment_metadata( $id );
[218] Fix | Delete
$width = 0;
[219] Fix | Delete
$height = 0;
[220] Fix | Delete
$is_intermediate = false;
[221] Fix | Delete
$img_url_basename = wp_basename( $img_url );
[222] Fix | Delete
[223] Fix | Delete
/*
[224] Fix | Delete
* If the file isn't an image, attempt to replace its URL with a rendered image from its meta.
[225] Fix | Delete
* Otherwise, a non-image type could be returned.
[226] Fix | Delete
*/
[227] Fix | Delete
if ( ! $is_image ) {
[228] Fix | Delete
if ( ! empty( $meta['sizes']['full'] ) ) {
[229] Fix | Delete
$img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url );
[230] Fix | Delete
$img_url_basename = $meta['sizes']['full']['file'];
[231] Fix | Delete
$width = $meta['sizes']['full']['width'];
[232] Fix | Delete
$height = $meta['sizes']['full']['height'];
[233] Fix | Delete
} else {
[234] Fix | Delete
return false;
[235] Fix | Delete
}
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
// Try for a new style intermediate size.
[239] Fix | Delete
$intermediate = image_get_intermediate_size( $id, $size );
[240] Fix | Delete
[241] Fix | Delete
if ( $intermediate ) {
[242] Fix | Delete
$img_url = str_replace( $img_url_basename, $intermediate['file'], $img_url );
[243] Fix | Delete
$width = $intermediate['width'];
[244] Fix | Delete
$height = $intermediate['height'];
[245] Fix | Delete
$is_intermediate = true;
[246] Fix | Delete
} elseif ( 'thumbnail' === $size && ! empty( $meta['thumb'] ) && is_string( $meta['thumb'] ) ) {
[247] Fix | Delete
// Fall back to the old thumbnail.
[248] Fix | Delete
$imagefile = get_attached_file( $id );
[249] Fix | Delete
$thumbfile = str_replace( wp_basename( $imagefile ), wp_basename( $meta['thumb'] ), $imagefile );
[250] Fix | Delete
[251] Fix | Delete
if ( file_exists( $thumbfile ) ) {
[252] Fix | Delete
$info = wp_getimagesize( $thumbfile );
[253] Fix | Delete
[254] Fix | Delete
if ( $info ) {
[255] Fix | Delete
$img_url = str_replace( $img_url_basename, wp_basename( $thumbfile ), $img_url );
[256] Fix | Delete
$width = $info[0];
[257] Fix | Delete
$height = $info[1];
[258] Fix | Delete
$is_intermediate = true;
[259] Fix | Delete
}
[260] Fix | Delete
}
[261] Fix | Delete
}
[262] Fix | Delete
[263] Fix | Delete
if ( ! $width && ! $height && isset( $meta['width'], $meta['height'] ) ) {
[264] Fix | Delete
// Any other type: use the real image.
[265] Fix | Delete
$width = $meta['width'];
[266] Fix | Delete
$height = $meta['height'];
[267] Fix | Delete
}
[268] Fix | Delete
[269] Fix | Delete
if ( $img_url ) {
[270] Fix | Delete
// We have the actual image size, but might need to further constrain it if content_width is narrower.
[271] Fix | Delete
list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
[272] Fix | Delete
[273] Fix | Delete
return array( $img_url, $width, $height, $is_intermediate );
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
return false;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
/**
[280] Fix | Delete
* Registers a new image size.
[281] Fix | Delete
*
[282] Fix | Delete
* @since 2.9.0
[283] Fix | Delete
*
[284] Fix | Delete
* @global array $_wp_additional_image_sizes Associative array of additional image sizes.
[285] Fix | Delete
*
[286] Fix | Delete
* @param string $name Image size identifier.
[287] Fix | Delete
* @param int $width Optional. Image width in pixels. Default 0.
[288] Fix | Delete
* @param int $height Optional. Image height in pixels. Default 0.
[289] Fix | Delete
* @param bool|array $crop {
[290] Fix | Delete
* Optional. Image cropping behavior. If false, the image will be scaled (default).
[291] Fix | Delete
* If true, image will be cropped to the specified dimensions using center positions.
[292] Fix | Delete
* If an array, the image will be cropped using the array to specify the crop location:
[293] Fix | Delete
*
[294] Fix | Delete
* @type string $0 The x crop position. Accepts 'left', 'center', or 'right'.
[295] Fix | Delete
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'.
[296] Fix | Delete
* }
[297] Fix | Delete
*/
[298] Fix | Delete
function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
[299] Fix | Delete
global $_wp_additional_image_sizes;
[300] Fix | Delete
[301] Fix | Delete
$_wp_additional_image_sizes[ $name ] = array(
[302] Fix | Delete
'width' => absint( $width ),
[303] Fix | Delete
'height' => absint( $height ),
[304] Fix | Delete
'crop' => $crop,
[305] Fix | Delete
);
[306] Fix | Delete
}
[307] Fix | Delete
[308] Fix | Delete
/**
[309] Fix | Delete
* Checks if an image size exists.
[310] Fix | Delete
*
[311] Fix | Delete
* @since 3.9.0
[312] Fix | Delete
*
[313] Fix | Delete
* @param string $name The image size to check.
[314] Fix | Delete
* @return bool True if the image size exists, false if not.
[315] Fix | Delete
*/
[316] Fix | Delete
function has_image_size( $name ) {
[317] Fix | Delete
$sizes = wp_get_additional_image_sizes();
[318] Fix | Delete
return isset( $sizes[ $name ] );
[319] Fix | Delete
}
[320] Fix | Delete
[321] Fix | Delete
/**
[322] Fix | Delete
* Removes a new image size.
[323] Fix | Delete
*
[324] Fix | Delete
* @since 3.9.0
[325] Fix | Delete
*
[326] Fix | Delete
* @global array $_wp_additional_image_sizes
[327] Fix | Delete
*
[328] Fix | Delete
* @param string $name The image size to remove.
[329] Fix | Delete
* @return bool True if the image size was successfully removed, false on failure.
[330] Fix | Delete
*/
[331] Fix | Delete
function remove_image_size( $name ) {
[332] Fix | Delete
global $_wp_additional_image_sizes;
[333] Fix | Delete
[334] Fix | Delete
if ( isset( $_wp_additional_image_sizes[ $name ] ) ) {
[335] Fix | Delete
unset( $_wp_additional_image_sizes[ $name ] );
[336] Fix | Delete
return true;
[337] Fix | Delete
}
[338] Fix | Delete
[339] Fix | Delete
return false;
[340] Fix | Delete
}
[341] Fix | Delete
[342] Fix | Delete
/**
[343] Fix | Delete
* Registers an image size for the post thumbnail.
[344] Fix | Delete
*
[345] Fix | Delete
* @since 2.9.0
[346] Fix | Delete
*
[347] Fix | Delete
* @see add_image_size() for details on cropping behavior.
[348] Fix | Delete
*
[349] Fix | Delete
* @param int $width Image width in pixels.
[350] Fix | Delete
* @param int $height Image height in pixels.
[351] Fix | Delete
* @param bool|array $crop {
[352] Fix | Delete
* Optional. Image cropping behavior. If false, the image will be scaled (default).
[353] Fix | Delete
* If true, image will be cropped to the specified dimensions using center positions.
[354] Fix | Delete
* If an array, the image will be cropped using the array to specify the crop location:
[355] Fix | Delete
*
[356] Fix | Delete
* @type string $0 The x crop position. Accepts 'left', 'center', or 'right'.
[357] Fix | Delete
* @type string $1 The y crop position. Accepts 'top', 'center', or 'bottom'.
[358] Fix | Delete
* }
[359] Fix | Delete
*/
[360] Fix | Delete
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
[361] Fix | Delete
add_image_size( 'post-thumbnail', $width, $height, $crop );
[362] Fix | Delete
}
[363] Fix | Delete
[364] Fix | Delete
/**
[365] Fix | Delete
* Gets an img tag for an image attachment, scaling it down if requested.
[366] Fix | Delete
*
[367] Fix | Delete
* The {@see 'get_image_tag_class'} filter allows for changing the class name for the
[368] Fix | Delete
* image without having to use regular expressions on the HTML content. The
[369] Fix | Delete
* parameters are: what WordPress will use for the class, the Attachment ID,
[370] Fix | Delete
* image align value, and the size the image should be.
[371] Fix | Delete
*
[372] Fix | Delete
* The second filter, {@see 'get_image_tag'}, has the HTML content, which can then be
[373] Fix | Delete
* further manipulated by a plugin to change all attribute values and even HTML
[374] Fix | Delete
* content.
[375] Fix | Delete
*
[376] Fix | Delete
* @since 2.5.0
[377] Fix | Delete
*
[378] Fix | Delete
* @param int $id Attachment ID.
[379] Fix | Delete
* @param string $alt Image description for the alt attribute.
[380] Fix | Delete
* @param string $title Image description for the title attribute.
[381] Fix | Delete
* @param string $align Part of the class name for aligning the image.
[382] Fix | Delete
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array of
[383] Fix | Delete
* width and height values in pixels (in that order). Default 'medium'.
[384] Fix | Delete
* @return string HTML IMG element for given image attachment.
[385] Fix | Delete
*/
[386] Fix | Delete
function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) {
[387] Fix | Delete
[388] Fix | Delete
list( $img_src, $width, $height ) = image_downsize( $id, $size );
[389] Fix | Delete
$hwstring = image_hwstring( $width, $height );
[390] Fix | Delete
[391] Fix | Delete
$title = $title ? 'title="' . esc_attr( $title ) . '" ' : '';
[392] Fix | Delete
[393] Fix | Delete
$size_class = is_array( $size ) ? implode( 'x', $size ) : $size;
[394] Fix | Delete
$class = 'align' . esc_attr( $align ) . ' size-' . esc_attr( $size_class ) . ' wp-image-' . $id;
[395] Fix | Delete
[396] Fix | Delete
/**
[397] Fix | Delete
* Filters the value of the attachment's image tag class attribute.
[398] Fix | Delete
*
[399] Fix | Delete
* @since 2.6.0
[400] Fix | Delete
*
[401] Fix | Delete
* @param string $class CSS class name or space-separated list of classes.
[402] Fix | Delete
* @param int $id Attachment ID.
[403] Fix | Delete
* @param string $align Part of the class name for aligning the image.
[404] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[405] Fix | Delete
* an array of width and height values in pixels (in that order).
[406] Fix | Delete
*/
[407] Fix | Delete
$class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );
[408] Fix | Delete
[409] Fix | Delete
$html = '<img src="' . esc_url( $img_src ) . '" alt="' . esc_attr( $alt ) . '" ' . $title . $hwstring . 'class="' . $class . '" />';
[410] Fix | Delete
[411] Fix | Delete
/**
[412] Fix | Delete
* Filters the HTML content for the image tag.
[413] Fix | Delete
*
[414] Fix | Delete
* @since 2.6.0
[415] Fix | Delete
*
[416] Fix | Delete
* @param string $html HTML content for the image.
[417] Fix | Delete
* @param int $id Attachment ID.
[418] Fix | Delete
* @param string $alt Image description for the alt attribute.
[419] Fix | Delete
* @param string $title Image description for the title attribute.
[420] Fix | Delete
* @param string $align Part of the class name for aligning the image.
[421] Fix | Delete
* @param string|int[] $size Requested image size. Can be any registered image size name, or
[422] Fix | Delete
* an array of width and height values in pixels (in that order).
[423] Fix | Delete
*/
[424] Fix | Delete
return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
[425] Fix | Delete
}
[426] Fix | Delete
[427] Fix | Delete
/**
[428] Fix | Delete
* Calculates the new dimensions for a down-sampled image.
[429] Fix | Delete
*
[430] Fix | Delete
* If either width or height are empty, no constraint is applied on
[431] Fix | Delete
* that dimension.
[432] Fix | Delete
*
[433] Fix | Delete
* @since 2.5.0
[434] Fix | Delete
*
[435] Fix | Delete
* @param int $current_width Current width of the image.
[436] Fix | Delete
* @param int $current_height Current height of the image.
[437] Fix | Delete
* @param int $max_width Optional. Max width in pixels to constrain to. Default 0.
[438] Fix | Delete
* @param int $max_height Optional. Max height in pixels to constrain to. Default 0.
[439] Fix | Delete
* @return int[] {
[440] Fix | Delete
* An array of width and height values.
[441] Fix | Delete
*
[442] Fix | Delete
* @type int $0 The width in pixels.
[443] Fix | Delete
* @type int $1 The height in pixels.
[444] Fix | Delete
* }
[445] Fix | Delete
*/
[446] Fix | Delete
function wp_constrain_dimensions( $current_width, $current_height, $max_width = 0, $max_height = 0 ) {
[447] Fix | Delete
if ( ! $max_width && ! $max_height ) {
[448] Fix | Delete
return array( $current_width, $current_height );
[449] Fix | Delete
}
[450] Fix | Delete
[451] Fix | Delete
$width_ratio = 1.0;
[452] Fix | Delete
$height_ratio = 1.0;
[453] Fix | Delete
$did_width = false;
[454] Fix | Delete
$did_height = false;
[455] Fix | Delete
[456] Fix | Delete
if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) {
[457] Fix | Delete
$width_ratio = $max_width / $current_width;
[458] Fix | Delete
$did_width = true;
[459] Fix | Delete
}
[460] Fix | Delete
[461] Fix | Delete
if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) {
[462] Fix | Delete
$height_ratio = $max_height / $current_height;
[463] Fix | Delete
$did_height = true;
[464] Fix | Delete
}
[465] Fix | Delete
[466] Fix | Delete
// Calculate the larger/smaller ratios.
[467] Fix | Delete
$smaller_ratio = min( $width_ratio, $height_ratio );
[468] Fix | Delete
$larger_ratio = max( $width_ratio, $height_ratio );
[469] Fix | Delete
[470] Fix | Delete
if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) {
[471] Fix | Delete
// The larger ratio is too big. It would result in an overflow.
[472] Fix | Delete
$ratio = $smaller_ratio;
[473] Fix | Delete
} else {
[474] Fix | Delete
// The larger ratio fits, and is likely to be a more "snug" fit.
[475] Fix | Delete
$ratio = $larger_ratio;
[476] Fix | Delete
}
[477] Fix | Delete
[478] Fix | Delete
// Very small dimensions may result in 0, 1 should be the minimum.
[479] Fix | Delete
$w = max( 1, (int) round( $current_width * $ratio ) );
[480] Fix | Delete
$h = max( 1, (int) round( $current_height * $ratio ) );
[481] Fix | Delete
[482] Fix | Delete
/*
[483] Fix | Delete
* Sometimes, due to rounding, we'll end up with a result like this:
[484] Fix | Delete
* 465x700 in a 177x177 box is 117x176... a pixel short.
[485] Fix | Delete
* We also have issues with recursive calls resulting in an ever-changing result.
[486] Fix | Delete
* Constraining to the result of a constraint should yield the original result.
[487] Fix | Delete
* Thus we look for dimensions that are one pixel shy of the max value and bump them up.
[488] Fix | Delete
*/
[489] Fix | Delete
[490] Fix | Delete
// Note: $did_width means it is possible $smaller_ratio == $width_ratio.
[491] Fix | Delete
if ( $did_width && $w === $max_width - 1 ) {
[492] Fix | Delete
$w = $max_width; // Round it up.
[493] Fix | Delete
}
[494] Fix | Delete
[495] Fix | Delete
// Note: $did_height means it is possible $smaller_ratio == $height_ratio.
[496] Fix | Delete
if ( $did_height && $h === $max_height - 1 ) {
[497] Fix | Delete
$h = $max_height; // Round it up.
[498] Fix | Delete
}
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function