Skip to content

Commit

Permalink
Merge pull request #180 from PRX/image_size_from_download
Browse files Browse the repository at this point in the history
Add download media to get its size if it does not exist in drupal
  • Loading branch information
rpeterman-gp authored Mar 26, 2024
2 parents f753cd6 + 1c10bcd commit 29f4702
Showing 1 changed file with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -615,14 +615,41 @@ function pmh_add_external_media_without_import( $url, $attributes = array(), $op
if (
$image_sizes
&&
isset( $image_sizes['width'] )
( isset( $image_sizes['width'] ) && $image_sizes['width'] )
&&
isset( $image_sizes['height'] )
( isset( $image_sizes['height'] ) && $image_sizes['height'] )
) {

// Set width and height.
$attachment_metadata['width'] = $image_sizes['width'];
$attachment_metadata['height'] = $image_sizes['height'];
} else {
// Get the attachment URL.
$s_attachment_url = wp_get_attachment_url( $attachment_id );
// Initialize media fix cli object.
$media_fix_cli = new PMH_Worker();

// Get the image dimensions.
$a_image_sizes = $media_fix_cli->clean_url_get_imagesize( $s_attachment_url );
update_post_meta( $attachment_id, '_temp_import_image_size_from_download', $a_image_sizes );

// If the image dimensions are found.
if ( $a_image_sizes ) {

list( $i_width, $i_height ) = $a_image_sizes;

// Update the attachment metadata.
$attachment_metadata['width'] = $i_width;
$attachment_metadata['height'] = $i_height;
} else {
// If width and height are still missing after a dowload set them with max wp size as default value.
if ( ! $attachment_metadata['width'] ) {
$attachment_metadata['width'] = 2560;
}
if ( ! $attachment_metadata['height'] ) {
$attachment_metadata['height'] = 2560;
}
}
}
}

Expand Down

0 comments on commit 29f4702

Please sign in to comment.