-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageManipulator.js
executable file
·198 lines (173 loc) · 5.75 KB
/
ImageManipulator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"use strict";
// Constructor
// ---------------------------------------------------------------------------------------
function ImageManipulator() {
}
// Private Function Helpers / Properties
// ---------------------------------------------------------------------------------------
var mainCanvas;
var imageQuality = 0.5;
var imageFormat = "jpeg";
var canvasBGColor = "#000";
/* 'Singleton' Pattern Canvas Creation
* If a canvas has been created before, return it, else create a new one.
*/
function createCanvas(width, height) {
if (typeof mainCanvas === "undefined") {
mainCanvas = document.createElement("canvas");
mainCanvas.setAttribute("id", "ImageManipulatorCanvas");
mainCanvas.setAttribute("width", width);
mainCanvas.setAttribute("height", height);
}
return mainCanvas;
}
/* Checks to determine if the height of the image exceeds the height of
* the desired height.
*/
function heightIsContrainingSide(widthRatio, heightRatio) {
if (widthRatio > heightRatio) {
return true;
} else {
return false;
}
}
/* Returns the aspect ration of width/height as a decimal
*/
function getImageAspectRatio(img) {
return (img.width / img.height);
}
/* Returns the ratio of an original size to the desired size as a decimal
* (desiredSize / originalSize)
*/
function getDesiredRatio(original, desired) {
return (desired / original);
}
/* Changes the background color of the canvas when an image is drawn
* Can be any html acceptable format
*/
function changeCanvasBGColor(canvas) {
var context = canvas.getContext("2d");
context.globalCompositeOperation = "destination-over";
context.fillStyle = canvasBGColor;
context.fillRect(0, 0, canvas.width, canvas.height);
}
// Exposed Private Function Definitions
// ---------------------------------------------------------------------------------------
/* Resizes an image to fit within new contraints
* Best compared to a sizeToFit function
* Returns a dataURL for the new image
*/
function resizeImage(img, desiredWidth, desiredHeight) {
var widthRatio = getDesiredRatio(img.width, desiredWidth);
var heightRatio = getDesiredRatio(img.height, desiredHeight);
var imageRatio = getImageAspectRatio(img);
var canvas = createCanvas(desiredWidth, desiredHeight);
var context = canvas.getContext("2d");
canvas.width = desiredWidth;
canvas.height = desiredHeight;
if (heightIsContrainingSide(widthRatio, heightRatio) === true) {
context.drawImage(img, ((canvas.width - (desiredHeight * imageRatio)) / 2), 0, desiredHeight * imageRatio, desiredHeight);
} else {
context.drawImage(img, 0, ((canvas.height - (desiredWidth / imageRatio)) / 2), desiredWidth, desiredWidth / imageRatio);
}
context = changeCanvasBGColor(canvas);
var dataURL = canvas.toDataURL("image/" + imageFormat, imageQuality);
return dataURL;
}
/* Rotates an image a defined number of degrees clockwise
* Returns a dataURL for the new image
*/
function rotateClockwise(img, degrees) {
var canvas = createCanvas(img.width, img.height);
var context = canvas.getContext("2d");
canvas.width = img.height;
canvas.height = img.width;
context.translate(img.height, 0);
context.rotate(degrees * (Math.PI / 180));
context.drawImage(img, 0, 0);
context = changeCanvasBGColor(canvas);
var dataURL = canvas.toDataURL("image/" + imageFormat, imageQuality);
return dataURL;
}
/* Rotates an image a defined number of degrees counter-clockwise
* Returns a dataURL for the new image
*/
function rotateCounterClockwise(img, degrees) {
var canvas = createCanvas(img.width, img.height);
var context = canvas.getContext("2d");
canvas.width = img.height;
canvas.height = img.width;
context.translate(0, img.width);
context.rotate(-degrees * (Math.PI / 180));
context.drawImage(img, 0, 0);
context = changeCanvasBGColor(canvas);
var dataURL = canvas.toDataURL("image/" + imageFormat, imageQuality);
return dataURL;
}
/* Checks if an image is in the portrait orientation
* Will return false for square images
* Returns boolean
*/
function isPortraitImage(img) {
if (img.height > img.width) {
return true;
} else {
return false;
}
}
/* Destroys the working canvas created for manipulations
* Returns undefined
*/
function destroyCanvas() {
mainCanvas = undefined;
}
/* Sets the image quality for the output image
* Takes decimal between 0 and 1
* Returns undefined
*/
function setImageQuality(quality) {
imageQuality = quality;
}
/* Sets the image format for the output image
* Takes string image type - png, gif, jpg
* Returns undefined
*/
function setImageFormat(format) {
imageFormat = format;
}
/* Sets the image background color for the output image
* Changes the letterboxing color around the image
* Takes decimal between 0 and 1
* Returns undefined
*/
function setBGColor(hexColor) {
canvasBGColor = hexColor;
}
// Exposed Functions
// ---------------------------------------------------------------------------------------
ImageManipulator.prototype = {
resizeImage: function(img, desiredWidth, desiredHeight) {
return resizeImage(img, desiredWidth, desiredHeight);
},
rotateClockwise: function(img, degrees) {
return rotateClockwise(img, degrees);
},
rotateCounterClockwise: function(img, degrees) {
return rotateCounterClockwise(img, degrees);
},
isPortraitImage: function(img) {
return isPortraitImage(img);
},
destroyCanvas: function() {
return destroyCanvas();
},
setImageQuality: function(quality) {
return setImageQuality(quality);
},
setImageFormat: function(format) {
return setImageFormat(format);
},
setBGColor: function(color) {
return setBGColor(color);
}
};