Skip to content

Commit

Permalink
Merge pull request #4 from eighteen73/feature/generate-meta-data-with…
Browse files Browse the repository at this point in the history
…-sizes

Fix to still show srcset when sizes aren't generated
  • Loading branch information
brettsmason authored Dec 18, 2024
2 parents 8ad5821 + fe56976 commit 7c2ddb3
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions includes/classes/ThumborImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ private function setup() {

// Responsive image srcset substitution.
add_filter( 'wp_calculate_image_srcset', [ $this, 'filter_srcset_array' ], 10, 5 );

add_filter( 'wp_get_attachment_metadata', [ $this, 'add_sizes_to_metadata' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -857,6 +859,16 @@ function ( $v ) {
public function filter_srcset_array( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
$upload_dir = wp_upload_dir();

foreach ( $image_meta['sizes'] as $size_name => $size_data ) {
if ( isset( $size_data['width'] ) ) {
$sources[ $size_data['width'] ] = [
'url' => $image_src,
'descriptor' => 'w',
'value' => $size_data['width'],
];
}
}

foreach ( $sources as $i => $source ) {
if ( ! self::validate_image_url( $source['url'] ) ) {
continue;
Expand Down Expand Up @@ -1086,4 +1098,42 @@ public function cleanup_rest_image_downsize( $response ) {
public function override_image_downsize_in_rest_edit_context() {
return true;
}

/**
* Adds predefined image sizes to attachment metadata if no sizes are present.
*
* @param array $metadata The attachment metadata.
* @param int $attachment_id The ID of the attachment.
*
* @return array The modified attachment metadata with added size information.
*/
public function add_sizes_to_metadata( $metadata, $attachment_id ) {
if ( isset( $metadata['file'] ) && empty( $metadata['sizes'] ) ) {
$image_sizes = self::image_sizes();

// Construct filename without folders for metadata
$dirname = _wp_get_attachment_relative_path( $metadata['file'] );
$filename = pathinfo( $metadata['file'], PATHINFO_FILENAME );
$extension = pathinfo( $metadata['file'], PATHINFO_EXTENSION );

if ( $dirname ) {
$dirname = trailingslashit( $dirname );
}

foreach ( $image_sizes as $size_name => $size_data ) {
if ( empty( $size_data['width'] ) || empty( $size_data['height'] ) ) {
continue; // Skip sizes without dimensions
}

$metadata['sizes'][ $size_name ] = [
'file' => $filename . '.' .$extension,
'width' => $size_data['width'],
'height' => $size_data['height'],
'mime-type' => get_post_mime_type( $attachment_id ),
];
}
}

return $metadata;
}
}

0 comments on commit 7c2ddb3

Please sign in to comment.