1
- import { CanvasService } from '../canvas.service ' ;
1
+ import { canvasService } from '../canvasService ' ;
2
2
import { OperatorFunction , ResizeOptions } from '../models' ;
3
- import { base64ToImgElement } from '../utils' ;
4
3
5
- // Need img/canvas source
6
- // Need width/height
7
4
/**
8
5
* @see https://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/
9
6
* @see http://stackoverflow.com/a/19262385/5688490
@@ -14,60 +11,58 @@ import { base64ToImgElement } from '../utils';
14
11
* TODO: Accept File, too
15
12
*/
16
13
export function resize ( options : ResizeOptions ) : OperatorFunction {
17
- return ( base64 : string ) => {
14
+ return ( ) => {
18
15
return new Promise ( resolve => {
19
- base64ToImgElement ( base64 ) . then ( img => {
20
- const resizeNeeded = img . width > options . maxWidth || img . height > options . maxHeight ;
16
+ const oldWidth = canvasService . canvas . width ;
17
+ const oldHeight = canvasService . canvas . height ;
21
18
22
- if ( ! resizeNeeded ) {
23
- console . log ( `No resizing needed, image of ${ img . width } x${ img . height } px is smaller than ${ options . maxWidth } x${ options . maxHeight } px` ) ;
24
- CanvasService . setSize ( img . width , img . height ) ;
25
- CanvasService . drawImage ( img . imgElement , img . width , img . height ) ;
26
- } else {
27
- let newWidth : number ;
28
- let newHeight : number ;
29
- let downscalingSteps : number ;
19
+ const resizeNeeded = oldWidth > options . maxWidth || oldHeight > options . maxHeight ;
30
20
31
- // TODO: Is this really working for all formats? What if maxWidth = 2000, maxHeight = 20 ?
32
- if ( img . width > img . height ) {
33
- newWidth = options . maxWidth ;
34
- newHeight = Math . round ( img . height * newWidth / img . width ) ;
35
- downscalingSteps = Math . ceil ( Math . log ( img . width / newWidth ) / Math . log ( 2 ) ) ;
36
- } else {
37
- newHeight = options . maxHeight ;
38
- newWidth = Math . round ( img . width * newHeight / img . height ) ;
39
- downscalingSteps = Math . ceil ( Math . log ( img . height / newHeight ) / Math . log ( 2 ) ) ;
40
- }
21
+ if ( ! resizeNeeded ) {
22
+ console . log ( `No resizing needed, image of ${ oldWidth } x${ oldHeight } px is smaller than ${ options . maxWidth } x${ options . maxHeight } px` ) ;
23
+ resolve ( ) ;
24
+ } else {
25
+ let newWidth : number ;
26
+ let newHeight : number ;
27
+ let downscalingSteps : number ;
41
28
42
- console . log ( `Resizing from ${ img . width } x${ img . height } px to ${ newWidth } x${ newHeight } in ${ downscalingSteps } steps` ) ;
29
+ // TODO: Is this really working for all formats? What if maxWidth = 2000, maxHeight = 20 ?
30
+ if ( oldWidth > oldHeight ) {
31
+ newWidth = options . maxWidth ;
32
+ newHeight = Math . round ( oldHeight * newWidth / oldWidth ) ;
33
+ downscalingSteps = Math . ceil ( Math . log ( oldWidth / newWidth ) / Math . log ( 2 ) ) ;
34
+ } else {
35
+ newHeight = options . maxHeight ;
36
+ newWidth = Math . round ( oldWidth * newHeight / oldHeight ) ;
37
+ downscalingSteps = Math . ceil ( Math . log ( oldHeight / newHeight ) / Math . log ( 2 ) ) ;
38
+ }
43
39
44
- if ( downscalingSteps > 1 ) {
45
- // To get the best result, we need to resize the image by 50% again and again until we reach the final dimensions
46
- // Step 1
47
- let oldScale = 1 ;
48
- let currentStepScale = .5 ;
49
- CanvasService . setSize ( img . width * currentStepScale , img . height * currentStepScale ) ;
50
- CanvasService . drawImage ( img . imgElement , img . width * currentStepScale , img . height * currentStepScale ) ;
40
+ console . log ( `Resizing from ${ oldWidth } x${ oldHeight } px to ${ newWidth } x${ newHeight } in ${ downscalingSteps } steps` ) ;
51
41
52
- // Step i
53
- for ( let i = 2 ; i < downscalingSteps ; i ++ ) {
54
- oldScale = currentStepScale ;
55
- currentStepScale = currentStepScale * .5 ;
56
- CanvasService . drawImage ( CanvasService . canvas , img . width * currentStepScale , img . height * currentStepScale , img . width * oldScale , img . height * oldScale ) ;
57
- }
42
+ if ( downscalingSteps === 1 ) {
43
+ // The image can directly be resized to the final dimensions
44
+ canvasService . resize ( newWidth , newHeight ) ;
45
+ } else {
46
+ // To get the best result, we need to resize the image by 50% again and again until we reach the final dimensions
47
+ // Step 1
48
+ let oldScale = 1 ;
49
+ let currentStepScale = 1 ;
50
+ // canvasService.resize(oldWidth * currentStepScale, oldHeight * currentStepScale);
58
51
59
- // Down-scaling step i+1 (draw final result)
60
- CanvasService . drawImage ( CanvasService . canvas , newWidth , newHeight , img . width * currentStepScale , img . height * currentStepScale ) ;
61
- CanvasService . crop ( newWidth , newHeight ) ;
62
- } else {
63
- // The image can directly be resized to the final dimensions
64
- CanvasService . setSize ( newWidth , newHeight ) ;
65
- CanvasService . drawImage ( img . imgElement , newWidth , newHeight ) ;
52
+ // Step i
53
+ for ( let i = 1 ; i < downscalingSteps ; i ++ ) {
54
+ oldScale = currentStepScale ;
55
+ currentStepScale = currentStepScale * .5 ;
56
+ canvasService . drawImage ( canvasService . canvas , oldWidth * currentStepScale , oldHeight * currentStepScale , oldWidth * oldScale , oldHeight * oldScale ) ;
66
57
}
58
+
59
+ // Down-scaling step i+1 (draw final result)
60
+ canvasService . drawImage ( canvasService . canvas , newWidth , newHeight , oldWidth * currentStepScale , oldHeight * currentStepScale ) ;
61
+ canvasService . crop ( newWidth , newHeight ) ;
67
62
}
63
+ }
68
64
69
- resolve ( CanvasService . canvas . toDataURL ( ) ) ;
70
- } ) ;
65
+ resolve ( ) ;
71
66
} ) ;
72
67
} ;
73
68
}
0 commit comments