Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# TinyPNG - JPEG, PNG & WebP image compression for WordPress
# TinyPNG - JPEG, PNG, WebP, AVIF & JPEG XL image compression for WordPress

[![Integration Tests](https://github.com/tinify/wordpress-plugin/actions/workflows/integration-tests.yml/badge.svg)](https://github.com/tinify/wordpress-plugin/actions/workflows/integration-tests.yml)
[![Check and Test](https://github.com/tinify/wordpress-plugin/actions/workflows/check-test.yml/badge.svg)](https://github.com/tinify/wordpress-plugin/actions/workflows/check-test.yml)

Make your website faster by optimizing your JPEG, PNG, and WebP images.
Make your website faster by optimizing your JPEG, PNG, WebP, AVIF and JPEG XL images.

This plugin automatically optimizes your images by integrating with the
popular image compression services TinyJPG and TinyPNG. You can download the
Expand Down
15 changes: 10 additions & 5 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ Stable tag: 3.8.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Speed up your website. Optimize your JPEG, PNG, and WebP images automatically with TinyPNG.
Speed up your website. Optimize your JPEG, PNG, WebP, AVIF and JPEG XL images automatically with TinyPNG.

== Description ==

The best & easiest image compression plugin for WordPress. Speed up your website by compressing and converting your images with TinyPNG for WordPress. Improve your websites’ performance by significantly reducing file sizes without noticeable quality loss. Support for WebP, AVIF, JPG and PNG formats.
The best & easiest image compression plugin for WordPress. Speed up your website by compressing and converting your images with TinyPNG for WordPress. Improve your websites’ performance by significantly reducing file sizes without noticeable quality loss. Support for WebP, AVIF, JPEG XL, JPG and PNG formats.
Start optimizing your images today to boost SEO, improve your PageSpeed scores, save bandwidth and enhance your website’s user experience.

= Why TinyPNG? =

* Next-gen format conversion, convert images to WebP and AVIF for even smaller files (NEW)
* Smart compression algorithm, reduce image file sizes without visible quality loss
* Broad format support, supports AVIF, WebP, PNG, animated PNG (APNG) and JPG.
* Broad format support, supports AVIF, JPEG XL, WebP, PNG, animated PNG (APNG) and JPG.
* Automatic image compression, compresses new images on upload.
* Bulk optimization, optimize your full media library in just one click.
* Individual image control, optimize a single image straight from your media library.
Expand All @@ -43,9 +43,11 @@ Google’s PageSpeed Insights and other tools strongly recommend serving images

With the TinyPNG WordPress plugin, your images can be automatically converted to AVIF or WebP when you upload them. No extra steps are required.

AVIF images are already in a next-gen format, so they are compressed but not converted.

= Optimizing all your images =

You can bulk optimize your existing JPEG, PNG, and WebP images all at once by going to Media > Bulk TinyPNG. Clicking on the big button will start compressing and converting, when needed, all unoptimized images in your media library.
You can bulk optimize your existing JPEG, PNG, WebP, AVIF and JPEG XL images all at once by going to Media > Bulk TinyPNG. Clicking on the big button will start compressing and converting, when needed, all unoptimized images in your media library.

If you have more images than your free credits allow, the tool will show you an estimated cost to optimize your entire library. If you are interested, you can upgrade your account to the paid plan and optimize the full library all in one go. Prefer to stay on the free plan? No problem! The bulk optimization tool will simply process images until your free limit is reached.

Expand Down Expand Up @@ -171,7 +173,10 @@ A: Yes! After installing the plugin, go to *Media > Bulk TinyPNG*, and click on
A: You can upgrade to a paid account by adding your *Payment details* on your [account dashboard](https://tinypng.com/dashboard/api). Additional compressions above 500 will then be charged at the end of each month as a one-time fee.

= Q: Does converting to AVIF or WebP use more credits? =
A: When the conversion feature is enabled (to convert images to AVIF or WebP), each image will use double the number of credits: one for compression and one for format conversion.
A: When the conversion feature is enabled (to convert images to AVIF or WebP), each image will use double the number of credits: one for compression and one for format conversion. AVIF images are not converted, so they use one credit per size.

= Q: Can I optimize JPEG XL images? =
A: Yes, if your site accepts JPEG XL uploads. WordPress does not accept them by default, and the plugin does not change that. WordPress cannot create thumbnails of a JPEG XL image, so only the uploaded image is compressed. Not every browser can display JPEG XL; with the conversion feature enabled, an AVIF or WebP version is created and served instead.

== Changelog ==
= 3.8.1 =
Expand Down
25 changes: 20 additions & 5 deletions src/class-tiny-apache-rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,21 @@ public static function toggle_rules( $old_value, $value, $option ) {
}
}

/**
* Reinstalls the rules when images are delivered with htaccess, so a
* plugin update can change the installed rules.
*
* @return void
*/
public static function refresh_rules() {
$convert_format = get_option( 'tinypng_convert_format' );
if ( isset( $convert_format['delivery_method'] ) &&
'htaccess' === $convert_format['delivery_method'] ) {
$installed = self::install_rules();
Tiny_Logger::debug( 'htaccess rules refreshed: ' . $installed );
}
}

/**
* Generate .htaccess rewrite rules for serving WebP and AVIF images.
*
Expand All @@ -99,7 +114,7 @@ private static function get_rewrite_rules() {
$rules[] = '</IfModule>';

$rules[] = '<IfModule mod_headers.c>';
$rules[] = '<FilesMatch "\.(jpe?g|png)$">';
$rules[] = '<FilesMatch "\.(jpe?g|png|jxl)$">';
$rules[] = 'Header append Vary Accept';
$rules[] = '</FilesMatch>';
$rules[] = '</IfModule>';
Expand All @@ -120,10 +135,10 @@ private static function get_rewrite_rules() {
private static function get_avif_rules() {
$rules = array();
$rules[] = 'RewriteCond %{HTTP_ACCEPT} image/avif';
$rules[] = 'RewriteCond %{REQUEST_URI} ^(.+)\.(?:jpe?g|png)$';
$rules[] = 'RewriteCond %{REQUEST_URI} ^(.+)\.(?:jpe?g|png|jxl)$';
$rules[] = 'RewriteCond %{DOCUMENT_ROOT}/%1.avif -f';
$rules[] = 'RewriteCond %{QUERY_STRING} !type=original';
$rules[] = 'RewriteRule (.+)\.(?:jpe?g|png)$ $1.avif [T=image/avif,L]';
$rules[] = 'RewriteRule (.+)\.(?:jpe?g|png|jxl)$ $1.avif [T=image/avif,L]';
return $rules;
}

Expand All @@ -135,10 +150,10 @@ private static function get_avif_rules() {
private static function get_webp_rules() {
$rules = array();
$rules[] = 'RewriteCond %{HTTP_ACCEPT} image/webp';
$rules[] = 'RewriteCond %{REQUEST_URI} ^(.+)\.(?:jpe?g|png)$';
$rules[] = 'RewriteCond %{REQUEST_URI} ^(.+)\.(?:jpe?g|png|jxl)$';
$rules[] = 'RewriteCond %{DOCUMENT_ROOT}/%1.webp -f';
$rules[] = 'RewriteCond %{QUERY_STRING} !type=original';
$rules[] = 'RewriteRule (.+)\.(?:jpe?g|png)$ $1.webp [T=image/webp,L]';
$rules[] = 'RewriteRule (.+)\.(?:jpe?g|png|jxl)$ $1.webp [T=image/webp,L]';

return $rules;
}
Expand Down
16 changes: 9 additions & 7 deletions src/class-tiny-bulk-optimization.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ private static function wpdb_retrieve_images_and_metadata( $start_id ) {
global $wpdb;

// Retrieve posts that have "_wp_attachment_metadata" image metadata
// and optionally contain "tiny_compress_images" metadata.
$sql_start_id = ( $start_id ? " $wpdb->posts.ID < $start_id AND " : '' );
$query =
// and optionally contain "tiny_compress_images" metadata. Before
// WordPress 6.0, a JPEG XL has no attachment metadata.
$sql_start_id = ( $start_id ? " $wpdb->posts.ID < $start_id AND " : '' );
$sql_mime_types = "'" . implode( "', '", Tiny_Image::compressible_mime_types() ) . "'";
$query =
"SELECT
$wpdb->posts.ID,
$wpdb->posts.post_title,
Expand All @@ -77,6 +79,7 @@ private static function wpdb_retrieve_images_and_metadata( $start_id ) {
FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta
ON $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = '_wp_attachment_metadata'
LEFT JOIN $wpdb->postmeta AS wp_postmeta_file
ON $wpdb->posts.ID = wp_postmeta_file.post_id
AND wp_postmeta_file.meta_key = '_wp_attached_file'
Expand All @@ -86,12 +89,11 @@ private static function wpdb_retrieve_images_and_metadata( $start_id ) {
WHERE
$sql_start_id
$wpdb->posts.post_type = 'attachment'
AND $wpdb->posts.post_mime_type IN ( $sql_mime_types )
AND (
$wpdb->posts.post_mime_type = 'image/jpeg' OR
$wpdb->posts.post_mime_type = 'image/png' OR
$wpdb->posts.post_mime_type = 'image/webp'
$wpdb->postmeta.meta_id IS NOT NULL
OR $wpdb->posts.post_mime_type = 'image/jxl'
)
AND $wpdb->postmeta.meta_key = '_wp_attachment_metadata'
GROUP BY unique_attachment_name
ORDER BY ID DESC
LIMIT " . self::PAGING_SIZE;
Expand Down
3 changes: 1 addition & 2 deletions src/class-tiny-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ private function is_valid_attachment( $attachment_id ) {
return false;
}

$supported_types = array( 'image/jpeg', 'image/png', 'image/webp' );
if ( ! in_array( $mime_type, $supported_types, true ) ) {
if ( ! in_array( $mime_type, Tiny_Image::compressible_mime_types(), true ) ) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions src/class-tiny-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Tiny_Helpers {
* @return string the truncated string
*/
public static function truncate_text( $text, $length ) {
if ( mb_strlen( $text ) > $length ) {
return mb_substr( $text, 0, $length - 3 ) . '...';
if ( mb_strlen( $text, 'UTF-8' ) > $length ) {
return mb_substr( $text, 0, $length - 3, 'UTF-8' ) . '...';
}
return $text;
}
Expand Down
81 changes: 63 additions & 18 deletions src/class-tiny-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ private function parse_wp_metadata() {
if ( ! is_array( $this->wp_metadata ) ) {
$this->wp_metadata = wp_get_attachment_metadata( $this->id );
}
if ( ! is_array( $this->wp_metadata ) && 'image/jxl' === $this->get_mime_type() ) {
// Before WordPress 6.0, a JPEG XL has no attachment metadata.
$this->wp_metadata = array();
}

if ( ! is_array( $this->wp_metadata ) || ! isset( $this->wp_metadata['file'] ) ) {
$file = $this->get_metadata_file();
if ( null === $file ) {
/*
No file metadata found, this might be another plugin messing with
metadata. Simply ignore this! */
Expand All @@ -60,15 +65,15 @@ private function parse_wp_metadata() {

$upload_dir = wp_upload_dir();
$path_prefix = $upload_dir['basedir'] . '/';
$path_info = pathinfo( $this->wp_metadata['file'] );
$path_info = pathinfo( $file );
if ( isset( $path_info['dirname'] ) ) {
$path_prefix .= $path_info['dirname'] . '/';
}

/*
Do not use pathinfo for getting the filename.
It doesn't work when the filename starts with a special character. */
$path_parts = explode( '/', $this->wp_metadata['file'] );
$path_parts = explode( '/', $file );
$this->name = end( $path_parts );
$filename = $path_prefix . $this->name;
$this->sizes[ self::ORIGINAL ] = new Tiny_Image_Size( $filename );
Expand Down Expand Up @@ -100,6 +105,26 @@ private function parse_wp_metadata() {
$this->wp_metadata['sizes'] = $sanitized_sizes;
}

/**
* WordPress cannot read JPEG XL, so the metadata of a JXL attachment
* holds no file. Its attached file is used instead.
*
* @return string|null the file relative to the upload directory
*/
private function get_metadata_file() {
if ( ! is_array( $this->wp_metadata ) ) {
return null;
}
if ( isset( $this->wp_metadata['file'] ) ) {
return $this->wp_metadata['file'];
}
if ( 'image/jxl' === $this->get_mime_type() ) {
$file = get_post_meta( $this->id, '_wp_attached_file', true );
return $file ? $file : null;
}
return null;
}

private function detect_duplicates( $active_sizes, $active_tinify_sizes ) {
$filenames = array();

Expand Down Expand Up @@ -206,12 +231,18 @@ public function get_wp_metadata() {
return $this->wp_metadata;
}

/**
* The attachment mime types the plugin compresses. A function instead of
* a constant, because PHP 5.4 and 5.5 do not allow arrays in constants.
*
* @return string[] mime types
*/
public static function compressible_mime_types() {
return array( 'image/jpeg', 'image/png', 'image/webp', 'image/avif', 'image/jxl' );
}

public function file_type_allowed() {
return in_array(
$this->get_mime_type(),
array( 'image/jpeg', 'image/png', 'image/webp' ),
true
);
return in_array( $this->get_mime_type(), self::compressible_mime_types(), true );
}

public function get_mime_type() {
Expand Down Expand Up @@ -254,7 +285,7 @@ public function compress() {

$active_tinify_sizes = $this->settings->get_active_tinify_sizes();

if ( $this->settings->get_conversion_enabled() ) {
if ( $this->conversion_applies() ) {
$uncompressed_sizes = $this->filter_image_sizes( 'uncompressed', $active_tinify_sizes );
$unconverted_sizes = $this->filter_image_sizes( 'unconverted', $active_tinify_sizes );

Expand Down Expand Up @@ -534,7 +565,7 @@ public function get_statistics( $active_sizes, $active_tinify_sizes ) {
$this->statistics['image_sizes_optimized'] = 0;
$this->statistics['available_unoptimized_sizes'] = 0;

$conversion_enabled = $this->settings->get_conversion_enabled();
$conversion_enabled = $this->conversion_applies();

foreach ( $this->sizes as $size_name => $size ) {
// skip duplicates or inactive sizes
Expand Down Expand Up @@ -575,7 +606,7 @@ public function get_statistics( $active_sizes, $active_tinify_sizes ) {
if ( $is_active_size ) {
if ( $size->has_been_converted() ) {
++$this->statistics['image_sizes_converted'];
} else {
} elseif ( ! $this->is_avif() ) {
++$this->statistics['available_unconverted_sizes'];
}

Expand Down Expand Up @@ -616,7 +647,22 @@ public static function is_retina( $size ) {
}

public function can_be_converted() {
return $this->settings->get_conversion_enabled() && $this->file_type_allowed();
return $this->conversion_applies() && $this->file_type_allowed();
}

/**
* A WebP or AVIF copy of an AVIF is not worth a credit, so an AVIF is
* only compressed. The extension is checked instead of the mime type,
* so that bulk statistics need no query per image.
*
* @return bool true if the image is an AVIF
*/
private function is_avif() {
return 'avif' === strtolower( pathinfo( (string) $this->name, PATHINFO_EXTENSION ) );
}

private function conversion_applies() {
return $this->settings->get_conversion_enabled() && ! $this->is_avif();
}

/**
Expand All @@ -627,14 +673,13 @@ public function can_be_converted() {
*
* @since 3.6.4
*
* @return array{string} mimetypes to which the image should be converted to
* @return string[] mimetypes to which the image should be converted to, empty when conversion does not apply
*/
private function convert_to() {
$convert_settings = $this->settings->get_conversion_options();
if ( ! $convert_settings['convert'] ) {
// conversion is off so return no mimetypes to convert to
if ( ! $this->conversion_applies() ) {
return array();
}
$convert_settings = $this->settings->get_conversion_options();

foreach ( $this->sizes as $size ) {
if ( ! $size->converted() ) {
Expand Down Expand Up @@ -662,11 +707,11 @@ private function convert_to() {
public function mark_as_compressed() {
$this->parse_tiny_metadata();

$conversion_enabled = $this->settings->get_conversion_enabled();
$conversion_enabled = $this->conversion_applies();

$active_tinify_sizes = $this->settings->get_active_tinify_sizes();

if ( $this->settings->get_conversion_enabled() ) {
if ( $conversion_enabled ) {
$uncompressed_sizes = $this->filter_image_sizes( 'uncompressed', $active_tinify_sizes );
$unconverted_sizes = $this->filter_image_sizes( 'unconverted', $active_tinify_sizes );

Expand Down
2 changes: 1 addition & 1 deletion src/class-tiny-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Tiny_Logger {
const LOG_LEVEL_ERROR = 'error';
const LOG_LEVEL_DEBUG = 'debug';

const MAX_LOG_SIZE = 2 * 1024 * 1024; // 2MB
const MAX_LOG_SIZE = 2097152; // 2MB

private static $instance = null;

Expand Down
17 changes: 15 additions & 2 deletions src/class-tiny-migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Tiny_Migrate {
* @since 3.7.0
* @var int
*/
const DB_VERSION = 1;
const DB_VERSION = 2;

/**
* WordPress option key used to track the applied database version.
Expand Down Expand Up @@ -68,7 +68,8 @@ class Tiny_Migrate {
*/
private static function migrations() {
return array(
1 => array( self::class, 'migrate_meta_key_to_private' ),
1 => array( __CLASS__, 'migrate_meta_key_to_private' ),
2 => array( __CLASS__, 'refresh_htaccess_rules' ),
);
}

Expand Down Expand Up @@ -144,4 +145,16 @@ private static function migrate_meta_key_to_private() {

return true;
}

/**
* Rewrites the installed .htaccess rules, so they also serve the
* converted copies of JPEG XL images.
*
* @return bool Always true, retrying does not help when the upload
* directory is not writable.
*/
private static function refresh_htaccess_rules() {
Tiny_Apache_Rewrite::refresh_rules();
return true;
}
}
Loading
Loading