IMAGE_COMPRESS

Updated at:

Compresses images to a target file size while maintaining minimum quality.

Requirement

IMAGE_COMPRESS is supported in Ververica (VVR) 11.6 and later.

Syntax

ROW<compressed_image_content VARBINARY, result_size INT, result_quality INT, result_long_edge INT> IMAGE_COMPRESS(image_content VARBINARY, max_size INT, max_edge INT, min_quality INT)

Parameters

Required

Argument

Type

Description

image_content

VARBINARY

Image content to compress.

Optional

Argument

Type

Default

Description

max_size

INT

7,864,320 (7.5 MB)

Maximum target file size, in bytes.

max_edge

INT

1920

Maximum length of the longest edge, in pixels.

min_quality

INT

40

Minimum quality threshold. Valid values: 0–100.

How it works

Compresses images in up to three stages, stopping when the file size meets max_size.

Stage 1: Initial resolution scaling

Scales the image so its longest edge does not exceed max_edge. Returns the image if the resulting size meets max_size.

Stage 2: Quality reduction

Holds resolution constant while gradually reducing quality. Returns the image if the size meets max_size before quality drops to min_quality.

Stage 3: Deep resolution scaling

Maintains min_quality while progressively scaling down resolution. Returns the image when the size meets max_size. Fails if the size still exceeds max_size when the shortest edge reaches 10 pixels.

Return values

Returns a ROW with the following fields:

Field

Type

Description

compressed_image_content

VARBINARY

Compressed image content.

result_size

INT

Size of the compressed image, in bytes.

result_quality

INT

Quality of the compressed image.

result_long_edge

INT

Length of the longest edge, in pixels.

Examples

Test data

Image_url (STRING)

https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg

Test statement

SELECT
  compressed_image_row.compressed_image_content AS compressed_image_content,
  compressed_image_row.result_size AS compress_result_size,
  compressed_image_row.result_quality AS compress_result_quality,
  compressed_image_row.result_long_edge AS compress_result_long_edge
FROM (
  SELECT IMAGE_COMPRESS(FETCH_CONTENT(image_url)) AS compressed_image_row FROM T1
);

Test result

Field

Type

Output

compressed_image_content

VARBINARY

Omitted.

compress_result_size

INT

496395

compress_result_quality

INT

100

compress_result_long_edge

INT

1920