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



_wp_make_subsizes › WordPress Function

Since5.3.0
Deprecatedn/a
_wp_make_subsizes ( $new_sizes, $file, $image_meta, $attachment_id, $mime_type = '' )
Access:
  • private
Parameters: (5)
  • (array) $new_sizes Array defining what sizes 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
  • (string) $mime_type Optional. The mime type to check for missing sizes. Default is the image mime of $file.
    Required: No
    Default: (empty)
Returns:
  • (array) The attachment meta data with updated `sizes` array. Includes an array of errors encountered while resizing.
Defined at:
Codex:
Change Log:
  • 6.1.0

Low-level function to create image sub-sizes.

Updates the image meta after each sub-size is created. Errors are stored in the returned image metadata array.


Source

function _wp_make_subsizes( $new_sizes, $file, $image_meta, $attachment_id, $mime_type = '' ) {
	if ( empty( $image_meta ) || ! is_array( $image_meta ) ) {
		// Not an image attachment.
		return array();
	}

	if ( ! $mime_type ) {
		$mime_type = wp_get_image_mime( $file );
	}

	// Check if any of the new sizes already exist.
	if ( isset( $image_meta['sizes'] ) && is_array( $image_meta['sizes'] ) ) {
		foreach ( $image_meta['sizes'] as $size_name => $size_meta ) {
			/*
			 * Only checks "size name" so we don't override existing images even if the dimensions
			 * don't match the currently defined size with the same name.
			 * To change the behavior, unset changed/mismatched sizes in the `sizes` array in image meta.
			 */
			if ( array_key_exists( $size_name, $new_sizes ) ) {
				// Unset the size if it is either the required mime type already exists either as main mime type or
				// within sources.
				if ( $size_meta['mime-type'] === $mime_type || isset( $size_meta['sources'][ $mime_type ] ) ) {
					unset( $new_sizes[ $size_name ] );
				}
			}
		}
	} else {
		$image_meta['sizes'] = array();
	}

	if ( empty( $new_sizes ) ) {
		// Nothing to do...
		return $image_meta;
	}

	/*
	 * Sort the image sub-sizes in order of priority when creating them.
	 * This ensures there is an appropriate sub-size the user can access immediately
	 * even when there was an error and not all sub-sizes were created.
	 */
	$priority = array(
		'medium'       => null,
		'large'        => null,
		'thumbnail'    => null,
		'medium_large' => null,
	);

	$new_sizes = array_filter( array_merge( $priority, $new_sizes ) );

	$editor = wp_get_image_editor( $file, array( 'mime_type' => $mime_type ) );

	if ( is_wp_error( $editor ) ) {
		// The image cannot be edited.
		return $image_meta;
	}

	$editor->set_output_mime_type( $mime_type );

	// If stored EXIF data exists, rotate the source image before creating sub-sizes.
	if ( ! empty( $image_meta['image_meta'] ) ) {
		$rotated = $editor->maybe_exif_rotate();

		if ( is_wp_error( $rotated ) ) {
			// TODO: Log errors.
		}
	}

	if ( method_exists( $editor, 'make_subsize' ) ) {
		foreach ( $new_sizes as $new_size_name => $new_size_data ) {
			$new_size_meta = $editor->make_subsize( $new_size_data );

			if ( is_wp_error( $new_size_meta ) ) {
				// TODO: Log errors.
			} else {
				// Save the size meta value.
				if ( ! isset( $image_meta['sizes'][ $new_size_name ] ) ) {
					$image_meta['sizes'][ $new_size_name ] = $new_size_meta;
				} else {
					// Remove any newly generated images that are larger than the primary mime type.
					$new_size     = isset( $new_size_meta['filesize'] ) ? $new_size_meta['filesize'] : 0;
					$primary_size = isset( $image_meta['sizes'][ $new_size_name ]['filesize'] ) ? $image_meta['sizes'][ $new_size_name ]['filesize'] : 0;

					if ( $new_size && $primary_size && $new_size >= $primary_size ) {
						wp_delete_file( dirname( $file ) . '/' . $new_size_meta['file'] );
						continue;
					}
				}
				if ( ! isset( $image_meta['sizes'][ $new_size_name ]['sources'] ) ) {
					$image_meta['sizes'][ $new_size_name ]['sources'] = array();
				}
				$image_meta['sizes'][ $new_size_name ]['sources'][ $mime_type ] = _wp_get_sources_from_meta( $new_size_meta );
				wp_update_attachment_metadata( $attachment_id, $image_meta );
			}
		}
	} else {
		// Fall back to `$editor->multi_resize()`.
		$created_sizes = $editor->multi_resize( $new_sizes );

		if ( ! empty( $created_sizes ) ) {
			foreach ( $created_sizes as $created_size_name => $created_size_meta ) {

				// Primary mime type is set in 'sizes' array.
				if ( ! isset( $image_meta['sizes'][ $created_size_name ] ) ) {
					$image_meta['sizes'][ $created_size_name ] = $created_size_meta;
				} else {
					// Remove any newly generated images that are larger than the primary mime type.
					$new_size     = isset( $created_size_meta['filesize'] ) ? $created_size_meta['filesize'] : 0;
					$primary_size = isset( $image_meta['sizes'][ $created_size_name ]['filesize'] ) ? $image_meta['sizes'][ $created_size_name ]['filesize'] : 0;

					if ( $new_size && $primary_size && $new_size >= $primary_size ) {
						wp_delete_file( dirname( $file ) . '/' . $created_size_meta['file'] );
						continue;
					}
				}
				if ( ! isset( $image_meta['sizes'][ $created_size_name ]['sources'] ) ) {
					$image_meta['sizes'][ $created_size_name ]['sources'] = array();
				}
				$image_meta['sizes'][ $created_size_name ]['sources'][ $mime_type ] = _wp_get_sources_from_meta( $created_size_meta );
			}
			wp_update_attachment_metadata( $attachment_id, $image_meta );
		}
	}

	return $image_meta;
}