Skip to content

Commit

Permalink
fixes #6 by @jpike88
Browse files Browse the repository at this point in the history
  • Loading branch information
ribizli committed Sep 4, 2020
1 parent 3be45ed commit 02087b4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export interface ResizeOptions {
maxWidth?: number;
quality?: number;
type?: string;
bgColor?: string;
}

export interface Options {
Expand All @@ -105,9 +106,12 @@ export interface Options {
- `resize.maxWidth`
- `resize.quality`: default: `0.7`
- `resize.type`: default: as the original image
- `resize.bgColor`: default: undefined
- `allowedExtensions`: default: undefined

Resize algorithm ensures, that the resized image can fit into the specified `resize.maxHeight x resize.maxWidth` size.
`bgColor` is an optional parameter to force a certain background color when 'flattening' transparent images (as the default
is black and this may be undesirable).

Allowed extensions array (e.g. `['jpg', 'jpeg', 'png']`; case insensitive): if specified and an input file
has different extension the `(imageSelected)` event is fired with the error field set to 'Extension Not Allowed'.
Expand Down
5 changes: 3 additions & 2 deletions projects/ngx-image2dataurl/src/lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ export interface ImageResult {
dataURL?: string;
error?: any;
resized?: {
dataURL: string;
type: string;
dataURL: string;
type: string;
}
}

Expand All @@ -14,6 +14,7 @@ export interface ResizeOptions {
maxWidth?: number;
quality?: number;
type?: string;
bgColor?: string;
}

export interface Options {
Expand Down
9 changes: 8 additions & 1 deletion projects/ngx-image2dataurl/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function createImageFromDataUrl(dataURL: string) {
}

export async function resizeImage(dataURL: string, {
bgColor,
maxHeight,
maxWidth,
quality = 0.7,
Expand All @@ -29,7 +30,7 @@ export async function resizeImage(dataURL: string, {
width = maxWidth;
}

if (height > maxHeight ) {
if (height > maxHeight) {
width = Math.round(width * maxHeight / height);
height = maxHeight;
}
Expand All @@ -39,6 +40,12 @@ export async function resizeImage(dataURL: string, {

//draw image on canvas
const ctx = canvas.getContext("2d");

if (bgColor) {
ctx.fillStyle = bgColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}

ctx.drawImage(image, 0, 0, width, height);

// get the data from canvas as 70% jpg (or specified type).
Expand Down

0 comments on commit 02087b4

Please sign in to comment.