图片裁剪/等比缩放,支持本地/同域视频文件截图功能 (html5 + canvas)。
width和height。width或者height.currentTime的帧,参数见VideoHandlerOptions.| English | 日本語 |
https://capricorncd.github.io/image-process-tools/demo
import { handleMediaFile } from 'image-process'
const options = {
mimeType: 'image/jpeg',
width: 600,
height: 400,
quality: 0.8
}
// 图片裁剪或视频截图处理
handleMediaFile(file, options)
.then(res => {
console.log(res)
})
.catch (err => {
console.error(err)
})
在HTML文件中使用
<script src="./dist/image-process.umd.js"></script>
<script>
imageProcess.handleMediaFile(file, options)
.then(res => console.log(res))
.catch (err => console.error(err))
</script>
# npm
npm install image-process
# npm i image-process
# yarn
yarn add image-process
# pnpm
pnpm install image-process
# pnpm i image-process
图片文件压缩或裁剪函数。
| 参数 | 类型 | 必须 | 描述 |
|---|---|---|---|
| file | File/Blob/string |
yes | 为string时,必须是base64数据字符串。 |
| options | ImageHandlerOptions |
no | 详情请见ImageHandlerOptions. |
Promise<ImageHandlerResult> 详情请见ImageHandlerResult.图片处理或视频截图处理函数。
| 参数 | 类型 | 必须 | 描述 |
|---|---|---|---|
| file | File |
yes | 图片或视频文件。 |
| options | MediaFileHandlerOptions |
no | 详情请见MediaFileHandlerOptions. |
Promise<MediaFileHandlerResult> 详情请见MediaFileHandlerResult.视频文件截图处理函数。
| 参数 | 类型 | 必须 | 描述 |
|---|---|---|---|
| file | File |
yes | 视频文件。 |
| options | VideoHandlerOptions |
no | 详情请见VideoHandlerOptions. |
Promise<VideoHandlerResult> 详情请见VideoHandlerResult.handleImageFile的参数。
| 属性 | 类型 | 必须 | 描述 |
|---|---|---|---|
| enableDevicePixelRatio | boolean |
no | 是否启用设备的ratio。设备ratio为2时,将会返回2倍宽高的图片。默认为false。 |
| mimeType | string |
no | 文件的mime类型,默认为image/jpeg。https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types |
| isForce | boolean |
no | 当原始图片宽高小于设置宽高时,是否强制缩放图片。默认为false。 |
| perResize | number |
no | 原始图片与目标图片尺寸相差很大时,一次缩放可能会出现锯齿,所以可采用多次缩放处理,防止锯齿出现。该参数为每次缩放的像素值。默认为500。 |
| quality | number |
no | 处理后返回的图片质量,取值0-1。详情请见toDataURL。默认为0.9。 |
| width | number |
no | 处理后的图片宽度。默认为0。 |
| height | number |
no | 处理后的图片高度。默认为0。 |
| longEdge | number |
no | 处理后的图片较长边的像素值,仅在width和height都为0时有效。默认为0。 |
| cropInfo | OptionsCropInfo |
no | 详情请见OptionsCropInfo。 |
handleImageFile的返回值。
| 属性 | 类型 | 必须 | 描述 |
|---|---|---|---|
| blob | Blob |
yes | 图片的Blob数据。 |
| data | string |
yes | 图片的base64数据。 |
| width | number |
yes | 图片宽度。 |
| height | number |
yes | 图片高度。 |
| type | string |
yes | 图片mime类型。 |
| size | SizeInfo |
yes | 图片文件大小信息,详情请见SizeInfo。 |
| url | string |
yes | 图片的Blob本地URL地址。 |
| element | HTMLImageElement/HTMLCanvasElement |
yes | HTMLImageElement或HTMLCanvasElement. |
| raw | MediaFileHandlerRawData |
yes | 处理之前的图片的数据信息。详情请见MediaFileHandlerRawData。 |
handleMediaFile的参数。 详情请见VideoHandlerOptions。
type MediaFileHandlerOptions = VideoHandlerOptions
处理之前的图片的数据信息。
| 属性 | 类型 | 必须 | 描述 |
|---|---|---|---|
| blob | Blob |
yes | 图片的Blob数据。 |
| data | string |
yes | 图片的base64数据。 |
| width | number |
yes | 图片宽度。 |
| height | number |
yes | 图片高度。 |
| type | string |
yes | 图片mime类型。 |
| size | SizeInfo |
yes | 图片文件大小信息,详情请见SizeInfo。 |
| url | string |
yes | 图片的Blob本地URL地址。 |
| element | HTMLImageElement |
yes | HTMLImageElement |
handleMediaFile的返回值。
| 属性 | 类型 | 必须 | 描述 |
|---|---|---|---|
| blob | Blob |
yes | 图片的Blob数据。 |
| data | string |
yes | 图片的base64数据。 |
| width | number |
yes | 图片宽度。 |
| height | number |
yes | 图片高度。 |
| type | string |
yes | 图片mime类型。 |
| size | SizeInfo |
yes | 图片文件大小信息,详情请见SizeInfo。 |
| url | string |
yes | 图片的Blob本地URL地址。 |
| element | HTMLImageElement/HTMLCanvasElement |
yes | HTMLImageElement或HTMLCanvasElement. |
| raw | MediaFileHandlerRawData |
yes | 处理之前的图片的数据信息。详情请见MediaFileHandlerRawData。 |
| videoInfo | VideoInfo |
no | 原始视频文件信息。详情请见VideoInfo。 |
| 属性 | 类型 | 必须 | 描述 |
|---|---|---|---|
| blob | Blob |
yes | 图片的Blob数据。 |
| data | string |
yes | 图片的base64数据。 |
| width | number |
yes | 图片宽度。 |
| height | number |
yes | 图片高度。 |
| type | string |
yes | 图片mime类型。 |
| size | SizeInfo |
yes | 图片文件大小信息,详情请见SizeInfo。 |
| url | string |
yes | 图片的Blob本地URL地址。 |
https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage
裁剪参数非有效值时将被忽略。

| 属性 | 类型 | 必须 | 描述 |
|---|---|---|---|
| sx | number |
yes | x坐标。 |
| sy | number |
yes | y坐标。 |
| sw | number |
yes | 从sx坐标开始沿x轴方向的长度。 |
| sh | number |
yes | 从sy坐标开始沿y轴方向的长度。 |
文件大小信息。
| 属性 | 类型 | 必须 | 描述 |
|---|---|---|---|
| text | string |
yes | 字符串形式的文件大小,比如1.23MiB。 |
| unit | string |
yes | 文件大小单位,比如MiB。 |
| value | number |
yes | 数值形式的文件大小,比如1.23。 |
| bytes | number |
yes | 文件大小字节。 |
handleVideoFile函数的参数。
| 属性 | 类型 | 必须 | 描述 |
|---|---|---|---|
| enableDevicePixelRatio | boolean |
no | 是否启用设备的ratio。设备ratio为2时,将会返回2倍宽高的图片。默认为false。 |
| mimeType | string |
no | 文件的mime类型,默认为image/jpeg。https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types |
| isForce | boolean |
no | 当原始图片宽高小于设置宽高时,是否强制缩放图片。默认为false。 |
| perResize | number |
no | 原始图片与目标图片尺寸相差很大时,一次缩放可能会出现锯齿,所以可采用多次缩放处理,防止锯齿出现。该参数为每次缩放的像素值。默认为500。 |
| quality | number |
no | 处理后返回的图片质量,取值0-1。详情请见toDataURL。默认为0.9。 |
| width | number |
no | 处理后的图片宽度。默认为0。 |
| height | number |
no | 处理后的图片高度。默认为0。 |
| longEdge | number |
no | 处理后的图片较长边的像素值,仅在width和height都为0时有效。默认为0。 |
| cropInfo | OptionsCropInfo |
no | 详情请见OptionsCropInfo。 |
| currentTime | number |
no | 视频文件的截图时间点。超出视频播放时间时,将截取最后一帧。默认随机截取某一帧。 |
handleVideoFile的返回值。
| 属性 | 类型 | 必须 | 描述 |
|---|---|---|---|
| blob | Blob |
yes | 图片的Blob数据。 |
| data | string |
yes | 图片的base64数据。 |
| width | number |
yes | 图片宽度。 |
| height | number |
yes | 图片高度。 |
| type | string |
yes | 图片mime类型。 |
| size | SizeInfo |
yes | 图片文件大小信息,详情请见SizeInfo。 |
| url | string |
yes | 图片的Blob本地URL地址。 |
| element | HTMLImageElement/HTMLCanvasElement |
yes | HTMLImageElement或HTMLCanvasElement. |
| raw | MediaFileHandlerRawData |
yes | 处理之前的图片的数据信息。详情请见MediaFileHandlerRawData。 |
| videoInfo | VideoInfo |
yes | When taking a screenshot of the video, the original video file information. See VideoInfo. |
视频文件信息。
| 属性 | 类型 | 必须 | 描述 |
|---|---|---|---|
| url | string |
yes | 视频文件的blob本地URL。 |
| videoFile | File |
yes | 视频文件File对象。 |
| videoWidth | number |
yes | 视频宽度。 |
| videoHeight | number |
yes | 视频高度。 |
| duration | number |
yes | 视频时长。 |
| currentTime | number |
yes | 截取图片的时间点位置。 |
以下方法的文件请见 https://github.com/capricorncd/zx-sml
import {
fileToBase64,
createElement,
formatBytes,
splitBase64,
createBlobURL,
base64ToBlob,
} from 'image-process'
Code and documentation copyright 2018-Present. Capricorncd. Code released under the MIT License.