Skip to content
StrifflyPublic

About

πŸ™ Takopic – Local Image Optimizer | Convert WebP & AVIF. Squeezes images. Spills no ink, sends no bill. JPG, PNG, WebP and AVIF optimised on your own server, free and unlimited.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Β 
Β 

Latest commit

Β 

History

369 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Takopic: squeezes images, spills no ink, sends no bill.

Takopic – Local Image Optimizer | Convert WebP & AVIF

πŸ™ Squeezes images. Spills no ink, sends no bill.

GNU General Public License 3.0 WordPress 6.2 or later PHP 8.2 or later

Features Β· Screenshots Β· Requirements Β· Installation Β· Privacy Β· FAQ Β· Developers Β· Changelog Β· Contributing Β· Support

Takopic optimises the images of your WordPress media library with the libraries of your own server: jpegoptim, optipng or oxipng, pngquant, cwebp and avifenc, or GD and Imagick for the WebP and AVIF formats. Free and unlimited, no API key, no quota: your images never leave your server.

✨ Features

Optimisation

  • Compresses JPG and PNG images in place, and creates their WebP and AVIF versions, kept only when lighter.
  • Keeps the original images, to restore them at any time.
  • Strips the metadata (camera, date, GPS position), but keeps the colour profile when the colours need it.
  • Handles CMYK JPGs and the photos stored sideways.

Media library

  • Optimises the images on upload, or the whole library in the background, with pause and cancel.
  • Re-optimises the images left behind by new settings or new image sizes of the theme.
  • Shows the status, the errors and the bytes saved of each image, and optimises or restores it in one click.

On the site

  • Serves AVIF and WebP with the JPG or PNG as fallback: in a <picture> element, or from the server with rewrite rules (Apache, nginx).
  • Tests the delivery.

For developers

  • takopic_image() and takopic_image_sources() give the <picture> of an image or its srcsets, in your own templates.
  • WP-CLI: wp takopic run, clean, restore, stats and health.
  • Filters for the folder of the originals, the priority and threads of the libraries, the pace of the background jobs and the path of a library.

All of them are documented in Developers.

πŸ“Έ Screenshots

The overview: the bytes saved, the status of the images and the libraries of each format.

The optimisation, the delivery, the settings and the media library

The optimisation of the media library in the background, with its progress.

The delivery of WebP and AVIF on the site, and its test.

The settings of each format: quality, compression, library.

The status and the bytes saved of each image, in the media library.

πŸ“‹ Requirements

WordPress 6.2 and PHP 8.2 or later, and PHP allowed to run the libraries of the server (proc_open() or exec()):

Format Library Without it
JPG jpegoptim Not compressed
PNG optipng or oxipng (faster) Not compressed
PNG lossy (optional) pngquant Lossless only
WebP cwebp GD or Imagick, with heavier PNG graphics
AVIF avifenc GD or Imagick (WordPress 6.5+), with less sharp colours
  • On Debian or Ubuntu: sudo apt install jpegoptim optipng webp libavif-bin.
  • On Windows: no JPG and PNG compression. For WebP and AVIF, put the official builds of cwebp and avifenc on the PATH, or set their path with the takopic_binary_path filter.

πŸ“¦ Installation

In Plugins > Add New Plugin, search for Takopic, then install and activate it. It is on WordPress.org.

After installing:

  • The new uploads are optimised right away: Optimise new uploads, ticked by default.
  • The images already in the library wait in the Optimise tab: Optimise new images optimises them in the background.
  • The Overview tab lists the missing libraries, and how to install them.
  • WebP and AVIF are not served until you turn them on in the Delivery tab.

Build from source

With Composer:

git clone https://github.com/Striffly/takopic.git
cd takopic
composer install # The dependencies, in vendor-prefixed/.

πŸ”’ Privacy

  • No external request: no API, no tracking. The delivery test only requests the images of the site, from its CDN if they are served by one.

  • No metadata: the optimised images lose it, GPS position included. The originals keep it.

  • Private originals: they are kept in uploads/takopic/originals-{key}/, the key drawn at random for each site, so their URL cannot be guessed. A .htaccess denies the web access to that folder on Apache and LiteSpeed. On nginx, add this rule to the server block of the site (also given on the Delivery tab):

    location ^~ /wp-content/uploads/takopic/originals- {
    	deny all;
    }
  • Originals out of the web root: set their folder with the takopic_backup_dir filter, before optimising any image. Takopic does not move the originals already kept: move the folder yourself when you change it.

    // e.g. next to the web root of a Bedrock site.
    add_filter('takopic_backup_dir', function () {
    	return dirname(ABSPATH, 2) . '/takopic-originals';
    });

❓ FAQ

Does Takopic replace my images?

By default, yes: the JPG and PNG files are replaced by their compressed version, and the originals are kept in uploads/takopic/originals-{key}/. Restore original images on the settings page, or Restore original on an image of the media library, puts them back. To only make the WebP and AVIF versions, untick Compress JPG images and Compress PNG images on the Settings tab.

Does it serve WebP and AVIF automatically?

Once turned on in the Delivery tab, in one of two ways:

  • In the HTML: the images of the content and of wp_get_attachment_image() (featured images, galleries, most themes) are wrapped in a <picture> element, with the JPG or PNG as fallback. Works everywhere, but may break a layout styling img directly. In a custom theme, use takopic_image().
  • From the server: the server answers photo.jpg with its AVIF or WebP version when the browser reads it. The HTML does not change, and CSS backgrounds are served too. Takopic writes the rules in uploads/.htaccess on Apache and LiteSpeed; on nginx, add these ones:
# In the http block:
map $http_accept $takopic_avif {
	default "";
	"~image/avif" ".avif";
}
map $http_accept $takopic_webp {
	default "";
	"~image/webp" ".webp";
}

# In the server block:
location ~* ^/wp-content/uploads/(?<takopic_file>.+\.(?:jpe?g|png))$ {
	add_header Vary Accept;
	try_files /wp-content/uploads/takopic/formats/$takopic_file$takopic_avif /wp-content/uploads/takopic/formats/$takopic_file$takopic_webp $uri =404;
}

A CDN must vary its cache on the Accept header, or serve the JPG to everyone. Test the delivery checks what the server sends.

What happens when the theme changes its image sizes?

Its images are listed as "Older settings": Re-optimise outdated images makes their thumbnails again from the originals, and optimises them.

Does it slow the site down?

The background jobs run the libraries at the lowest priority, behind the requests of the site. On a small server, the filters slow them down further.

Does it work on shared hosting?

Yes for WebP and AVIF, made by GD or Imagick when the libraries are missing. The JPG and PNG compression needs the libraries, which most shared hosts do not let you install: ask your host. The Overview tab tells what is missing.

What happens when I deactivate or delete Takopic?

Deactivated, Takopic stops its background job and removes its rewrite rules: the site serves the JPG and PNG images again. Its settings and files stay, and the rules come back on activation.

Deleted, Takopic removes its settings and data from the database, but leaves the files: the compressed images, their WebP and AVIF versions and the originals. To get the originals back, run Restore original images before deleting it.

Before deactivating, a dialog shows how many images Takopic has optimised and the size of uploads/takopic/, with a button to restore the originals, and warns when the theme calls takopic_image(): its pages break while Takopic is deactivated. With nothing to warn about, Takopic deactivates at once.

Which settings do the libraries use?

Every optimisation starts from the original image:

# JPG
jpegoptim --strip-all --all-progressive --overwrite --max={quality} photo.jpg

# PNG: optipng, or oxipng when installed
optipng -clobber -strip all -o {level} \
	-out photo.png takopic/originals-{key}/photo.png
oxipng -o {level} --strip safe --force --threads {threads} \
	--out photo.png takopic/originals-{key}/photo.png

# PNG, lossy compression: pngquant, then optipng or oxipng on tmp.png
pngquant --quality={quality - 15}-{quality} --strip --skip-if-larger --force \
	--output tmp.png -- takopic/originals-{key}/photo.png

# WebP
cwebp -q {quality} photo.jpg -o takopic/formats/photo.jpg.webp

# AVIF
avifenc -q {quality} --ignore-exif --ignore-xmp --speed {speed} --jobs {threads} \
	photo.jpg takopic/formats/photo.jpg.avif
  • avifenc older than 1.0 gets the quality as cq-level. Its speed is 6 by default.
  • The libraries run on several files of an image at once, within takopic_threads (4 on upload, 2 in background jobs): avifenc and oxipng get the threads left, {threads}.
  • A PNG is also tried with cwebp -near_lossless 60, kept if lighter: graphics and screenshots get much lighter and sharper, photos stay lossy.
  • Without cwebp or avifenc, GD or Imagick make the WebP at the same quality, and the AVIF at the quality + 5 (up to 89, from 90 as is), close to avifenc. The PNG images are not tried in near-lossless then.

πŸ”§ Developers

Theme development

The <picture> option only covers the content and wp_get_attachment_image(). For the images of your own markup, e.g. from an ACF field, use these functions.

Important

Keep the function_exists() check: without it, the pages break while Takopic is deactivated.

takopic_image() returns the <picture> of an image, whatever the options:

if (function_exists('takopic_image')) {
	echo takopic_image($image_id, 'hero');       // Class of the <picture> element.
	echo takopic_image($image_id, 'hero', true); // data-src and data-srcset, for a JavaScript lazy loader.
}
<picture class="takopic-picture hero">
	<source type="image/avif" srcset="takopic/formats/photo.jpg.avif 2500w, …" sizes="…">
	<source type="image/webp" srcset="takopic/formats/photo.jpg.webp 2500w, …" sizes="…">
	<img data-takopic src="photo.jpg" srcset="photo.jpg 2500w, …" sizes="…" width="2500" height="1667" alt="">
</picture>

takopic_image_sources() returns the sources only, for a theme writing its own <picture>: from a srcset or an image URL, the srcset of each format, keyed by MIME type, best first. A size without a file in a format keeps its JPG or PNG.

$sources = function_exists('takopic_image_sources') ? takopic_image_sources($srcset) : [];
// ['image/avif' => 'takopic/formats/photo.jpg.avif 2500w, photo-300x200.jpg 300w',
//  'image/webp' => 'takopic/formats/photo.jpg.webp 2500w, takopic/formats/photo-300x200.jpg.webp 300w']

foreach ($sources as $type => $format_srcset) {
	printf('<source type="%s" srcset="%s" sizes="%s">', esc_attr($type), esc_attr($format_srcset), esc_attr($sizes));
}

WP-CLI

Command What it does
wp takopic health Checks that the libraries are installed.
wp takopic run Optimises the new images, with the settings of the settings page.
wp takopic clean Removes the WebP and AVIF files. The JPG and PNG files stay compressed.
wp takopic restore Restores the original images and removes every file made by Takopic.
wp takopic stats Prints the bytes saved on the media library. --rebuild measures every image again.
wp takopic version Shows the version of Takopic.

wp takopic run takes these options, all listed by wp help takopic run:

wp takopic run                   # Optimises the new images.
wp takopic run --force           # Optimises every image again.
wp takopic run --regenerate      # Makes the thumbnails again from the originals first, implies --force.
wp takopic run --thumbnails-only # Leaves the full size images out.

# Turns a format off or on: --[no-]jpg, --[no-]png, --[no-]webp, --[no-]avif.
wp takopic run --no-avif

# Overrides a setting of the settings page: --jpg-quality, --png-compression, --png-level,
# --png-lossy-quality, --webp-quality, --avif-quality, --avif-speed.
wp takopic run --webp --no-avif --no-jpg --no-png --webp-quality=75 --force

# Exits with an error when images fail, e.g. in a deployment script.
wp takopic run || exit 1

wp takopic stats first measures the images optimised before Takopic counted the bytes saved, from the sizes of their files: nothing is encoded again.

Filters

takopic_backup_dir

The folder of the original images. Set it before optimising any image: Takopic does not move the originals already kept. See Privacy to keep them out of the web root.

apply_filters('takopic_backup_dir', string $dir)
  • $dir: the absolute path of uploads/takopic/originals-{key}.

takopic_nice

The priority of the libraries, from 0 (normal) to 19 (lowest). Windows has none.

apply_filters('takopic_nice', int $level, string $context)
  • $level: 19 in background jobs, 0 otherwise.
  • $context: 'upload', 'background' or 'cli'.

takopic_threads

The threads the libraries run on at once, all together: cwebp, jpegoptim, optipng and pngquant take one each, avifenc and oxipng the threads left.

apply_filters('takopic_threads', int|string $threads, string $context)
  • $threads: 4 on upload, 2 in background jobs, 'all' (every core) in WP-CLI.
  • $context: 'upload', 'background' or 'cli'.
// In a mu-plugin: lighter background jobs on a small server.
add_filter('takopic_threads', function ($threads, $context) {
	return $context === 'background' ? 1 : $threads;
}, 10, 2);

takopic_timeout

The seconds a library may take on a file, then it is stopped.

apply_filters('takopic_timeout', int $seconds)
  • $seconds: 120.

takopic_binary_path

The path of a library, or false to turn it off. Without cwebp or avifenc, GD or Imagick make the format.

apply_filters('takopic_binary_path', string|null $path, string $name)
  • $path: the one found in the folders of the PATH, then in /usr/bin, /usr/local/bin, /bin, /opt/homebrew/bin and /opt/local/bin (PHP-FPM often passes an empty PATH), null if none.
  • $name: 'jpegoptim', 'optipng', 'oxipng', 'pngquant', 'cwebp', 'avifenc' or 'nice'.
// A library installed out of the usual folders.
add_filter('takopic_binary_path', function ($path, $name) {
	return $name === 'avifenc' ? '/home/me/bin/avifenc' : $path;
}, 10, 2);

takopic_job_default_time_limit

The seconds a background job runs in one request, before the next request takes over. On multisite, the filter carries the ID of the site: takopic_2_job_default_time_limit on the site 2.

apply_filters('takopic_job_default_time_limit', int $seconds)
  • $seconds: 20.

takopic_job_seconds_between_batches

A pause after each image of a background job, in seconds. On multisite, the filter carries the ID of the site: takopic_2_job_seconds_between_batches on the site 2.

apply_filters('takopic_job_seconds_between_batches', int $seconds)
  • $seconds: 0.

Contributing

To release Takopic, run its tests or draw its logo, see CONTRIBUTING.md.

β˜• Support

Takopic is free, and will stay so. If it saves you time or bytes, you can buy me a coffee β˜•:

QR code of the Bitcoin address

πŸ™ Credits

Takopic is developed by ClΓ©ment Gayot.

It started as a fork of Squidge, created by Ainsley Clark in 2021 with contributions from Roman K, Rohinish and karbivskij, and was largely rewritten in 2026. Many thanks to them for the foundations: the idea of relying on the native libraries of the server comes from Squidge.

Thanks also to Media Optimizer by YooHW for some ideas: the menu, the delivery page, the display of the bytes saved, and more.

πŸ“œ License

Takopic is free software, released under the GNU General Public License v3 or later.

About

πŸ™ Takopic – Local Image Optimizer | Convert WebP & AVIF. Squeezes images. Spills no ink, sends no bill. JPG, PNG, WebP and AVIF optimised on your own server, free and unlimited.

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages