diff --git a/README.md b/README.md index 573f017..6538241 100644 --- a/README.md +++ b/README.md @@ -4,25 +4,26 @@ Is a tool to split images. The code is based on [image-splitter](https://github.com/achimoraites/image-splitter) code. -The idea is that you tell to the lib what image and: +The idea is that you tell to the lib what source image to use and: - N images per row and M images per colum -> You get image splitted into N x M images. - N px per row and M px per colum -> You get image splitted into images with N x M px. -Dos parámetros: - -1. `image` puede ser String (fichero local o url) o Buffer o Jimp Object -2. `options` es un JSON con los campos - 1. `mode` -> Split mode `grid` (by default) | `vertical` | `horizontal` - 2. `slices` -> Number of slices - 3. `width` -> Width in pixels per slice - 4. `height` -> Height in pixels per slice - 5. `name` -> name to the slices - 6. `extension` -> extension to the slices - 7. `unique` -> only non-repeated slices with `true`, `false` by default - -Si `image` no es un fichero local entonces en `options` tiene que venir si o si lo siguiente: - -- `name` -- ¿`extension`? - +Then you tell if you want to store them and how to return data. + +Inputs are two arguments: + +1. `image` -> Source image => `String` (local file or url) | `Buffer` | `Jimp Object` +2. `options` -> JSON with next properties: + 1. `mode` -> Split mode => `grid` (by default) | `vertical` | `horizontal` + 2. `tiles` -> JSON with properties related to the slices or commonly known as tiles + 1. `rows` -> Number of rows + 2. `columns` -> Number of columns + 3. `width` -> Width in pixels per tile + 4. `height` -> Height in pixels per tile + 5. `unique` -> If you need all tiles or non-repeated => `false` (all tiles by default) | `true` (non-repeated tiles) + 3. `output` -> JSON with properties related to the output / return data and how store it + 1. `data` -> Type of data to be returned => `buffer` (default) | `path` (local path) + 2. `path` -> Local path to save the tiles + 3. `name` -> Preffix name to save the tiles + 4. `extension` -> Supported extension to save tiles => `jpg` | `png` | `bmp` | `gif` | `tiff` diff --git a/src/enums.ts b/src/enums.ts index 9948940..f67de8f 100644 --- a/src/enums.ts +++ b/src/enums.ts @@ -5,7 +5,16 @@ export enum Mode { Horizontal = 'horizontal' } -export enum Output { - Path = 'path', - Buffer = 'buffer' +export enum Data { + Buffer = 'buffer', + Path = 'path' +} + +export enum Extension { + JPG = 'jpg', + JPEG = 'jpeg', + PNG = 'png', + BMP = 'bmp', + GIF = 'gif', + TIFF = 'tiff' } diff --git a/src/types.d.ts b/src/types.d.ts index ae42794..338b808 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -1,39 +1,48 @@ import Jimp from 'jimp/*' -import { Output } from './enums' +import { Data, Extension } from './enums' export type Image = string | Buffer | Jimp export type Images = string[] -/** - * 1. Modo de corte -> Cuadricula, vertical, horizontal - * 2. Datos para el corte - * 2.1 Grid -> n slices | width + height - * 2.2 Vertical -> n slices | height - * 2.3 Horizontal -> n slices | weidth - * 3. No slices repetidas - * 4. Que se devuelve -> Path | Buffer - * 4.1 Path -> Se guardan las imagenes y - * se devuelve un array string con el path de cada slices - * 4.2 Buffer -> No se guardan las imágenes y - * se devuelve un array Buffer con la data de cada slice - **/ -export interface Options { - mode: Mode +export interface Size { + width: number + height: number +} + +export interface Tile { rows?: number columns?: number width?: number height?: number unique?: boolean - output?: Output - save?: boolean +} + +export interface Output { + data: Data path?: string name?: string - extension?: string + extension?: Extension } -export interface Size { - width: number - height: number +/** + * 1. Slice Mode -> mode => grid | vertical | horizontal + * 2. Data for the slices -> tiles + * 2.1 Grid tiles -> rows + columns | width + height + * 2.2 Vertical tiles -> rows | height + * 2.3 Horizontal tiles -> columns | width + * 2.4 Non-repeated tiles -> unique => true | false + * 3. Output info -> output + * 3.1 Data to return -> data => buffer | path + * If data is path or you want to store the slices it needs to provide more properties + * 3.2 Local path to store slices -> path + * 3.3 Name preffix to slices -> name + * 3.4 Extension to slices -> extension => jpg | png | bmp | gif | tiff + **/ + +export interface Options { + mode: Mode + tiles?: Tile + output?: Output } diff --git a/src/utils.ts b/src/utils.ts index eda81df..9b44569 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -17,8 +17,9 @@ export const isSubmultiple = (numerator: any, denominator: any): boolean => isNa export const validPairSubmultiples = (numerator1: any, denominator1: any, numerator2: any, denominator2: any): boolean => isSubmultiple(numerator1, denominator1) && isSubmultiple(numerator2, denominator2) -export const parseModeSlices = (mode: Mode, rows: any, columns: any, width: any, height: any, size: Size): boolean => { +export const parseModeSlices = (mode: Mode, tiles: any, size: Size): boolean => { parseMode(mode) + const { rows, columns, width, height } = tiles switch (mode) { // Mode Grid -> rows + columns || width + height case Mode.Grid: @@ -61,7 +62,7 @@ export const parseModeSlices = (mode: Mode, rows: any, columns: any, width: any, export const parseOptions = (options: any, size: Size): boolean => { // Mode + Slices (Rows + Columns || Width + Height) - parseModeSlices(options.mode, options?.row, options?.columns, options?.width, options?.height, size) + parseModeSlices(options.mode, options?.tiles, size) // Name // Extension // Unique diff --git a/tests/utils.spec.ts b/tests/utils.spec.ts index bbb485a..21f0693 100644 --- a/tests/utils.spec.ts +++ b/tests/utils.spec.ts @@ -1,7 +1,7 @@ import path from 'path' import { Mode } from '../src/enums' import { SpliteaError } from '../src/errors' -import { Size } from '../src/types' +import { Size, Tile } from '../src/types' import { isNatural, isSubmultiple, parseMode, parseModeSlices, validPairNaturalNumbers, validPairSubmultiples } from '../src/utils' const imgFolder = path.join(__dirname, '..', 'examples') @@ -114,90 +114,87 @@ describe('Test Utils Module', () => { const mode = Mode.Grid const size: Size = { width: imgTest.width, height: imgTest.height } const errorSubmultiple = new SpliteaError(`you need to provide two natural submultiples of ${size.width} and ${size.height}, columns + rows or width (px) + height (px)`) + const tiles: Tile = { rows: undefined, columns: undefined, width: undefined, height: undefined, unique: false } // OK Rows & Columns - let rows: any = 2 - let columns: any = 2 - let width: any - let height: any - expect(parseModeSlices(mode, rows, columns, width, height, size)).toBeTruthy() + tiles.rows = 2 + tiles.columns = 2 + expect(parseModeSlices(mode, tiles, size)).toBeTruthy() // OK Width & Height - rows = undefined - columns = undefined - width = 2 - height = 2 - expect(parseModeSlices(mode, rows, columns, width, height, size)).toBeTruthy() + tiles.rows = undefined + tiles.columns = undefined + tiles.width = 2 + tiles.height = 2 + expect(parseModeSlices(mode, tiles, size)).toBeTruthy() // Fail Rows Ok Columns - rows = 3 - columns = 2 - width = undefined - height = undefined - expect(() => parseModeSlices(mode, rows, columns, width, height, size)).toThrow(errorSubmultiple) + tiles.rows = 3 + tiles.columns = 2 + tiles.width = undefined + tiles.height = undefined + expect(() => parseModeSlices(mode, tiles, size)).toThrow(errorSubmultiple) // Ok Rows Fail Columns - rows = 2 - columns = 3 - width = undefined - height = undefined - expect(() => parseModeSlices(mode, rows, columns, width, height, size)).toThrow(errorSubmultiple) + tiles.rows = 2 + tiles.columns = 3 + tiles.width = undefined + tiles.height = undefined + expect(() => parseModeSlices(mode, tiles, size)).toThrow(errorSubmultiple) // Fail Width & Ok Height - rows = undefined - columns = undefined - width = 3 - height = 2 - expect(() => parseModeSlices(mode, rows, columns, width, height, size)).toThrow(errorSubmultiple) + tiles.rows = undefined + tiles.columns = undefined + tiles.width = 3 + tiles.height = 2 + expect(() => parseModeSlices(mode, tiles, size)).toThrow(errorSubmultiple) // Ok Width & Fail Height - rows = undefined - columns = undefined - width = 2 - height = 3 - expect(() => parseModeSlices(mode, rows, columns, width, height, size)).toThrow(errorSubmultiple) + tiles.rows = undefined + tiles.columns = undefined + tiles.width = 2 + tiles.height = 3 + expect(() => parseModeSlices(mode, tiles, size)).toThrow(errorSubmultiple) }) test('Horizontal', () => { const mode = Mode.Horizontal const size: Size = { width: imgTest.width, height: imgTest.height } const errorSubmultiple = new SpliteaError(`you need to provide one natural submultiple of ${size.width}, columns or width (px)`) - const rows: any = undefined - const height: any = undefined + const tiles: Tile = { rows: undefined, columns: undefined, width: undefined, height: undefined, unique: false } // OK Columns - let columns: any = 2 - let width: any - expect(parseModeSlices(mode, rows, columns, width, height, size)).toBeTruthy() + tiles.columns = 2 + tiles.width = undefined + expect(parseModeSlices(mode, tiles, size)).toBeTruthy() // OK Width - columns = undefined - width = 2 - expect(parseModeSlices(mode, rows, columns, width, height, size)).toBeTruthy() + tiles.columns = undefined + tiles.width = 2 + expect(parseModeSlices(mode, tiles, size)).toBeTruthy() // Fail Columns - columns = 3 - width = undefined - expect(() => parseModeSlices(mode, rows, columns, width, height, size)).toThrow(errorSubmultiple) + tiles.columns = 3 + tiles.width = undefined + expect(() => parseModeSlices(mode, tiles, size)).toThrow(errorSubmultiple) // Fail Width - columns = undefined - width = 3 - expect(() => parseModeSlices(mode, rows, columns, width, height, size)).toThrow(errorSubmultiple) + tiles.columns = undefined + tiles.width = 3 + expect(() => parseModeSlices(mode, tiles, size)).toThrow(errorSubmultiple) }) test('Vertical', () => { const mode = Mode.Vertical const size: Size = { width: imgTest.width, height: imgTest.height } const errorSubmultiple = new SpliteaError(`you need to provide one natural submultiple of ${size.height}, rows or height (px)`) - const columns: any = undefined - const width: any = undefined + const tiles: Tile = { rows: undefined, columns: undefined, width: undefined, height: undefined, unique: false } // OK Rows - let rows: any = 2 - let height: any - expect(parseModeSlices(mode, rows, columns, width, height, size)).toBeTruthy() + tiles.rows = 2 + tiles.height = undefined + expect(parseModeSlices(mode, tiles, size)).toBeTruthy() // OK Height - rows = undefined - height = 2 - expect(parseModeSlices(mode, rows, columns, width, height, size)).toBeTruthy() + tiles.rows = undefined + tiles.height = 2 + expect(parseModeSlices(mode, tiles, size)).toBeTruthy() // Fail Rows - rows = 3 - height = undefined - expect(() => parseModeSlices(mode, rows, columns, width, height, size)).toThrow(errorSubmultiple) + tiles.rows = 3 + tiles.height = undefined + expect(() => parseModeSlices(mode, tiles, size)).toThrow(errorSubmultiple) // Fail Height - rows = undefined - height = 3 - expect(() => parseModeSlices(mode, rows, columns, width, height, size)).toThrow(errorSubmultiple) + tiles.rows = undefined + tiles.height = 3 + expect(() => parseModeSlices(mode, tiles, size)).toThrow(errorSubmultiple) }) }) })