Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert uploaded PNG files to AVIF or WebP #1421

Merged
merged 22 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
150a1f7
Also convert PNGs
adamsilverstein Jul 31, 2024
358e1bf
Adjust defaults for test_it_should_return_default_transforms_when_fil…
adamsilverstein Jul 31, 2024
998f77c
Merge branch 'trunk' into add/png-conversion
adamsilverstein Jul 31, 2024
e8a9e4f
Merge branch 'trunk' into add/png-conversion
adamsilverstein Aug 5, 2024
052e588
Merge branch 'trunk' into add/png-conversion
adamsilverstein Aug 8, 2024
7c5d47d
Change language for fallback toggle, removing JPEG references
adamsilverstein Aug 8, 2024
bce1879
Output PNG when fallback enabled
adamsilverstein Aug 8, 2024
8e21acc
Update tests to include png transforms
adamsilverstein Aug 8, 2024
85c829a
Refine docs and checkbox for fallback image
adamsilverstein Aug 8, 2024
5c77ad6
update picture element description
adamsilverstein Aug 8, 2024
06b36f1
Enable PNG in picture element
adamsilverstein Aug 8, 2024
0b14a5b
update doc block for webp_uploads_picture_element_mime_types filter
adamsilverstein Aug 8, 2024
a4ae0c9
remove reference to JPEG in uploaded image handling
adamsilverstein Aug 8, 2024
1854ba7
Merge branch 'trunk' into add/png-conversion
adamsilverstein Aug 13, 2024
35cf716
Merge branch 'trunk' into add/png-conversion
adamsilverstein Aug 26, 2024
c8e613c
remove merge artifact
adamsilverstein Aug 26, 2024
18b760d
small language fine tuning
adamsilverstein Aug 26, 2024
0a9b072
Merge branch 'trunk' into add/png-conversion
adamsilverstein Aug 28, 2024
5189b35
Update plugins/webp-uploads/picture-element.php
adamsilverstein Aug 29, 2024
3d60d82
Rename webp_uploads_is_jpeg_fallback_enabled->webp_uploads_is_fallbac…
adamsilverstein Aug 30, 2024
9bf832e
Add some additional @since notations
adamsilverstein Sep 4, 2024
74003e0
Merge branch 'trunk' into add/png-conversion
adamsilverstein Sep 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugins/webp-uploads/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ function webp_uploads_get_upload_image_mime_transforms(): array {
'image/jpeg' => array( 'image/' . $output_format ),
'image/webp' => array( 'image/webp' ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/' . $output_format ),
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
);

// Check setting for whether to generate both JPEG and the modern output format.
if ( webp_uploads_is_jpeg_fallback_enabled() ) {
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
$default_transforms = array(
'image/jpeg' => array( 'image/jpeg', 'image/' . $output_format ),
'image/png' => array( 'image/png', 'image/' . $output_format ),
'image/' . $output_format => array( 'image/' . $output_format, 'image/jpeg' ),
);
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/webp-uploads/picture-element.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function webp_uploads_wrap_image_in_picture( string $image, string $context, int
/**
* Filter the image mime types that can be used for the <picture> element.
*
* Default is: ['image/avif', 'image/webp']. Returning an empty array will skip using the `picture` element.
* Default is: ['image/avif', 'image/webp', 'image/jpeg', 'image/png]. Returning an empty array will skip using the `picture` element.
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
*
* The mime types will output in the picture element in the order they are provided.
* The original image will be used as the fallback image for browsers that don't support the picture element.
Expand Down
2 changes: 1 addition & 1 deletion plugins/webp-uploads/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Converts images to more modern formats such as WebP or AVIF during upload.

This plugin adds WebP and AVIF support for media uploads within the WordPress application. By default, AVIF images will be generated if supported on the hosting server, otherwise WebP will be used as the output format. When both formats are available, the output format can be selected under `Settings > Media`. Modern images will be generated only for new uploads, pre-existing images will only converted to a modern format if images are regenerated. Images can be regenerated with a plugin like [Regenerate Thumbnails](https://wordpress.org/plugins/regenerate-thumbnails/) or via WP-CLI with the `wp media regenerate` [command](https://developer.wordpress.org/cli/commands/media/regenerate/).

By default, only modern image format sub-sizes will be generated for JPEG uploads - only the original uploaded file will still exist as a JPEG image, generated image sizes use be WebP or AVIF files. To change this behavior, there is a checkbox in `Settings > Media` "Also output JPEG" that - when checked - will result in the plugin generating both JPEG and WebP or AVIF images for every sub-size (noting again that this will only affect newly uploaded images, i.e. after making said change).
By default, only modern image format sub-sizes will be generated for JPEG or PNG uploads - only the original uploaded file will still exist as a JPEG/PNG image, generated image sizes will be WebP or AVIF files. To change this behavior, there is a checkbox in `Settings > Media` "Output fallback images" that - when checked - will result in the plugin generating both the original format as well as WebP or AVIF images for every sub-size (noting again that this will only affect newly uploaded images, i.e. after making said change).

_This plugin was formerly known as WebP Uploads._

Expand Down
18 changes: 9 additions & 9 deletions plugins/webp-uploads/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ function webp_uploads_add_media_settings_fields(): void {
return;
}

// Add JPEG Output settings field.
// Add fallback image output settings field.
add_settings_field(
'perflab_generate_webp_and_jpeg',
__( 'Also output JPEG', 'webp-uploads' ),
__( 'Output fallback images', 'webp-uploads' ),
'webp_uploads_generate_webp_jpeg_setting_callback',
'media',
'perflab_modern_image_format_settings',
Expand Down Expand Up @@ -145,7 +145,7 @@ function webp_uploads_generate_avif_webp_setting_callback(): void {
<label for="perflab_modern_image_format">
<?php esc_html_e( 'Generate images in this format', 'webp-uploads' ); ?>
</label>
<p class="description" id="perflab_modern_image_format_description"><?php esc_html_e( 'Select the format to use when generating new images from uploaded JPEGs.', 'webp-uploads' ); ?></p>
<p class="description" id="perflab_modern_image_format_description"><?php esc_html_e( 'Select the format to use when generating new images from uploaded images.', 'webp-uploads' ); ?></p>
<?php if ( ! $avif_supported ) : ?>
<br />
<div class="notice notice-warning inline">
Expand All @@ -172,9 +172,9 @@ function webp_uploads_generate_webp_jpeg_setting_callback(): void {
?>
<label for="perflab_generate_webp_and_jpeg">
<input name="perflab_generate_webp_and_jpeg" type="checkbox" id="perflab_generate_webp_and_jpeg" aria-describedby="perflab_generate_webp_and_jpeg_description" value="1"<?php checked( '1', get_option( 'perflab_generate_webp_and_jpeg' ) ); ?> />
<?php esc_html_e( 'Output JPEG images in addition to the modern format', 'webp-uploads' ); ?>
<?php esc_html_e( 'Also generate fallback images in the original upload format', 'webp-uploads' ); ?>
</label>
<p class="description" id="perflab_generate_webp_and_jpeg_description"><?php esc_html_e( 'Enabling JPEG output can improve compatibility, but will increase the filesystem storage use of your images.', 'webp-uploads' ); ?></p>
<p class="description" id="perflab_generate_webp_and_jpeg_description"><?php esc_html_e( 'Enabling fallback image output can improve compatibility, but will increase the filesystem storage use of your images.', 'webp-uploads' ); ?></p>
<?php
}

Expand All @@ -196,7 +196,7 @@ function webp_uploads_use_picture_element_callback(): void {
}
</style>
<div id="webp_uploads_picture_element_notice" class="notice notice-info inline" <?php echo $jpeg_fallback_enabled ? 'hidden' : ''; ?>>
<p><?php esc_html_e( 'This setting requires JPEG also be output as a fallback option.', 'webp-uploads' ); ?></p>
<p><?php esc_html_e( 'This setting requires fallback image output to be enabled.', 'webp-uploads' ); ?></p>
</div>
<div id="webp_uploads_picture_element_fieldset" class="<?php echo ! $jpeg_fallback_enabled ? 'disabled' : ''; ?>">
<label for="webp_uploads_use_picture_element" id="webp_uploads_use_picture_element_label">
Expand Down Expand Up @@ -228,9 +228,9 @@ function webp_uploads_use_picture_element_callback(): void {
?>
<em><?php esc_html_e( '(experimental)', 'webp-uploads' ); ?></em>
</label>
<p class="description" id="webp_uploads_use_picture_element_description"><?php esc_html_e( 'The picture element serves a modern image format with a fallback to JPEG. Warning: Make sure you test your theme and plugins for compatibility. In particular, CSS selectors will not match images when using the child combinator (e.g. figure > img).', 'webp-uploads' ); ?></p>
<p class="description" id="webp_uploads_use_picture_element_description"><?php esc_html_e( 'The picture element serves a modern image format with a fallback to the original upload format. Warning: Make sure you test your theme and plugins for compatibility. In particular, CSS selectors will not match images when using the child combinator (e.g. figure > img).', 'webp-uploads' ); ?></p>
<div id="webp_uploads_jpeg_fallback_notice" class="notice notice-info inline" <?php echo $picture_element_option ? '' : 'hidden'; ?>>
<p><?php esc_html_e( 'Picture elements will only be used when JPEG fallback images are available. So this will not apply to any images you may have uploaded while the "Also generate JPEG" setting was disabled.', 'webp-uploads' ); ?></p>
<p><?php esc_html_e( 'Picture elements will only be used when fallback images are available. So this will only apply to images you have uploaded while the "Also generate fallback images" setting was enabled.', 'webp-uploads' ); ?></p>
</div>
</div>
<script>
Expand All @@ -239,7 +239,7 @@ function webp_uploads_use_picture_element_callback(): void {
document.getElementById( 'webp_uploads_jpeg_fallback_notice' ).hidden = ! this.checked;
} );

// Listen for clicks on the JPEG output checkbox, enabling/disabling the
// Listen for clicks on the fallback output checkbox, enabling/disabling the
// picture element checkbox accordingly.
document.getElementById( 'perflab_generate_webp_and_jpeg' ).addEventListener( 'change', function () {
document.querySelector( '.webp-uploads-use-picture-element' ).classList.toggle( 'webp-uploads-disabled', ! this.checked );
Expand Down
4 changes: 4 additions & 0 deletions plugins/webp-uploads/tests/test-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,14 @@ public function test_it_should_return_default_transforms_when_filter_returns_non
'image/jpeg' => array( 'image/avif' ),
'image/webp' => array( 'image/webp' ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/avif' ),
);
} else {
$default_transforms = array(
'image/jpeg' => array( 'image/webp' ),
'image/webp' => array( 'image/webp' ),
'image/avif' => array( 'image/avif' ),
'image/png' => array( 'image/webp' ),
);
}

Expand Down Expand Up @@ -431,6 +433,7 @@ public function test_it_should_return_jpeg_and_webp_transforms_when_option_gener
$this->assertSame(
array(
'image/jpeg' => array( 'image/jpeg', 'image/avif' ),
'image/png' => array( 'image/png', 'image/avif' ),
'image/avif' => array( 'image/avif', 'image/jpeg' ),
),
$transforms
Expand All @@ -439,6 +442,7 @@ public function test_it_should_return_jpeg_and_webp_transforms_when_option_gener
$this->assertSame(
array(
'image/jpeg' => array( 'image/jpeg', 'image/webp' ),
'image/png' => array( 'image/png', 'image/webp' ),
'image/webp' => array( 'image/webp', 'image/jpeg' ),
),
$transforms
Expand Down
Loading