post_mime_type ) { return $metadata; } // Fix width and height if ( ( ! empty( $metadata['width'] ) && ( 0 !== $metadata['width'] ) ) && ( ! empty( $metadata['height'] ) && 0 !== $metadata['height'] ) ) { return $metadata; } $file = get_attached_file( $attachment_id ); if ( ! $file ) { return $metadata; } if ( empty( $metadata['width'] ) ) { $metadata['width'] = 0; } if ( empty( $metadata['height'] ) ) { $metadata['height'] = 0; } if ( empty( $metadata['file'] ) ) { $metadata['file'] = _wp_relative_upload_path( $file ); } if ( empty( $metadata['sizes'] ) ) { $metadata['sizes'] = array(); } $img_size = wp_getimagesize( $file ); // Legacy PHP Version, return false, fake it till manual. if ( empty( $img_size ) ) { $img_size = array( 0 => 0, 1 => 0, 2 => 19, 3 => 'width="0" height="0"', 'mime' => 'image/avif', ); } if ( is_array( $img_size ) && ( 0 !== $img_size[0] ) && ( 0 !== $img_size[1] ) ) { // Do nothing, we have what we need } else { // Only hand-parse dimensions from structurally valid AVIF files. if ( ! $this->has_valid_avif_ftyp( $file ) ) { return $metadata; } // Manually get width and height $binary_string = file_get_contents( $file ); $ispe_pos = strpos( $binary_string, 'ispe' ); if ( false === $ispe_pos ) { // Corrupted Image. return false; } $dim_start_pos = $ispe_pos + 8; $dim_bin = substr( $binary_string, $dim_start_pos, 8 ); $width = hexdec( bin2hex( substr( $dim_bin, 0, 4 ) ) ); $height = hexdec( bin2hex( substr( $dim_bin, 4, 8 ) ) ); if ( $width && $height && is_numeric( $width ) && is_numeric( $height ) ) { $img_size[0] = absint( $width ); $img_size[1] = absint( $height ); } // wp_getimagesize() failed, try with Imagick // if ( extension_loaded( 'imagick' ) && class_exists( 'Imagick' ) ) { // try { // $imagick = new \Imagick( $file ); // $img_dim = $imagick->getImageGeometry(); // $img_size[0] = $img_dim['width']; // $img_size[1] = $img_dim['height']; // $imagick->clear(); // } catch ( \Exception $e ) { // // Do nothing for now. // } // } } if ( ! $img_size ) { $avif_specs = false; } else { $file_size = filesize( $file ); $avif_specs = array( 'width' => $img_size[0], 'height' => $img_size[1], 'mime' => $img_size['mime'], 'dimension' => $img_size[0] . 'x' . $img_size[1], 'ext' => str_replace( 'image/', '', $img_size['mime'] ), 'size' => $file_size, 'size_format' => size_format( $file_size ), ); } if ( is_wp_error( $avif_specs ) || ! $avif_specs ) { return $metadata; } $metadata['width'] = $avif_specs['width']; $metadata['height'] = $avif_specs['height']; return $metadata; // Fix scaled version of the image } /** * Make sure AVIF files are displayable in the browser * * @since 5.7.0 */ public function make_avif_displayable( $result, $path ) { if ( str_ends_with( $path, '.avif' ) ) { return true; } return $result; } /** * Allow AVIF uploads even when GD/Imagick cannot generate sub-sizes. * * WordPress 6.8+ blocks those uploads by default. This module's purpose * is still to accept the original file (browsers can display it). * * @since 9.1.2 * * @param bool $prevent Whether to prevent uploads of unsupported image types. * @param string|null $mime_type The mime type of the file being uploaded (if available). * @return bool */ public function allow_avif_without_editor_support( $prevent, $mime_type ) { if ( 'image/avif' === $mime_type ) { return false; } return $prevent; } /** * Clear Plupload's client-side AVIF block (classic Media Library + media-new.php). * * media_upload_form() calls wp_prevent_unsupported_mime_type_uploads with * $mime_type = null, so the REST-oriented filter above is not enough there. * * @since 9.1.2 * * @param array $settings Plupload settings. * @return array */ public function allow_avif_in_plupload( $settings ) { $settings['avif_upload_error'] = false; return $settings; } /** * Check whether a file is a structurally valid AVIF (ISO-BMFF) via its ftyp box. * * A valid file must begin with a box whose size (bytes 0-3, big-endian) * fits the file, whose type (bytes 4-7) is 'ftyp', and which declares the * 'avif' or 'avis' brand as major brand (bytes 8-11) or a compatible brand * (bytes 16+, within the box). Only the first 64 bytes are read, so spoof * files are rejected cheaply and no whole file is loaded into memory. * * @since 9.1.2 * * @param string $file Absolute path to the file to check. * @return bool True when the file declares a valid AVIF ftyp box. */ private function has_valid_avif_ftyp( $file ) { if ( empty( $file ) || ! is_readable( $file ) ) { return false; } $file_size = filesize( $file ); $header = file_get_contents( $file, false, null, 0, 64 ); // bounded read if ( false === $header || false === $file_size || strlen( $header ) < 16 ) { return false; } // First box must be ftyp, and its declared size must be plausible. // The size check also kills HTML-comment polyglots such as // "