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