class GenerateEmbedding

Traits

SerializesModels
Queueable

Constants

MAX_TILES_THRESHOLD

Maximum number of Zoomify tiles to load before falling back to full image.

If a bbox requires more tiles than this, loading the full image is faster due to reduced request overhead.

ZOOMIFY_TILE_SIZE

Zoomify tile size in pixels (standard for Zoomify format).

Properties

Image $image

The image to generate an embedding of.

User $user

The user who initiated the job.

array|null $bbox

The bbox of the image to generate an embedding for (x, y, width, height).

protected bool $deleteWhenMissingModels

Ignore this job if the image or user does not exist any more.

$tries

The number of times the job may be attempted.

Methods

__construct(Image $image, User $user, array|null $bbox = null)

Create a new instance.

static string
getPendingJobsCacheKey(User $user)

Get the cache key for tracking pending jobs for a user.

void
handle()

Handle the job.

string
getFilename()

Get the filename for the embedding.

string
generateEmbedding(Image $image)

Generate the embedding.

string
getImageBufferForPyworker(string $path)

Get the byte string of the resized image for the Python worker.

Image
getVipsImageFromFullImage(string $path)

Load the full image, crop to bbox if set, and resize for the Python worker.

string
sendPyworkerRequest(string $buffer)

Send the scaled-down PNG image to the Python worker and return the embedding npy file as binary blob.

int
getMaxZoomLevel()

Calculate the maximum Zoomify zoom level (full resolution) for this image.

array
prepareTileLoadingInfo(array $bbox, int $modelInputSize)

Prepare all information needed for loading Zoomify tiles.

Image
getVipsImageFromZoomifyTiles(array $tileInfo)

Load and stitch Zoomify tiles for the bbox and resize for the Python worker.

void
decrementPendingJobsCounter()

Decrement the pending jobs counter for the user.

Details

at line 78
__construct(Image $image, User $user, array|null $bbox = null)

Create a new instance.

Parameters

Image $image
User $user
array|null $bbox

Optional bbox (x, y, width, height)

at line 88
static string getPendingJobsCacheKey(User $user)

Get the cache key for tracking pending jobs for a user.

Parameters

User $user

Return Value

string

at line 98
void handle()

Handle the job.

Return Value

void

at line 120
string getFilename()

Get the filename for the embedding.

Return Value

string

at line 132
protected string generateEmbedding(Image $image)

Generate the embedding.

Parameters

Image $image

Return Value

string

at line 145
protected string getImageBufferForPyworker(string $path)

Get the byte string of the resized image for the Python worker.

Parameters

string $path

Return Value

string

at line 179
protected Image getVipsImageFromFullImage(string $path)

Load the full image, crop to bbox if set, and resize for the Python worker.

Parameters

string $path

Return Value

Image

at line 200
protected string sendPyworkerRequest(string $buffer)

Send the scaled-down PNG image to the Python worker and return the embedding npy file as binary blob.

Parameters

string $buffer

Return Value

string

at line 223
protected int getMaxZoomLevel()

Calculate the maximum Zoomify zoom level (full resolution) for this image.

The max level is determined by how many zoom levels are needed so that 256 × 2^maxLevel >= max(image_width, image_height).

See prepareTileLoadingInfo() fore more explanation.

Return Value

int

at line 257
protected array prepareTileLoadingInfo(array $bbox, int $modelInputSize)

Prepare all information needed for loading Zoomify tiles.

This calculates:

  1. Best zoom level to use (lowest resolution where bbox >= modelInputSize)
  2. Scale factor for that zoom level
  3. Bbox coordinates at that zoom level
  4. Which tiles (rows/cols) are needed
  5. Total tile count
  6. Number of tiles before this zoom level (for TileGroup index calculation)
  7. Number of tile columns at this zoom level (for TileGroup index calculation)

Zoomify structure:

  • Level 0: Lowest resolution (single 256x256 tile)
  • Level N: Higher resolution (dimension = 256 × 2^N)
  • Max level: Full resolution

Zoom level selection math:

  • At max zoom level (full res): bbox has dimensions from request
  • At lower levels: bbox_dimension_at_level = bbox_dimension × 2^(level - maxLevel)
  • We need: max(bbox_width, bbox_height) × 2^(level - maxLevel) >= modelInputSize
  • Solving: level >= maxLevel + log2(modelInputSize / max(bbox_width, bbox_height))

Parameters

array $bbox
int $modelInputSize

Return Value

array

at line 318
protected Image getVipsImageFromZoomifyTiles(array $tileInfo)

Load and stitch Zoomify tiles for the bbox and resize for the Python worker.

Parameters

array $tileInfo

Tile loading information from prepareTileLoadingInfo()

Return Value

Image

at line 356
protected void decrementPendingJobsCounter()

Decrement the pending jobs counter for the user.

Return Value

void