Skip to content

k.utils.image

Image format, size and watermark

Overview

k.utils.image handles image bytes or site FileInfo in a script.

changeFormat()

Convert the image bytes to the format corresponding to the new extension.

ParameterTypeRequiredDescription
binarynumber[]YesSource image bytes, usually from k.file.readBinary() or an uploaded file
newExtensionstringYesTarget extension, such as .webp, .png, or .jpg

Returns: number[], the converted image bytes.

ts
const jpg = k.file.readBinary("media/source.jpg")
const webp = k.utils.image.changeFormat(jpg, ".webp")

k.file.writeBinary("media/source.webp", webp)
return k.file.get("media/source.webp")

resize()

OverloadDescription
resize(file, height, width, saveAs?)Adjust site files
resize(bytes, height, width)Adjust byte array
ParameterTypeRequiredDescription
fileFileInfoYesSite file object, usually from k.file.get()
bytesnumber[]YesImage byte array
heightnumberYesTarget height. In the byte-array overload, pass 0 to calculate it from width
widthnumberYesTarget width. In the byte-array overload, pass 0 to calculate it from height
saveAsstringNoSite file path to save as; omitted means overwrite the source file

Returns:

OverloadReturn
resize(file, height, width, saveAs?)FileInfo, the resized site file
resize(bytes, height, width)number[], the resized image bytes

For a site file, load FileInfo with k.file.get() first:

ts
const original = k.file.get("media/product.jpg")
const resized = k.utils.image.resize(original, 600, 800, "media/product-800.jpg")

return {
    url: resized.url,
    size: resized.size
}

For uploads or bytes returned by another API, use the byte-array overload:

ts
const file = k.request.files[0]
const thumbnail = k.utils.image.resize(file.bytes, 240, 240)
k.file.writeBinary(`media/thumbs/${file.fileName}`, thumbnail)

changeDpi()

Modify the DPI and save as site file.

ParameterTypeRequiredDescription
fileUploadFile | FileInfoYesUploaded file or site file object
targetDpinumberYesTarget DPI; values below 72 are treated as 72
saveAsstringRequired for UploadFile, optional for FileInfoSave path. The FileInfo overload overwrites the source file when omitted

Returns: FileInfo, the saved site file.

ts
const source = k.file.get("media/print-cover.jpg")
const output = k.utils.image.changeDpi(source, 300, "media/print-cover-300dpi.jpg")

return {
    path: output.fullName,
    size: output.size
}

addWatermark()

Add watermark to image bytes; WatermarkOptions includes align, justify, opacity, offsetX/Y, repeat.

ParameterTypeRequiredDescription
binarynumber[]YesBase image bytes
watermarknumber[]YesWatermark image bytes
optionWatermarkOptionsYesWatermark position, opacity, and repeat behavior

Returns: number[], the watermarked image bytes.

WatermarkOptions

PropertyTypeDescription
alignstringVertical position: top, center, bottom
justifystringHorizontal position: left, center, right
opacitynumberOpacity, usually from 0 to 1
offsetXnumberHorizontal offset ratio, for example 0.04 means 4%
offsetYnumberVertical offset ratio
repeatbooleanWhether to tile the watermark
ts
const image = k.file.readBinary("media/product.jpg")
const watermark = k.file.readBinary("media/watermark.png")

const output = k.utils.image.addWatermark(image, watermark, {
    align: "bottom",
    justify: "right",
    offsetX: 0.04,
    offsetY: 0.04,
    opacity: 0.8,
    repeat: false
})

k.file.writeBinary("media/product-watermarked.jpg", output)

getSize() / getFrameCount() / getGifFrameCount() / convertToTwoFramesGif()

Read dimensions, frame number or convert GIF.

MethodParameterReturnDescription
getSize(image)number[]{ width, height }Read image dimensions
getFrameCount(image)number[]numberRead GIF / WebP frame count
getGifFrameCount(image)number[]numberOlder frame-count method; prefer getFrameCount()
convertToTwoFramesGif(image)number[]number[]Convert to two-frame GIF bytes
ts
const image = k.file.readBinary("media/banner.jpg")
const size = k.utils.image.getSize(image)

return {
    width: size.width,
    height: size.height
}

getFrameCount() reads GIF / WebP frame counts; convertToTwoFramesGif() can reduce an animation to a two-frame GIF.

ts
const animation = k.file.readBinary("media/loading.webp")
const frameCount = k.utils.image.getFrameCount(animation)

if (frameCount > 2) {
    const gif = k.utils.image.convertToTwoFramesGif(animation)
    k.file.writeBinary("media/loading-preview.gif", gif)
}