Skip to content
Merged
Prev Previous commit
Next Next commit
Merge branch 'feature/174-full-image-size' into feature/174-full-imag…
…e-size-in-content
  • Loading branch information
mitogh committed Feb 28, 2022
commit e29965875c6f77a303083b0742c5e1cfee712c24
98 changes: 1 addition & 97 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,102 +175,6 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id )

add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property', 10, 2 );

/**
* Creates a `sources` property at the root of the metadata to store the information about
* the created full sized images. It creates an image with the size of the full image size.
*
* @since n.e.x.t
*
* @param array $metadata The current metadta of the modified image.
* @param int $attachment_id The ID of the attachment being modified.
* @return array The metadata modified of the image.
*/
function webp_uploads_create_sources_property_for_full_image( $metadata, $attachment_id ) {
// This should take place only on the JPEG image.
$valid_mime_transforms = webp_uploads_get_supported_image_mime_transforms();

// Not a supported mime type to create the sources property.
$mime_type = get_post_mime_type( $attachment_id );
if ( ! isset( $valid_mime_transforms[ $mime_type ] ) ) {
return $metadata;
}

$file = get_attached_file( $attachment_id, true );

// File does not exist.
if ( ! file_exists( $file ) ) {
return $metadata;
}

if ( ! isset( $metadata['width'], $metadata['height'] ) ) {
return $metadata;
}

if ( ! isset( $metadata['sources'] ) || ! is_array( $metadata['sources'] ) ) {
$metadata['sources'] = array();
}

if ( empty( $metadata['sources'][ $mime_type ] ) ) {
$metadata['sources'][ $mime_type ] = array(
'file' => wp_basename( $file ),
'filesize' => filesize( $file ),
);
wp_update_attachment_metadata( $attachment_id, $metadata );
}

$allowed_mimes = array_flip( wp_get_mime_types() );
$original_directory = pathinfo( $file, PATHINFO_DIRNAME );
$original_extension = pathinfo( $file, PATHINFO_EXTENSION );

foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) {
// If this property exists no need to create the image again.
if ( ! empty( $metadata['sources'][ $targeted_mime ] ) ) {
continue;
}

if ( ! isset( $allowed_mimes[ $targeted_mime ] ) || ! is_string( $allowed_mimes[ $targeted_mime ] ) ) {
continue;
}

if ( ! wp_image_editor_supports( array( 'mime_type' => $targeted_mime ) ) ) {
continue;
}

$editor = wp_get_image_editor( $file );

if ( is_wp_error( $editor ) ) {
continue;
}

$editor->resize( $metadata['width'], $metadata['height'], false );
$extension = explode( '|', $allowed_mimes[ $targeted_mime ] );
$extension = reset( $extension );

if ( empty( $extension ) || empty( $original_extension ) ) {
continue;
}

$filename = wp_basename( $file, ".{$original_extension}" );
$target_file_name = trailingslashit( $original_directory ) . "{$filename}.{$extension}";
$image = $editor->save( $target_file_name, $targeted_mime );

if ( is_wp_error( $image ) ) {
continue;
}

$metadata['sources'][ $targeted_mime ] = array(
'file' => $image['file'],
'filesize' => isset( $image['path'] ) ? filesize( $image['path'] ) : 0,
);

wp_update_attachment_metadata( $attachment_id, $metadata );
}

return $metadata;
}

add_filter( 'wp_generate_attachment_metadata', 'webp_uploads_create_sources_property_for_full_image', 10, 2 );

/**
* Creates a new image based of the specified attachment with a defined mime type
* this image would be stored in the same place as the provided size name inside the
Expand Down Expand Up @@ -462,7 +366,7 @@ function webp_uploads_remove_sources_files( $attachment_id ) {
$original_mime_from_post = get_post_mime_type( $attachment_id );
$original_mime_from_file = wp_check_filetype( $file )['type'];

// Delete of full sizes mime types.
// Delete full sizes mime types.
foreach ( $metadata['sources'] as $mime => $properties ) {
// Don't remove the image with the same mime type as the original image as this would be removed by WordPress.
if ( $mime === $original_mime_from_post || $mime === $original_mime_from_file ) {
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.