For AI agents: the complete documentation index is available at /apps-plugin/docs/llms.txt, the full documentation bundle is available at /apps-plugin/docs/llms-full.txt, and this page is available as Markdown at /apps-plugin/docs/js-api/media/compressImage.md.

compressImage

压缩图片。

扫码预览

扫码预览 compressImage

支持版本

前端库版本不低于 0.0.26。

支持平台

平台支持情况
Android支持
iOS支持
PC不支持
HarmonyOS不支持

支持场景

场景支持情况
页面支持
卡片不支持

接入准备

前置条件

  • src 指向豆包应用插件可访问、存在且可解码的本地图片

调用方式

异步 API

compressImage(params: CompressImageParams): Promise<CompressImageResult>

入参

名称类型必填默认值约束说明
compressedHeightnumber否-正数,且与 compressedWidth 同时传入输出图片高度,单位 px。仅在 `compressedWidth` 同时为正数时调整尺寸。
compressedWidthnumber否-正数,且与 compressedHeight 同时传入输出图片宽度,单位 px。仅在 `compressedHeight` 同时为正数时调整尺寸。
qualitynumber否800 至 100JPEG 输出质量。
srcstring是--图片路径。

调用示例

import { compressImage } from '@doubao-dev/framework/api';

const { tempFilePath } = await compressImage({
  src: '/tmp/image.png',
  quality: 80
});

console.log(tempFilePath);

成功返回

名称类型必返说明
tempFilePathstring是压缩后 JPEG 图片的临时文件路径。

返回示例

{
  "tempFilePath": "appletfile://temp/compressed-example.jpg"
}

错误处理

错误对象和通用错误码见 通用错误处理。

失败示例

{
  "errNo": 104,
  "errMsg": "src cannot be empty"
}

错误码

errNoerrMsg平台触发条件处理建议
104src cannot be emptyAndroidsrc 为空字符串传入非空的本地图片路径。
104src must not be emptyiOSsrc 为空字符串传入非空的本地图片路径。
203source file does not existAndroid本地源图片文件不存在检查路径仍在有效期内并重新获取文件。