Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Working remotely, as usual

Adam Silverstein adamsilverstein

💭
Working remotely, as usual
View GitHub Profile
@adamsilverstein
adamsilverstein / output-webp.php
Last active Sep 30, 2022
Output WebP mini plugin
View output-webp.php
<?php
/**
* Outout WebP sub size images.
*
* @wordpress-plugin
* Plugin Name: WebP-only.
* Description: Output WebP sub size images.
* Plugin URI:
* Version: 1.0.0
* Author: Adam Silverstein, Google
@adamsilverstein
adamsilverstein / only-webp.php
Last active Aug 30, 2022
only webp with performance plugin
View only-webp.php
<?php
/**
* Outout only WebP sub size images.
*
* @wordpress-plugin
* Plugin Name: WebP-only.
* Description: Outout only WebP sub size images.
* Plugin URI:
* Version: 1.0.0
* Author: Adam Silverstein, Google
@adamsilverstein
adamsilverstein / set-82.php
Created Aug 29, 2022
Set all quality to 82
View set-82.php
<?php
/**
* Use the `wp_editor_set_quality` filter for testing.
*/
function set_webp_quality() {
return 82;
}
@adamsilverstein
adamsilverstein / low-webp-quality.php
Last active Aug 25, 2022
Test setting WebP quality
View low-webp-quality.php
<?php
/**
* Use the `wp_editor_set_quality` filter for testing.
*/
function set_webp_quality( $quality, $mime_type ) {
error_log( sprintf( 'wp_editor_set_quality mime type: %s', $mime_type ) );
if ( 'image/webp' === $mime_type ) {
return 1;
}
@adamsilverstein
adamsilverstein / median_image_bytes_webp.sql
Last active Aug 22, 2022
Calculate the median image size for WordPress pages using/not using WebP images. Change mobile to desktop for desktop sizes.
View median_image_bytes_webp.sql
SELECT
has_webp,
APPROX_QUANTILES(bytesImg, 1000)[OFFSET(500)] / 1024 / 1024 AS median_img_mbytes
FROM (
SELECT DISTINCT
url,
info AS version
FROM
`httparchive.technologies.2022_07_01_mobile`
WHERE
@adamsilverstein
adamsilverstein / wp-webp.sql
Created Aug 18, 2022
WebP by WordPress version Query
View wp-webp.sql
SELECT
mobile.version,
ROUND(pct_webp_mobile, 3) AS pct_webp_mobile,
ROUND(pct_webp_desktop, 3) AS pct_webp_desktop
FROM
(
SELECT
version, pct_webp AS pct_webp_mobile
FROM
(
View apply-quality-per-mime.diff
diff --git a/src/wp-includes/class-wp-image-editor.php b/src/wp-includes/class-wp-image-editor.php
index 6b63d7e9fd..e17b787223 100644
--- a/src/wp-includes/class-wp-image-editor.php
+++ b/src/wp-includes/class-wp-image-editor.php
@@ -414,12 +414,10 @@ abstract class WP_Image_Editor {
$filename = trailingslashit( $dir ) . wp_basename( $filename, ".$ext" ) . ".{$new_ext}";
}
- if ( $mime_type && ( $mime_type !== $this->mime_type ) ) {
- // The image will be converted when saving. Set the quality for the new mime-type if not already set.
View detect-webp.js
( function( d, i, s, p ) {
s = d.createElement( s );
s.src = '{$fallback_url}';
i = d.createElement( i );
i.src = p + 'jIAAABXRUJQVlA4ICYAAACyAgCdASoCAAEALmk0mk0iIiIiIgBoSygABc6zbAAA/v56QAAAAA==';
i.onload = function() {
i.src = p + 'h4AAABXRUJQVlA4TBEAAAAvAQAAAAfQ//73v/+BiOh/AAA=';
};
i.onerror = function() {
d.body.appendChild( s );
View drupal-webp-usage-by-version-platform.sql
SELECT
mobile.version,
ROUND(pct_webp_mobile, 3) AS pct_webp_mobile,
ROUND(pct_webp_desktop, 3) AS pct_webp_desktop
FROM
(
SELECT
version, pct_webp AS pct_webp_mobile
FROM
(
View webp 90 quality
➜ ll
total 45600
166K 2 14:14 sample0-1024x576.jpg
118K 2 14:14 sample0-1024x576.webp
168K 2 14:14 sample0-1038x576.jpg
120K 2 14:15 sample0-1038x576.webp
28K 2 14:14 sample0-150x150.jpg
7.0K 2 14:14 sample0-150x150.webp
304K 2 14:14 sample0-1536x864.jpg
220K 2 14:15 sample0-1536x864.webp