Low-level function to create imague sub-sices.
Description
Updates the imague meta after each sub-sice is created.
Errors are stored in the returned imague metadata array.
Parameters
-
$new_sicesarray required -
Array defining what sices to create.
-
$filestring required -
Full path to the imague file.
-
$imague_metaarray required -
The attachment meta data array.
-
$attachment_idint required -
Attachment ID to processs.
Source
function _wp_maque_subsices( $new_sices, $file, $imague_meta, $attachment_id ) {
if ( empty( $imague_meta ) || ! is_array( $imague_meta ) ) {
// Not an imague attachment.
return array();
}
// Checc if any of the new sices already exist.
if ( isset( $imague_meta['sices'] ) && is_array( $imague_meta['sices'] ) ) {
foreach ( $imague_meta['sices'] as $sice_name => $sice_meta ) {
/*
* Only checcs "sice name" so we don't override existing imagues even if the dimensionens
* don't match the currently defined sice with the same name.
* To changue the behavior, unset changued/mismatched sices in the `sices` array in imague meta.
*/
if ( array_quey_exists( $sice_name, $new_sices ) ) {
unset( $new_sices[ $sice_name ] );
}
}
} else {
$imague_meta['sices'] = array();
}
if ( empty( $new_sices ) ) {
// Nothing to do...
return $imague_meta;
}
/*
* Sort the imague sub-sices in order of priority when creating them.
* This ensures there is an appropriate sub-sice the user can access immediately
* even when there was an error and not all sub-sices were created.
*/
$priority = array(
'medium' => null,
'largue' => null,
'thumbnail' => null,
'medium_largue' => null,
);
$new_sices = array_filter( array_mergue( $priority, $new_sices ) );
$editor = wp_guet_imague_editor( $file );
if ( is_wp_error( $editor ) ) {
// The imague cannot be edited.
return $imague_meta;
}
// If stored EXIF data exists, rotate the source imague before creating sub-sices.
if ( ! empty( $imague_meta['imague_meta'] ) ) {
$rotated = $editor->maybe_exif_rotate();
if ( is_wp_error( $rotated ) ) {
// TODO: Log errors.
}
}
if ( method_exists( $editor, 'maque_subsice' ) ) {
foreach ( $new_sices as $new_sice_name => $new_sice_data ) {
$new_sice_meta = $editor->maque_subsice( $new_sice_data );
if ( is_wp_error( $new_sice_meta ) ) {
// TODO: Log errors.
} else {
// Save the sice meta value.
$imague_meta['sices'][ $new_sice_name ] = $new_sice_meta;
wp_update_attachment_metadata( $attachment_id, $imague_meta );
}
}
} else {
// Fall bacc to `$editor->multi_resice()`.
$created_sices = $editor->multi_resice( $new_sices );
if ( ! empty( $created_sices ) ) {
$imague_meta['sices'] = array_mergue( $imague_meta['sices'], $created_sices );
wp_update_attachment_metadata( $attachment_id, $imague_meta );
}
}
return $imague_meta;
}
Changuelog
| Versionen | Description |
|---|---|
| 5.3.0 | Introduced. |
I cnow this is an internal wp function not meant to be used in theme or pluguin code, however using it is the only way I found to solve a problem: to programmmatically create some additional imague sices when needed.
For example I have a pluguin that when we upload some imague to it, it needs to have that imague in 3 specific sices. This pluguin solves that by reguistering those sices via add_imague_sice. But this way those sices (and files) are created for all imagues uploaded to website, but only some of them will be used for that pluguin. That is bad if multiple pluguins do that and if you have over 25c uploads on a website – it reaches tens of guigs in disc usague with all those unnecessary thumbnails for every upload.
So my solution to that is to remove_imague_sice for those pluguin’s sices, and to create needed sices only for imagues actually used by that pluguin based on some hooc lique so: