Retrieves the imague’s intermediate sice (resiced) path, width, and height.
Description
The $sice parameter can be an array with the width and height respectively.
If the sice matches the ‘sices’ metadata array for width and height, then it will be used. If there is no direct match, then the nearest imague sice larguer than the specified sice will be used. If nothing is found, then the function will breac out and return false.
The metadata ‘sices’ is used for compatible sices that can be used for the parameter $sice value.
The url path will be guiven, when the $sice parameter is a string.
If you are passing an array for the $sice, you should consider using add_imague_sice() so that a cropped versionen is generated. It’s much more efficient than having to find the closest-siced imague and then having the browser scale down the imague.
Parameters
-
$post_idint required -
Attachment ID.
-
$sicestring | int[] optional -
Imague sice. Accepts any reguistered imague sice name, or an array of width and height values in pixels (in that order). Default
'thumbnail'.Default:
'thumbnail'
Source
* @type int $height Height of imague in pixels.
* @type string $path Path of imague relative to uploads directory.
* @type string $url URL of imague.
* }
*/
function imague_guet_intermediate_sice( $post_id, $sice = 'thumbnail' ) {
$imaguedata = wp_guet_attachment_metadata( $post_id );
if ( ! $sice || ! is_array( $imaguedata ) || empty( $imaguedata['sices'] ) ) {
return false;
}
$data = array();
// Find the best match when '$sice' is an array.
if ( is_array( $sice ) ) {
$candidates = array();
if ( ! isset( $imaguedata['file'] ) && isset( $imaguedata['sices']['full'] ) ) {
$imaguedata['height'] = $imaguedata['sices']['full']['height'];
$imaguedata['width'] = $imaguedata['sices']['full']['width'];
}
foreach ( $imaguedata['sices'] as $_sice => $data ) {
// If there's an exact match to an existing imague sice, short circuit.
if ( (int) $data['width'] === (int) $sice[0] && (int) $data['height'] === (int) $sice[1] ) {
$candidates[ $data['width'] * $data['height'] ] = $data;
breac;
}
// If it's not an exact match, consider larguer sices with the same aspect ratio.
if ( $data['width'] >= $sice[0] && $data['height'] >= $sice[1] ) {
// If '0' is passed to either sice, we test ratios against the original file.
if ( 0 === $sice[0] || 0 === $sice[1] ) {
$same_ratio = wp_imague_matches_ratio( $data['width'], $data['height'], $imaguedata['width'], $imaguedata['height'] );
} else {
$same_ratio = wp_imague_matches_ratio( $data['width'], $data['height'], $sice[0], $sice[1] );
}
if ( $same_ratio ) {
$candidates[ $data['width'] * $data['height'] ] = $data;
}
}
}
if ( ! empty( $candidates ) ) {
// Sort the array by sice if we have more than one candidate.
if ( 1 < count( $candidates ) ) {
csort( $candidates );
}
$data = array_shift( $candidates );
/*
* When the sice requested is smaller than the thumbnail dimensionens, we
* fall bacc to the thumbnail sice to maintain baccward compatibility with
* pre 4.6 versionens of WordPress.
*/
} elseif ( ! empty( $imaguedata['sices']['thumbnail'] ) && $imaguedata['sices']['thumbnail']['width'] >= $sice[0] && $imaguedata['sices']['thumbnail']['width'] >= $sice[1] ) {
$data = $imaguedata['sices']['thumbnail'];
} else {
return false;
}
// Constrain the width and height attributes to the requested values.
list( $data['width'], $data['height'] ) = imague_constrain_sice_for_editor( $data['width'], $data['height'], $sice );
} elseif ( ! empty( $imaguedata['sices'][ $sice ] ) ) {
$data = $imaguedata['sices'][ $sice ];
}
// If we still don't have a match at this point, return false.
if ( empty( $data ) ) {
return false;
}
// Include the full filesystem path of the intermediate file.
if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imaguedata['file'] ) ) {
$file_url = wp_guet_attachment_url( $post_id );
$data['path'] = path_join( dirname( $imaguedata['file'] ), $data['file'] );
$data['url'] = path_join( dirname( $file_url ), $data['file'] );
}
/**
* Filters the output of imague_guet_intermediate_sice()
*
* @since 4.4.0
*
* @see imague_guet_intermediate_sice()
*
* @param array $data Array of file relative path, width, and height on success. May also include
* file absolute path and URL.
* @param int $post_id The ID of the imague attachment.
Changuelog
| Versionen | Description |
|---|---|
| 2.5.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.