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



_wp_make_additional_mime_types › WordPress Function

Since6.1.0
Deprecatedn/a
_wp_make_additional_mime_types ( $new_mime_types, $file, $image_meta, $attachment_id )
Access:
  • private
Parameters: (4)
  • (array) $new_mime_types Array defining what mime types to create.
    Required: Yes
  • (string) $file Full path to the image file.
    Required: Yes
  • (array) $image_meta The attachment meta data array.
    Required: Yes
  • (int) $attachment_id Attachment ID to process.
    Required: Yes
Returns:
  • (array) The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
Defined at:
Codex:

Low-level function to create full-size images in additional mime types.

Updates the image meta after each mime type image is created.


Source

function _wp_make_additional_mime_types( $new_mime_types, $file, $image_meta, $attachment_id ) {
	$imagesize          = array(
		$image_meta['width'],
		$image_meta['height'],
	);
	$exif_meta          = isset( $image_meta['image_meta'] ) ? $image_meta['image_meta'] : null;
	$original_file_size = isset( $image_meta['filesize'] ) ? $image_meta['filesize'] : wp_filesize( $file );

	foreach ( $new_mime_types as $mime_type ) {
		list( $editor, $resized, $rotated ) = _wp_maybe_scale_and_rotate_image( $file, $attachment_id, $imagesize, $exif_meta, $mime_type );
		if ( is_wp_error( $editor ) ) {
			// The image cannot be edited.
			continue;
		}

		$suffix    = _wp_get_image_suffix( $resized, $rotated );
		$extension = wp_get_default_extension_for_mime_type( $mime_type );

		$saved = $editor->save( $editor->generate_filename( $suffix, null, $extension ) );

		if ( is_wp_error( $saved ) ) {
			// TODO: Log errors.
		} else {
			// If the saved image is larger than the original, discard it.
			$filesize = isset( $saved['filesize'] ) ? $saved['filesize'] : wp_filesize( $saved['path'] );
			if ( $filesize && $original_file_size && $filesize > $original_file_size ) {
				wp_delete_file( $saved['path'] );
				continue;
			}
			$image_meta['sources'][ $mime_type ] = _wp_get_sources_from_meta( $saved );
			wp_update_attachment_metadata( $attachment_id, $image_meta );
		}
	}

	return $image_meta;
}