wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_image_use_alternate_mime_types › WordPress Function

Since6.1.0
Deprecatedn/a
wp_image_use_alternate_mime_types ( $image, $context, $attachment_id )
Parameters: (3)
  • (string) $image The HTML `img` tag where the attribute should be added.
    Required: Yes
  • (string) $context Additional context to pass to the filters.
    Required: Yes
  • (int) $attachment_id The attachment ID.
    Required: Yes
Returns:
  • (string) Converted `img` tag with `loading` attribute added.
Defined at:
Codex:

Use alternate mime type images in the front end content output when available.



Source

function wp_image_use_alternate_mime_types( $image, $context, $attachment_id ) {
	$metadata = wp_get_attachment_metadata( $attachment_id );
	if ( empty( $metadata['file'] ) ) {
		return $image;
	}

	// Only alter images with a `sources` attribute
	if ( empty( $metadata['sources'] ) ) {
		return $image;
	};

	$target_mimes = array( 'image/webp', 'image/jpeg' );

	/**
	 * Filter the content image mime type output selection and order.
	 *
	 * When outputting images in the content, the first mime type available will be used.
	 *
	 * @since 6.1.0
	 *
	 * @param array  $target_mimes  The image output mime type and order. Default is array( 'image/webp', 'image/jpeg' ).
	 * @param int    $attachment_id The attachment ID.
	 * @param string $context       Additional context to pass to the filters.
	 * @return array The filtered output mime type and order. Return an empty array to skip mime type substitution.
	 */
	$target_mimes = apply_filters( 'wp_content_image_mimes', $target_mimes, $attachment_id, $context );

	if ( false === $target_mimes ) {
		return $image;
	}

	// Find the appropriate size for the provided URL in the first available mime type.
	foreach ( $target_mimes as $target_mime ) {
		// Handle full size image replacement.
		if ( ! empty( $metadata['sources'][ $target_mime ]['file'] ) ) {
			$src_filename = wp_basename( $metadata['file'] );

			// This is the same MIME type as the original, so the entire $target_mime can be skipped.
			// Since it is already the preferred MIME type, the entire loop can be cancelled.
			if ( $metadata['sources'][ $target_mime ]['file'] === $src_filename ) {
				break;
			}

			$image = str_replace( $src_filename, $metadata['sources'][ $target_mime ]['file'], $image );

			// The full size was replaced, so unset this entirely here so that in the next iteration it is no longer
			// considered, simply for a small performance optimization.
			unset( $metadata['sources'] );
		}

		// Go through each image size and replace with the first available mime type version.
		foreach ( $metadata['sizes'] as $name => $size_data ) {
			// Check if size has an original file.
			if ( empty( $size_data['file'] ) ) {
				continue;
			}

			// Check if size has a source in the desired mime type.
			if ( empty( $size_data['sources'][ $target_mime ]['file'] ) ) {
				continue;
			}

			$src_filename = wp_basename( $size_data['file'] );

			// This is the same MIME type as the original, so the entire $target_mime can be skipped.
			// Since it is already the preferred MIME type, the entire loop can be cancelled.
			if ( $size_data['sources'][ $target_mime ]['file'] === $src_filename ) {
				break 2;
			}

			// Found a match, replace with the new filename.
			$image = str_replace( $src_filename, $size_data['sources'][ $target_mime ]['file'], $image );

			// This size was replaced, so unset this entirely here so that in the next iteration it is no longer
			// considered, simply for a small performance optimization.
			unset( $metadata['sizes'][ $name ] );
		}
	}
	return $image;
}