resizeImage(params: Object)

Requires:
BizKit≥3.0.0@ray-js/ray≥0.3.23
Platforms:
iOSAndroid
import { resizeImage } from '@ray-js/ray';

// Promise usage
try {
  const res = await resizeImage({ ...params });
  console.log(res);
} catch (err) {
  console.log(err.errorMsg);
}

Description

Compress an image: first crop to the target size while preserving the original aspect ratio, then apply quality compression based on the file size limit

Parameters

PropertyTypeRequiredDescription
aspectFitWidthnumberYesCompression parameters aspectFitWidth Adaptive width
aspectFitHeightnumberYesaspectFitHeight Adaptive height
maxFileSizenumberNomaxFileSize Maximum image file size limit; if empty, no limit. Unit: B
pathstringYespath Image path
complete() => voidNoCompletion callback (executed on both success and failure)
success(params: Object) => voidNoCallback on successful API call
fail(params: Object) => voidNoFailure callback
success callback parameters
PropertyTypeDescriptionBizKit Since
pathstringImage return content path Image pathv3.1.2
fail callback parameters
PropertyTypeDescription
errorMsgstringError message
errorCodestring | numberError code
Error extension

Return Value

None

Examples

import { downloadFile, resizeImage } from '@ray-js/ray'

// Download a real image to a local path first, then resize it.
downloadFile({
  url:
    "https://images.tuyacn.com/rms-static/b7bed040-87b8-11eb-864d-a9f07cbbdf37-1616051299140.png?tyName=logo_cn.png",
  success: res => {
    const path = res.tempFilePath || res.filePath || res.apFilePath;
    console.log("downloaded", path);
    resizeImage({
      aspectFitWidth: 200,
      aspectFitHeight: 200,
      maxFileSize: 102400,
      path: path,
      success: data => {
        console.log(data);
      },
      fail: error => {
        console.error(error);
      },
    });
  },
  fail: error => {
    console.error(error);
  },
});