Gallery3:Modules:image optimizer - Gallery Codex
Personal tools

Gallery3:Modules:image optimizer

From Gallery Codex

Image Optimizer Module

Image Optimizer uses three underlying toolkits, all performing lossless transformations on GIF, JPG, and PNG images. These are used to alter:

  • Resize and thumb image optimization: reduce the size/nature of thumb and resize images so they load faster
  • Full-size image rotation: ensure that rotations of JPG images are made losslessly (this is not the case with ImageMagick, etc.)

This module was inspired by the WordPress module EWWW Image Optimizer and the Gallery3 module Jpegtran.

Graphics toolkits

The three toolkits used by the module are:

The module comes with all three toolkits pre-compiled as libraries. By default it uses them, but can be configured to use copies installed elsewhere on your server instead.

The module's lib directory includes both Windows and Linux versions of the toolkits. For security reasons, Windows should not be used on production sites. Both versions are provided here to enable development copies to still run Windows without issue.

Features

The module is fully-configurable, and can be set to perform any combination of the following:

Full-size image manipulation

  • use Jpegtran for lossless rotations

Thumb and resize image manipulation

  • Compression optimization (lossless). Recommended for thumbs and resize images.
  • Remove all meta data. Recommended for thumb images - 80% size reduction typical, which drastically changes page load speed.
  • Make images progressive/interlaced (lossless). Recommended for resize images - provides preview of photo during download.
  • Image format conversion (not necessarily lossless). Recommended for thumb images - converting PNG/GIF to JPG can reduce the size by a huge amount, helping page load speed.

Installation

This module may be downloaded from the Gallery forums here.

  • Download and extract image_optimizer_1.zip, which contains the complete module with included pre-compiled libraries for Linux.
  • Optionally, also download and extract image_optimizer_1_win.zip, which includes pre-compiled libraries for Windows also.
  • Put the image_optimizer folder into your Gallery modules folder.
  • Log into your Gallery as an administrator and activate the module in the Admin -> Modules menu.
  • Go to the Admin -> Settings -> Image optimizer menu to configure as desired.

By default, the module only affects new images. There are two additional features in the admin settings to help rebuild of all your existing thumb and resize images.

  • Mark all existing images for rebuild (for thumbs and resizes separately). Afterward, go to Maintenace -> Rebuild Images. Depending on your gallery size, this can be very time consuming.
  • Enable update mode. This deactivates all other graphics rules to allow fast optimization on existing images; MUST deactivate this after initial rebuild! No new images should be added to the gallery while this is enabled! This will add site message banners to remind you of its presence.

For uninstallation, proper deactivation ahead of time (Admin -> Modules menu) ensures that there are no permanent effects to your database. To re-encode your existing thumb and resize images without the optimization rules, simply mark them for rebuild before deactivation.

Technical details

Some technical details/discussions:

  • Temp files are created for each step, ensuring that errors don't overwrite existing files.
  • Resize and thumb optimization use the already-shrunk images as sources, not the full-size ones. This means it goes faster.
  • Converting GIF files to PNG or JPG, of course, eliminates any animation they may have.
  • The graphics rules in the database for image optimizer MUST run last. By default, the module sets their priority at 999999999 (standard thumb/resize resizing rules are at 100, and watermark uses 1000). If some other module should set their priority even higher, this may need to be altered.
  • Image format conversion uses the normal Gallery graphics toolkit (GD/ImageMagick/GraphicsMagick), but everything else uses the three toolkits included.
  • Image format conversion changes the format, but leaves the filename extension of the original version (e.g. turning on PNG->JPG conversion means full-size PNG "photo.png" will have a JPG thumb called "photo.png"). This is to work around an existing bug in Gallery, where there is no way to define the resize/thumb image name independently. Furthermore, ".album.jpg" is applied to all album thumbs, regardless of actual type (this also applies to missing thumbs which copy "missing_image.png" as ".album.jpg"). To avoid many rewrites of core functions (and not-easily-reversible database changes), this module simply works/hacks around the bug instead of correcting it.
  • Of course, the downside to adding the optimization step is that the initial generation of thumb and resize images of new images added to the gallery will take longer. The good news is that these three tools are notably faster than their full-featured counterparts.
  • Keeping the optimization as a second step in graphics_rules (as opposed to entirely replacing the thumb/resize creation process) ensures good modularity and flexibility with the rest of Gallery and other Gallery modules.

Prerequisites

  • If you're going to be rebuilding over roughly 15,000 images you may encounter a MySQL limitation. This will manifest in a few ways. Image rebuild will stall at around the 15,000 image mark. Attempting to resume a stalled rebuild job will fail and report no error. Attempting to delete a stalled rebuild job will return an error message to the admin console. This is caused by the MySQL system variable "max_allowed_packet". The default value for this in MySQL is 1,048,576 which causes the 15,000 image limit. To resolve this, modify your MySQL my.cnf file and add a directive to increase the max_allowed_packet variable. Adding this line increases the limit from the default of 1MB to 50MB
max_allowed_packet=52428800


Changelog

  • 2012/12/04, version 1

Initial release