Skip to content

Commit 575ce15

Browse files
Media: enable AVIF support.
Add support for uploading, editing and saving AVIF images when supported by the server. Add 'image/avif' to supported mime types. Correctly identify AVIF images and sizes even when PHP doesn't support AVIF. Resize uploaded AVIF files (when supported) and use for front end markup. Props adamsilverstein, lukefiretoss, ayeshrajans, navjotjsingh, Tyrannous, jb510, gregbenz, nickpagz, JavierCasares, mukesh27, yguyon, swissspidy. Fixes #51228. git-svn-id: https://develop.svn.wordpress.org/trunk@57524 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e9e2281 commit 575ce15

32 files changed

Lines changed: 1178 additions & 19 deletions

phpcs.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<exclude-pattern>/src/wp-includes/class-requests\.php</exclude-pattern>
6060
<exclude-pattern>/src/wp-includes/class-simplepie\.php</exclude-pattern>
6161
<exclude-pattern>/src/wp-includes/class-snoopy\.php</exclude-pattern>
62+
<exclude-pattern>/src/wp-includes/class-avif-info\.php</exclude-pattern>
6263
<exclude-pattern>/src/wp-includes/deprecated\.php</exclude-pattern>
6364
<exclude-pattern>/src/wp-includes/ms-deprecated\.php</exclude-pattern>
6465
<exclude-pattern>/src/wp-includes/pluggable-deprecated\.php</exclude-pattern>

src/js/_enqueues/vendor/plupload/handlers.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,11 @@ jQuery( document ).ready( function( $ ) {
608608
wpQueueError( pluploadL10n.noneditable_image );
609609
up.removeFile( file );
610610
return;
611+
} else if ( file.type === 'image/avif' && up.settings.avif_upload_error ) {
612+
// Disallow uploading of AVIF images if the server cannot edit them.
613+
wpQueueError( pluploadL10n.noneditable_image );
614+
up.removeFile( file );
615+
return;
611616
}
612617

613618
fileQueued( file );

src/js/_enqueues/vendor/plupload/wp-plupload.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ window.wp = window.wp || {};
363363
error( pluploadL10n.noneditable_image, {}, file, 'no-retry' );
364364
up.removeFile( file );
365365
return;
366+
} else if ( file.type === 'image/avif' && up.settings.avif_upload_error ) {
367+
// Disallow uploading of AVIF images if the server cannot edit them.
368+
error( pluploadL10n.noneditable_image, {}, file, 'no-retry' );
369+
up.removeFile( file );
370+
return;
366371
}
367372

368373
// Generate attributes for a new `Attachment` model.

src/js/_enqueues/vendor/thickbox/thickbox.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,16 @@ function tb_show(caption, url, imageGroup) {//function called when the user clic
7676
baseURL = url;
7777
}
7878

79-
var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$|\.webp$/;
79+
var urlString = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$|\.webp$|\.avif$/;
8080
var urlType = baseURL.toLowerCase().match(urlString);
8181

8282
if(urlType == '.jpg' ||
8383
urlType == '.jpeg' ||
8484
urlType == '.png' ||
8585
urlType == '.gif' ||
8686
urlType == '.bmp' ||
87-
urlType == '.webp'
87+
urlType == '.webp' ||
88+
urlType == '.avif'
8889
){//code to show images
8990

9091
TB_PrevCaption = "";

src/js/media/controllers/library.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ Library = wp.media.controller.State.extend(/** @lends wp.media.controller.Librar
196196
isImageAttachment: function( attachment ) {
197197
// If uploading, we know the filename but not the mime type.
198198
if ( attachment.get('uploading') ) {
199-
return /\.(jpe?g|png|gif|webp)$/i.test( attachment.get('filename') );
199+
return /\.(jpe?g|png|gif|webp|avif)$/i.test( attachment.get('filename') );
200200
}
201201

202202
return attachment.get('type') === 'image';

src/wp-admin/includes/image-edit.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,12 @@ function wp_stream_image( $image, $mime_type, $attachment_id ) {
390390
return imagewebp( $image, null, 90 );
391391
}
392392
return false;
393+
case 'image/avif':
394+
if ( function_exists( 'imageavif' ) ) {
395+
header( 'Content-Type: image/avif' );
396+
return imageavif( $image, null, 90 );
397+
}
398+
return false;
393399
default:
394400
return false;
395401
}
@@ -494,6 +500,11 @@ function wp_save_image_file( $filename, $image, $mime_type, $post_id ) {
494500
return imagewebp( $image, $filename );
495501
}
496502
return false;
503+
case 'image/avif':
504+
if ( function_exists( 'imageavif' ) ) {
505+
return imageavif( $image, $filename );
506+
}
507+
return false;
497508
default:
498509
return false;
499510
}

src/wp-admin/includes/image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ function file_is_valid_image( $path ) {
10061006
* @return bool True if suitable, false if not suitable.
10071007
*/
10081008
function file_is_displayable_image( $path ) {
1009-
$displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_ICO, IMAGETYPE_WEBP );
1009+
$displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP, IMAGETYPE_ICO, IMAGETYPE_WEBP, IMAGETYPE_AVIF );
10101010

10111011
$info = wp_getimagesize( $path );
10121012
if ( empty( $info ) ) {

src/wp-admin/includes/media.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,6 +2198,11 @@ function media_upload_form( $errors = null ) {
21982198
$plupload_init['webp_upload_error'] = true;
21992199
}
22002200

2201+
// Check if AVIF images can be edited.
2202+
if ( ! wp_image_editor_supports( array( 'mime_type' => 'image/avif' ) ) ) {
2203+
$plupload_init['avif_upload_error'] = true;
2204+
}
2205+
22012206
/**
22022207
* Filters the default Plupload settings.
22032208
*

src/wp-admin/includes/schema.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,7 @@ function populate_network_meta( $network_id, array $meta = array() ) {
12501250
'png',
12511251
'gif',
12521252
'webp',
1253+
'avif',
12531254
// Video.
12541255
'mov',
12551256
'avi',

0 commit comments

Comments
 (0)