4.3.3 • Published 5 months ago

image-process v4.3.3

Weekly downloads
7
License
MIT
Repository
github
Last release
5 months ago

image-process

A Image clipping or scaling, support local or same domain video file screenshot. It's implemented in canvas.

  • Image cropping: Just set valid cropping options (See ImageHandlerOptions), or set valid width and height, the image will be centered and cropped.
  • Proportional scaling: Just set the width or height.
  • Video screenshot: Take a picture according to the set currentTime of the VideoHandlerOptions.

简体中文 | 日本語

Demo

https://capricorncd.github.io/image-process-tools/demo

Usage

import { handleMediaFile } from 'image-process'

const options = {
  mimeType: 'image/jpeg',
  width: 600,
  height: 400,
  quality: 0.8
}

// Crop image or video screenshot
handleMediaFile(file, options)
  .then(res => {
    console.log(res)
  })
  .catch (err => {
    console.error(err)
  })

Use in 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>

Installation

# npm
npm install image-process
# npm i image-process

# yarn
yarn add image-process

# pnpm
pnpm install image-process
# pnpm i image-process

Methods

handleImageFile(file, options)

Image file compression or cropping function.

ParamTypesRequiredDescription
fileFile/Blob/stringyesIt's string can only be base64 data.
optionsImageHandlerOptionsnoSee ImageHandlerOptions.

handleMediaFile(file, options)

Image processing or video screenshot processing function.

ParamTypesRequiredDescription
fileFileyesImage or video file.
optionsMediaFileHandlerOptionsnoSee MediaFileHandlerOptions.

handleVideoFile(file, options)

Video file screenshot processing function.

ParamTypesRequiredDescription
fileFileyesVideo file object.
optionsVideoHandlerOptionsnoSee VideoHandlerOptions.

Types

ImageHandlerOptions

An options of the handleImageFile function.

PropTypesRequiredDescription
enableDevicePixelRatiobooleannoWhether to enable the device pixel ratio, when 2 times, the size of the returned image is x2. Default is false.
mimeTypestringnoMultipurpose Internet Mail Extensions. Default is image/jpeg. https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
isForcebooleannoWhen the image width or height is less than the set value, force the target image width or height to be adjusted to the set value. Default is false.
perResizenumbernoReduce the width each time. To prevent jagged edges when scaling an image. Default is 500.
qualitynumbernoA Number between 0 and 1 indicating the image quality to use for image formats that use lossy compression such as image/jpeg and image/webp. If this argument is anything else, the default value for image quality is used. The default value is 0.92. Other arguments are ignored. See toDataURL. Default is 0.9.
widthnumbernoThe width of the processed image. Default is 0.
heightnumbernoThe height of the processed image. Default is 0.
longEdgenumbernoThe value of long edge. Valid when width and height are 0. Default is 0.
cropInfoOptionsCropInfonoSee OptionsCropInfo.
interface ImageHandlerOptions {
  // Whether to enable the device pixel ratio, when 2 times, the size of the returned image is x2. Default is `false`.
  enableDevicePixelRatio?: boolean
  // Multipurpose Internet Mail Extensions. Default is `image/jpeg`.
  // https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
  mimeType?: string
  // When the image width or height is less than the set value,
  // force the target image width or height to be adjusted to the set value.
  // Default is `false`.
  isForce?: boolean
  // Reduce the width each time. To prevent jagged edges when scaling an image.
  // Default is `500`.
  perResize?: number
  // A Number between 0 and 1 indicating the image quality to use for image formats that use lossy compression such as image/jpeg and image/webp.
  // If this argument is anything else, the default value for image quality is used. The default value is 0.92. Other arguments are ignored.
  // See [toDataURL](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL).
  // Default is `0.9`.
  quality?: number
  // The `width` of the processed image. Default is `0`.
  width?: number
  // The `height` of the processed image. Default is `0`.
  height?: number
  // The value of long edge. Valid when width and height are `0`. Default is `0`.
  longEdge?: number
  // See [OptionsCropInfo](#OptionsCropInfo).
  cropInfo?: OptionsCropInfo
}

ImageHandlerResult

Data returned of the handleImageFile function.

PropTypesRequiredDescription
blobBlobyesImage blob data.
datastringyesImage base64 data.
widthnumberyesThe width of the image.
heightnumberyesThe height of the image.
typestringyesThe type of the image.
sizeSizeInfoyesThe size information of the image. See SizeInfo.
urlstringyesA blob url of the image.
elementHTMLImageElement/HTMLCanvasElementyesHTMLImageElement or HTMLCanvasElement.
rawMediaFileHandlerRawDatayesRaw information of the image file being processed. See MediaFileHandlerRawData.
interface ImageHandlerResult extends MediaHandlerResultBase {
  // `HTMLImageElement` or `HTMLCanvasElement`.
  element: HTMLImageElement | HTMLCanvasElement
  // Raw information of the image file being processed. See [MediaFileHandlerRawData](#MediaFileHandlerRawData).
  raw: MediaFileHandlerRawData
}

MediaFileHandlerOptions

An options of the handleMediaFile function. See VideoHandlerOptions.

type MediaFileHandlerOptions = VideoHandlerOptions

MediaFileHandlerRawData

Raw information of the image file being processed.

PropTypesRequiredDescription
blobBlobyesImage blob data.
datastringyesImage base64 data.
widthnumberyesThe width of the image.
heightnumberyesThe height of the image.
typestringyesThe type of the image.
sizeSizeInfoyesThe size information of the image. See SizeInfo.
urlstringyesA blob url of the image.
elementHTMLImageElementyesHTMLImageElement
interface MediaFileHandlerRawData extends MediaHandlerResultBase {
  // `HTMLImageElement`
  element: HTMLImageElement
}

MediaFileHandlerResult

Data returned of the handleMediaFile function.

PropTypesRequiredDescription
blobBlobyesImage blob data.
datastringyesImage base64 data.
widthnumberyesThe width of the image.
heightnumberyesThe height of the image.
typestringyesThe type of the image.
sizeSizeInfoyesThe size information of the image. See SizeInfo.
urlstringyesA blob url of the image.
elementHTMLImageElement/HTMLCanvasElementyesHTMLImageElement or HTMLCanvasElement.
rawMediaFileHandlerRawDatayesRaw information of the image file being processed. See MediaFileHandlerRawData.
videoInfoVideoInfonoVideo file information. See VideoInfo.
interface MediaFileHandlerResult extends ImageHandlerResult {
  // Video file information. See [VideoInfo](#videoinfo).
  videoInfo?: VideoInfo
}

MediaHandlerResultBase

PropTypesRequiredDescription
blobBlobyesImage blob data.
datastringyesImage base64 data.
widthnumberyesThe width of the image.
heightnumberyesThe height of the image.
typestringyesThe type of the image.
sizeSizeInfoyesThe size information of the image. See SizeInfo.
urlstringyesA blob url of the image.
interface MediaHandlerResultBase {
  // Image blob data.
  blob: Blob
  // Image base64 data.
  data: string
  // The width of the image.
  width: number
  // The height of the image.
  height: number
  // The type of the image.
  type: string
  // The size information of the image. See [SizeInfo](#SizeInfo).
  size: SizeInfo
  // A blob url of the image.
  url: string
}

OptionsCropInfo

https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/drawImage

It will be ignored when the value is invalid.

canvas-drawimage

PropTypesRequiredDescription
sxnumberyesThe x-axis coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context. Use the 3- or 5-argument syntax to omit this argument.
synumberyesThe y-axis coordinate of the top left corner of the sub-rectangle of the source image to draw into the destination context. Use the 3- or 5-argument syntax to omit this argument.
swnumberyesThe width of the sub-rectangle of the source image to draw into the destination context. If not specified, the entire rectangle from the coordinates specified by sx and sy to the bottom-right corner of the image is used. Use the 3- or 5-argument syntax to omit this argument.
shnumberyesThe height of the sub-rectangle of the source image to draw into the destination context. Use the 3- or 5-argument syntax to omit this argument.
interface OptionsCropInfo {
  // The x-axis coordinate of the top left corner of the sub-rectangle of the source `image` to draw into the destination context. Use the 3- or 5-argument syntax to omit this argument.
  sx: number
  // The y-axis coordinate of the top left corner of the sub-rectangle of the source `image` to draw into the destination context. Use the 3- or 5-argument syntax to omit this argument.
  sy: number
  // The width of the sub-rectangle of the source `image` to draw into the destination context. If not specified, the entire rectangle from the coordinates specified by `sx` and `sy` to the bottom-right corner of the image is used. Use the 3- or 5-argument syntax to omit this argument.
  sw: number
  // The height of the sub-rectangle of the source `image` to draw into the destination context. Use the 3- or 5-argument syntax to omit this argument.
  sh: number
}

SizeInfo

File size information.

PropTypesRequiredDescription
textstringyesFile size as a string, 1.23MiB etc.
unitstringyesUnit of file size, MiB etc.
valuenumberyesThe size of the file as a suitable number, without units, 1.23 etc.
bytesnumberyesWhat is the size of the image in bytes.
interface SizeInfo {
  // File size as a string, `1.23MiB` etc.
  text: string
  // Unit of file size, `MiB` etc.
  unit: string
  // The size of the file as a suitable number, without units, `1.23` etc.
  value: number
  // What is the size of the image in bytes.
  bytes: number
}

VideoHandlerOptions

An options of the handleVideoFile function.

PropTypesRequiredDescription
enableDevicePixelRatiobooleannoWhether to enable the device pixel ratio, when 2 times, the size of the returned image is x2. Default is false.
mimeTypestringnoMultipurpose Internet Mail Extensions. Default is image/jpeg. https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types
isForcebooleannoWhen the image width or height is less than the set value, force the target image width or height to be adjusted to the set value. Default is false.
perResizenumbernoReduce the width each time. To prevent jagged edges when scaling an image. Default is 500.
qualitynumbernoA Number between 0 and 1 indicating the image quality to use for image formats that use lossy compression such as image/jpeg and image/webp. If this argument is anything else, the default value for image quality is used. The default value is 0.92. Other arguments are ignored. See toDataURL. Default is 0.9.
widthnumbernoThe width of the processed image. Default is 0.
heightnumbernoThe height of the processed image. Default is 0.
longEdgenumbernoThe value of long edge. Valid when width and height are 0. Default is 0.
cropInfoOptionsCropInfonoSee OptionsCropInfo.
currentTimenumbernoThe HTMLMediaElement interface's currentTime property specifies the current playback time in seconds. If it is longer than the video duration, the last frame will be captured. The default is a random timestamp in the video duration.
interface VideoHandlerOptions extends ImageHandlerOptions {
  // The `HTMLMediaElement` interface's `currentTime` property specifies the current playback time in seconds.
  // If it is longer than the video duration, the last frame will be captured.
  // The default is a `random` timestamp in the video duration.
  currentTime?: number
}

VideoHandlerResult

Data returned of the handleVideoFile function.

PropTypesRequiredDescription
blobBlobyesImage blob data.
datastringyesImage base64 data.
widthnumberyesThe width of the image.
heightnumberyesThe height of the image.
typestringyesThe type of the image.
sizeSizeInfoyesThe size information of the image. See SizeInfo.
urlstringyesA blob url of the image.
elementHTMLImageElement/HTMLCanvasElementyesHTMLImageElement or HTMLCanvasElement.
rawMediaFileHandlerRawDatayesRaw information of the image file being processed. See MediaFileHandlerRawData.
videoInfoVideoInfoyesWhen taking a screenshot of the video, the original video file information. See VideoInfo.
interface VideoHandlerResult extends ImageHandlerResult {
  // When taking a screenshot of the video, the original video file information.
  // See [VideoInfo](#VideoInfo).
  videoInfo: VideoInfo
}

VideoInfo

The original video file information.

PropTypesRequiredDescription
urlstringyesA blob url of the video file.
videoFileFileyesThe video file object.
videoWidthnumberyesThe width of the video.
videoHeightnumberyesThe height of the video.
durationnumberyesThe duration of the video.
currentTimenumberyesThe time point of the video screenshot.
interface VideoInfo {
  // A blob url of the video file.
  url: string
  // The video file object.
  videoFile: File
  // The width of the video.
  videoWidth: number
  // The height of the video.
  videoHeight: number
  // The duration of the video.
  duration: number
  // The time point of the video screenshot.
  currentTime: number
}

Other methods

These methods's documentation see https://github.com/capricorncd/zx-sml

import {
  fileToBase64,
  createElement,
  formatBytes,
  splitBase64,
  createBlobURL,
  base64ToBlob,
} from 'image-process'

License

Code and documentation copyright 2018-Present. Capricorncd. Code released under the MIT License.

4.3.3

5 months ago

4.3.2

1 year ago

4.3.1

1 year ago

4.3.0

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.1.0

3 years ago

4.1.1

3 years ago

4.0.0

4 years ago

3.3.8

4 years ago

3.3.7

5 years ago

3.3.6

5 years ago

3.3.5

5 years ago

3.3.4

5 years ago

3.3.3

6 years ago

3.3.2

6 years ago

3.3.1

6 years ago

3.3.0

6 years ago

3.2.1

6 years ago

3.2.0

6 years ago

3.1.0

6 years ago

3.0.4

6 years ago

3.0.3

6 years ago

3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.2.4

6 years ago

2.2.3

6 years ago

2.2.2

7 years ago