class-activation.php
7 months ago
class-admin-menu-organizer.php
7 months ago
class-admin-menu-svg-icon-mask.php
7 months ago
class-auto-publish-posts-with-missed-schedule.php
7 months ago
class-avif-upload.php
7 months ago
class-captcha-protection.php
7 months ago
class-change-login-url.php
7 months ago
class-cleanup-admin-bar.php
7 months ago
class-common-methods.php
7 months ago
class-content-duplication.php
7 months ago
class-content-order.php
7 months ago
class-custom-admin-footer-text.php
7 months ago
class-custom-body-class.php
7 months ago
class-custom-css.php
7 months ago
class-custom-nav-menu-items-in-new-tab.php
7 months ago
class-deactivation.php
7 months ago
class-disable-author-archives.php
7 months ago
class-disable-comments.php
7 months ago
class-disable-dashboard-widgets.php
7 months ago
class-disable-embeds.php
7 months ago
class-disable-feeds.php
7 months ago
class-disable-gutenberg.php
7 months ago
class-disable-rest-api.php
7 months ago
class-disable-smaller-components.php
7 months ago
class-disable-updates.php
7 months ago
class-disable-xml-rpc.php
7 months ago
class-display-system-summary.php
7 months ago
class-email-address-obfuscator.php
7 months ago
class-email-delivery.php
7 months ago
class-enhance-list-tables.php
7 months ago
class-external-permalinks.php
7 months ago
class-heartbeat-control.php
7 months ago
class-hide-admin-bar.php
7 months ago
class-hide-admin-notices.php
7 months ago
class-image-sizes-panel.php
7 months ago
class-image-upload-control.php
7 months ago
class-insert-head-body-footer-code.php
7 months ago
class-last-login-column.php
7 months ago
class-limit-login-attempts.php
7 months ago
class-login-id-type.php
7 months ago
class-login-logout-menu.php
7 months ago
class-maintenance-mode.php
7 months ago
class-manage-ads-appads-txt.php
7 months ago
class-manage-robots-txt.php
7 months ago
class-media-replacement.php
7 months ago
class-multiple-user-roles.php
7 months ago
class-obfuscate-author-slugs.php
7 months ago
class-open-external-links-in-new-tab.php
7 months ago
class-password-protection.php
7 months ago
class-redirect-after-login.php
7 months ago
class-redirect-after-logout.php
7 months ago
class-redirect-fourofour.php
7 months ago
class-registration-date-column.php
7 months ago
class-revisions-control.php
7 months ago
class-search-engines-visibility.php
7 months ago
class-settings-fields-render.php
7 months ago
class-settings-sanitization.php
7 months ago
class-settings-sections-fields.php
7 months ago
class-show-custom-taxonomy-filters.php
7 months ago
class-site-identity-on-login-page.php
7 months ago
class-svg-upload.php
7 months ago
class-various-admin-ui-enhancements.php
7 months ago
class-view-admin-as-role.php
7 months ago
class-wider-admin-menu.php
7 months ago
class-wp-config-transformer.php
7 months ago
class-avif-upload.php
236 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class for AVIF Upload module |
| 7 | * |
| 8 | * @since 6.9.5 |
| 9 | */ |
| 10 | class AVIF_Upload { |
| 11 | |
| 12 | /** |
| 13 | * Add AVIF mime type to list of mime types |
| 14 | * |
| 15 | * @since 5.7.0 |
| 16 | */ |
| 17 | public function add_avif_mime_type( $wp_get_mime_types ) { |
| 18 | |
| 19 | $wp_get_mime_types['avif'] = 'image/avif'; |
| 20 | return $wp_get_mime_types; |
| 21 | |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Add AVIF mime type to allowed mime types |
| 26 | * |
| 27 | * @since 5.7.0 |
| 28 | */ |
| 29 | public function allow_avif_mime_type_upload( $mimes ) { |
| 30 | |
| 31 | $mimes['avif'] = 'image/avif'; |
| 32 | return $mimes; |
| 33 | |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Add AVIF to mapping of mime types to their respective extensions |
| 38 | * |
| 39 | * @since 5.7.0 |
| 40 | */ |
| 41 | public function add_avif_mime_type_to_exts( $mime_to_ext ) { |
| 42 | |
| 43 | $mime_to_ext['image/avif'] = 'avif'; |
| 44 | return $mime_to_ext; |
| 45 | |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Add correct dimension for AVIF images |
| 50 | * |
| 51 | * @link https://plugins.trac.wordpress.org/browser/avif-support/trunk/includes/AvifSupport.php#L104 |
| 52 | * @since 5.7.0 |
| 53 | */ |
| 54 | public function add_avif_image_dimension( $metadata, $attachment_id, $context ) { |
| 55 | |
| 56 | if ( empty( $metadata ) ) { |
| 57 | return $metadata; |
| 58 | } |
| 59 | |
| 60 | $attachment_post = get_post( $attachment_id ); |
| 61 | |
| 62 | if ( ! $attachment_post || is_wp_error( $attachment_post ) ) { |
| 63 | return $metadata; |
| 64 | } |
| 65 | |
| 66 | if ( 'image/avif' !== $attachment_post->post_mime_type ) { |
| 67 | return $metadata; |
| 68 | } |
| 69 | |
| 70 | // Fix width and height |
| 71 | |
| 72 | if ( |
| 73 | ( ! empty( $metadata['width'] ) |
| 74 | && ( 0 !== $metadata['width'] ) ) |
| 75 | && ( ! empty( $metadata['height'] ) |
| 76 | && 0 !== $metadata['height'] ) |
| 77 | ) { |
| 78 | return $metadata; |
| 79 | } |
| 80 | |
| 81 | $file = get_attached_file( $attachment_id ); |
| 82 | |
| 83 | if ( ! $file ) { |
| 84 | return $metadata; |
| 85 | } |
| 86 | |
| 87 | if ( empty( $metadata['width'] ) ) { |
| 88 | $metadata['width'] = 0; |
| 89 | } |
| 90 | |
| 91 | if ( empty( $metadata['height'] ) ) { |
| 92 | $metadata['height'] = 0; |
| 93 | } |
| 94 | |
| 95 | if ( empty( $metadata['file'] ) ) { |
| 96 | $metadata['file'] = _wp_relative_upload_path( $file ); |
| 97 | } |
| 98 | |
| 99 | if ( empty( $metadata['sizes'] ) ) { |
| 100 | $metadata['sizes'] = array(); |
| 101 | } |
| 102 | |
| 103 | $img_size = wp_getimagesize( $file ); |
| 104 | |
| 105 | // Legacy PHP Version, return false, fake it till manual. |
| 106 | if ( empty( $img_size ) ) { |
| 107 | $img_size = array( |
| 108 | 0 => 0, |
| 109 | 1 => 0, |
| 110 | 2 => 19, |
| 111 | 3 => 'width="0" height="0"', |
| 112 | 'mime' => 'image/avif', |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | if ( is_array( $img_size ) && ( 0 !== $img_size[0] ) && ( 0 !== $img_size[1] ) ) { |
| 117 | // Do nothing, we have what we need |
| 118 | } else { |
| 119 | |
| 120 | // Manually get width and height |
| 121 | $binary_string = file_get_contents( $file ); |
| 122 | $ispe_pos = strpos( $binary_string, 'ispe' ); |
| 123 | |
| 124 | if ( false === $ispe_pos ) { |
| 125 | // Corrupted Image. |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | $dim_start_pos = $ispe_pos + 8; |
| 130 | $dim_bin = substr( $binary_string, $dim_start_pos, 8 ); |
| 131 | $width = hexdec( bin2hex( substr( $dim_bin, 0, 4 ) ) ); |
| 132 | $height = hexdec( bin2hex( substr( $dim_bin, 4, 8 ) ) ); |
| 133 | |
| 134 | if ( $width && $height && is_numeric( $width ) && is_numeric( $height ) ) { |
| 135 | $img_size[0] = absint( $width ); |
| 136 | $img_size[1] = absint( $height ); |
| 137 | } |
| 138 | |
| 139 | // wp_getimagesize() failed, try with Imagick |
| 140 | // if ( extension_loaded( 'imagick' ) && class_exists( 'Imagick' ) ) { |
| 141 | // try { |
| 142 | // $imagick = new \Imagick( $file ); |
| 143 | // $img_dim = $imagick->getImageGeometry(); |
| 144 | // $img_size[0] = $img_dim['width']; |
| 145 | // $img_size[1] = $img_dim['height']; |
| 146 | |
| 147 | // $imagick->clear(); |
| 148 | // } catch ( \Exception $e ) { |
| 149 | // // Do nothing for now. |
| 150 | // } |
| 151 | // } |
| 152 | |
| 153 | } |
| 154 | |
| 155 | if ( ! $img_size ) { |
| 156 | $avif_specs = false; |
| 157 | } else { |
| 158 | $file_size = filesize( $file ); |
| 159 | $avif_specs = array( |
| 160 | 'width' => $img_size[0], |
| 161 | 'height' => $img_size[1], |
| 162 | 'mime' => $img_size['mime'], |
| 163 | 'dimension' => $img_size[0] . 'x' . $img_size[1], |
| 164 | 'ext' => str_replace( 'image/', '', $img_size['mime'] ), |
| 165 | 'size' => $file_size, |
| 166 | 'size_format' => size_format( $file_size ), |
| 167 | ); |
| 168 | } |
| 169 | |
| 170 | if ( is_wp_error( $avif_specs ) || ! $avif_specs ) { |
| 171 | return $metadata; |
| 172 | } |
| 173 | |
| 174 | $metadata['width'] = $avif_specs['width']; |
| 175 | $metadata['height'] = $avif_specs['height']; |
| 176 | |
| 177 | return $metadata; |
| 178 | |
| 179 | // Fix scaled version of the image |
| 180 | |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Make sure AVIF files are displayable in the browser |
| 185 | * |
| 186 | * @since 5.7.0 |
| 187 | */ |
| 188 | public function make_avif_displayable( $result, $path ) { |
| 189 | if ( str_ends_with( $path, '.avif' ) ) { |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | return $result; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Handle rare scenarios where exif and fileinfo fail to detect AVIF |
| 198 | * |
| 199 | * @since 5.7.0 |
| 200 | */ |
| 201 | public function handle_exif_and_fileinfo_fail( $wp_check_filetype_and_ext, $file, $filename, $mimes, $real_mime ) { |
| 202 | |
| 203 | // AVIF is properly handled, no need to do anything else |
| 204 | if ( $wp_check_filetype_and_ext['ext'] && $wp_check_filetype_and_ext['type'] ) { |
| 205 | return $wp_check_filetype_and_ext; |
| 206 | } |
| 207 | |
| 208 | // Not an .avif file, no need to do anything else |
| 209 | if ( ! str_ends_with( $filename, '.avif' ) ) { |
| 210 | return $wp_check_filetype_and_ext; |
| 211 | } else { |
| 212 | $binary_string = file_get_contents( $file ); |
| 213 | $ispe_pos = strpos( $binary_string, 'ispe' ); |
| 214 | |
| 215 | if ( false === $ispe_pos ) { |
| 216 | // Corrupted Image. |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | $dim_start_pos = $ispe_pos + 8; |
| 221 | $dim_bin = substr( $binary_string, $dim_start_pos, 8 ); |
| 222 | $width = hexdec( bin2hex( substr( $dim_bin, 0, 4 ) ) ); |
| 223 | $height = hexdec( bin2hex( substr( $dim_bin, 4, 8 ) ) ); |
| 224 | |
| 225 | // If this is a valid image with proper width and height, set filetype and ext to AVIF |
| 226 | if ( $width && $height && is_numeric( $width ) && is_numeric( $height ) ) { |
| 227 | $wp_check_filetype_and_ext['type'] = 'image/avif'; |
| 228 | $wp_check_filetype_and_ext['ext'] = 'avif'; |
| 229 | } |
| 230 | |
| 231 | return $wp_check_filetype_and_ext; |
| 232 | } |
| 233 | |
| 234 | } |
| 235 | |
| 236 | } |