HEX
Server: nginx
System: Linux pool64-304-45.dca.atomicsites.net 5.10.0-31-amd64 #1 SMP Debian 5.10.221-1 (2024-07-14) x86_64
User: (0)
PHP: 8.4.18
Disabled: pcntl_fork
Upload Files
File: //wordpress/mu-plugins/photon-subsizes/trait.photon-subsizes-image-editor.php
<?php

/**
 * Trait that overrides image editor methods to skip the generation of intermediate image files
 */
trait Photon_Subsizes_Image_Editor {
	/**
	 * Answers whether the current MIME type is supported by Photon
	 */
	public function is_photon_mime_type() {
		$mime_type = $this->mime_type;
		if ( ! $mime_type ) {
			$file_extension = pathinfo( $this->file, PATHINFO_EXTENSION );
			$mime_type = static::get_mime_type( $file_extension );
		}
		return photon_subsizes_is_photon_mime_type( $mime_type );
	}

	/**
	 * Legacy method for creating intermediate images
	 * 
	 * `make_subsize()` is preferred but not necessarily implemented by all image editors.
	 * We provide an implementation for `make_subsize()` in a separate trait.
	 */
	public function multi_resize( $sizes ) {
		if ( ! $this->is_photon_mime_type() ) {
			return parent::multi_resize( $sizes );
		}

		$metadata = array();

		foreach ( $sizes as $size => $size_data ) {
			$meta = $this->_make_photon_subsize( $size_data );

			if ( ! is_wp_error( $meta ) ) {
				$metadata[ $size ] = $meta;
			}
		}

		return $metadata;
	}

	/**
	 * Creates subsize metadata without creating the actual intermediate image files
	 */
	public function _make_photon_subsize( $size_data ) {
		if ( ! isset( $size_data['width'] ) && ! isset( $size_data['height'] ) ) {
			return new WP_Error( 'image_subsize_create_error', __( 'Cannot resize the image. Both width and height are not set.' ) );
		}

		if ( ! isset( $size_data['width'] ) ) {
			$size_data['width'] = null;
		}
		if ( ! isset( $size_data['height'] ) ) {
			$size_data['height'] = null;
		}
		if ( ! isset( $size_data['crop'] ) ) {
			$size_data['crop'] = false;
		}

		/* dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h */
		$dims = image_resize_dimensions( $this->size['width'], $this->size['height'], $size_data['width'], $size_data['height'], $size_data['crop'] );
		if ( ! $dims || count( $dims ) < 8 ) {
			return new WP_Error( 'error_getting_dimensions', __( 'Could not calculate resized image dimensions' ), $this->file );
		}
		$resized_width = $dims[4];
		$resized_height = $dims[5];

		// Use freestanding block to be clear exacly how long we are temporarily modifying the instance's `size` property
		{
			$orig_size = $this->size;
			$this->size = array(
				'width' => $resized_width,
				'height' => $resized_height
			);
			list( $filename, $extension, $mime_type ) = $this->get_output_format();
			if ( ! $filename ) {
				$filename = $this->generate_filename( null, null, $extension );
			}
			$this->size = $orig_size;
		}

		$subsize_meta = array(
			// NOTE: Core filters the filename through the `image_make_intermediate_size` filter,
			// but since the file doesn't really exist, we skip the filter to avoid causing problems.
			'file'      => wp_basename( $filename ),
			'width'     => $resized_width,
			'height'    => $resized_height,
			'mime-type' => $mime_type,
			'virtual'   => true,
		); 

		return $subsize_meta;
	}
}