Class: ImageUpscaleWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Worker, Workers::StatusBroadcastable
Defined in:
app/workers/image_upscale_worker.rb

Overview

Background worker for upscaling images using ImageKit or Topaz Labs AI.

Supports two upscaling engines:

  1. ImageKit (default) - Fast, built-in e-upscale transformation
  2. Topaz Labs Gigapixel - Industry-leading quality with specialized models

This worker:

  1. Downloads/processes the upscaled version via selected engine
  2. Validates the result is a valid image
  3. Uploads it as a new image or replaces the original
  4. Updates the Image record with new dimensions/size
  5. Purges the ImageKit cache
  6. Tags the image appropriately

Examples:

Process with ImageKit (default)

ImageUpscaleWorker.perform_async(123, format: 'jpeg', quality: 95, engine: 'imagekit')

Process with Topaz Labs

ImageUpscaleWorker.perform_async(123, format: 'jpeg', engine: 'topaz', topaz_model: 'low_resolution_v2')

Batch enqueue eligible images

ImageUpscaleWorker.perform_async(nil, batch: true, limit: 100)

See Also:

Constant Summary collapse

MIN_VALID_FILE_SIZE =

Minimum file size for a valid image (must be larger than this)

1000
ENGINES =

Supported upscaling engines

%w[imagekit topaz].freeze
ENGINE_TAGS =

Tag applied for each engine

{
  'imagekit' => 'imagekit-upscaled',
  'topaz' => 'topaz-upscaled'
}.freeze

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(image_id = nil, options = {}) ⇒ Object

Parameters:

  • image_id (Integer, nil) (defaults to: nil)

    The Image record ID, or nil for batch mode

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :format (String)

    Output format ('png', 'jpeg', 'webp')

  • :quality (Integer)

    Quality for lossy formats (1-100)

  • :save_as_new (Boolean)

    Create new image copy instead of replacing (default: true)

  • :engine (String)

    Upscaling engine: 'imagekit' (default) or 'topaz'

  • :topaz_model (String)

    Topaz Labs model (only for topaz engine)

  • :batch (Boolean)

    Enable batch mode to find and enqueue eligible images

  • :limit (Integer)

    Max images to enqueue in batch mode

  • :redirect_to (String)

    URL to redirect to after completion



58
59
60
61
62
63
64
65
66
# File 'app/workers/image_upscale_worker.rb', line 58

def perform(image_id = nil, options = {})
  options = options.with_indifferent_access

  if options[:batch] || image_id.nil?
    batch_enqueue(options)
  else
    process_image(image_id, options)
  end
end