@@ -8,6 +8,10 @@ export interface AddAuthImgDialogData {
8
8
attribute : Attribute ;
9
9
}
10
10
11
+ const MAX_SIZE = 100 ;
12
+ const MIN_SIZE = 50 ;
13
+ const MAX_LENGTH = 5120 ;
14
+
11
15
@Component ( {
12
16
selector : 'perun-web-apps-add-auth-img-dialog' ,
13
17
templateUrl : './add-auth-img-dialog.component.html' ,
@@ -31,6 +35,7 @@ export class AddAuthImgDialogComponent implements OnInit {
31
35
this . theme = this . data . theme ;
32
36
this . attribute = this . data . attribute ;
33
37
this . newImage = this . attribute . value as unknown as string ;
38
+ this . imageType = null ;
34
39
}
35
40
36
41
handleInputChange ( e : Event | DragEvent ) : void {
@@ -42,13 +47,41 @@ export class AddAuthImgDialogComponent implements OnInit {
42
47
return ;
43
48
}
44
49
reader . onload = this . _handleReaderLoaded . bind ( this ) as ( ) => void ;
50
+ this . imageType = file . type ;
45
51
reader . readAsDataURL ( file ) ;
46
52
}
47
53
48
- _handleReaderLoaded ( e : ProgressEvent ) : void {
49
- const reader = e . target as FileReader ;
50
- const result = reader . result as string ;
51
- this . imgTooLong = result . length >= 5120 ;
54
+ _handleReaderLoaded ( e ) {
55
+ const reader = e . target ;
56
+
57
+ let size = MAX_SIZE ;
58
+ let result = null ;
59
+ do {
60
+ const img = document . createElement ( "img" ) ;
61
+ img . src = reader . result ;
62
+ const canvas = document . createElement ( "canvas" ) ;
63
+ let ctx = canvas . getContext ( "2d" ) ;
64
+ ctx . drawImage ( img , 0 , 0 ) ;
65
+ const width = img . width ;
66
+ const height = img . height ;
67
+ if ( width > height ) {
68
+ if ( width > MAX_SIZE ) {
69
+ height *= MAX_SIZE / width ;
70
+ width = MAX_SIZE ;
71
+ }
72
+ } else if ( height > MAX_SIZE ) {
73
+ width *= MAX_SIZE / height ;
74
+ height = MAX_SIZE ;
75
+ }
76
+ canvas . width = width ;
77
+ canvas . height = height ;
78
+ ctx = canvas . getContext ( "2d" ) ;
79
+ ctx . drawImage ( img , 0 , 0 , width , height ) ;
80
+ result = canvas . toDataURL ( this . imageType ) ;
81
+ size -= 10 ;
82
+ } while ( size >= MIN_SIZE && result . length >= MAX_LENGTH ) ;
83
+
84
+ this . imgTooLong = result . length >= MAX_LENGTH ;
52
85
this . newImage = result ;
53
86
}
54
87
@@ -73,20 +106,19 @@ export class AddAuthImgDialogComponent implements OnInit {
73
106
const MIN_COLOR = 120 ; // Min value for a color component
74
107
const FILL_CHANCE = 0.5 ; // Chance of a square being filled [0, 1]
75
108
const SQUARE = 20 ; // Size of a grid square in pixels
76
- const GRID = 5 ; // Number of squares width and height
77
- const SIZE = SQUARE * GRID ; // Size of the canvas
109
+ const GRID = MAX_SIZE / SQUARE ; // Number of squares width and height
78
110
const FILL_COLOR = '#FFFFFF' ; // canvas background color
79
111
80
112
/* Create a temporary canvas */
81
113
function setupCanvas ( ) : HTMLCanvasElement {
82
114
const canvas = document . createElement ( 'canvas' ) ;
83
- canvas . width = SIZE ;
84
- canvas . height = SIZE ;
115
+ canvas . width = MAX_SIZE ;
116
+ canvas . height = MAX_SIZE ;
85
117
86
118
// Fill canvas background
87
119
const context = canvas . getContext ( '2d' ) ;
88
120
context . beginPath ( ) ;
89
- context . rect ( 0 , 0 , SIZE , SIZE ) ;
121
+ context . rect ( 0 , 0 , MAX_SIZE , MAX_SIZE ) ;
90
122
context . fillStyle = FILL_COLOR ;
91
123
context . fill ( ) ;
92
124
return canvas ;
0 commit comments