Skip to content

Commit 88bd98b

Browse files
author
Sergey Russkih
committed
update
1 parent 7de8cd2 commit 88bd98b

File tree

7 files changed

+48
-314
lines changed

7 files changed

+48
-314
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ document.getElementById('file-input').onchange = function (e) {
1919
function (img) {
2020
document.body.appendChild(img);
2121
},
22-
{maxWidth: 600}
22+
{width: 600}
2323
);
2424
};
2525
```
@@ -42,7 +42,7 @@ document.getElementById('file-input').onchange = function (e) {
4242
function (img) {
4343
document.body.appendChild(img);
4444
},
45-
{maxWidth: 600}
45+
{width: 600}
4646
);
4747
if (!loadingImage) {
4848
// Alternative code ...
@@ -59,7 +59,7 @@ document.getElementById('file-input').onchange = function (e) {
5959
function (img) {
6060
document.body.appendChild(img);
6161
},
62-
{maxWidth: 600}
62+
{width: 600}
6363
);
6464
loadingImage.onload = loadingImage.onerror = null;
6565
};
@@ -78,16 +78,16 @@ window.loadImage(
7878
document.body.appendChild(img);
7979
}
8080
},
81-
{maxWidth: 600}
81+
{width: 600}
8282
);
8383
```
8484

8585
The optional third argument is a map of options:
8686

87-
* **maxWidth**: Defines the maximum width of the img/canvas element.
88-
* **maxHeight**: Defines the maximum height of the img/canvas element.
89-
* **minWidth**: Defines the minimum width of the img/canvas element.
90-
* **minHeight**: Defines the minimum height of the img/canvas element.
87+
* **width**: Defines the width of the img/canvas element.
88+
* **height**: Defines the height of the img/canvas element.
89+
* **crop**: Defines croping of the canvas element. (for now works only for canvas)
90+
* **upscale**: Define upscaling of the img/canvas element.
9191
* **canvas**: Defines if the returned element should be a [canvas](https://developer.mozilla.org/en/HTML/Canvas) element.
9292
* **noRevoke**: By default, the [created object URL](https://developer.mozilla.org/en/DOM/window.URL.createObjectURL) is revoked after the image has been loaded, except when this option is set to *true*.
9393

@@ -100,10 +100,10 @@ window.loadImage(
100100
document.body.appendChild(img);
101101
},
102102
{
103-
maxWidth: 600,
104-
maxHeight: 300,
105-
minWidth: 100,
106-
minHeight: 50,
103+
width: 600,
104+
height: 300,
105+
crop: true,
106+
upsacel: true,
107107
canvas: true,
108108
noRevoke: true
109109
}

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ <h2>Result</h2>
6969
</div>
7070
</div>
7171
</div>
72-
<script src="load-image.min.js"></script>
72+
<script src="load-image.js"></script>
7373
<!-- jQuery and Bootstrap JS are not required, but included for the demo -->
74-
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
74+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
7575
<script src="http://blueimp.github.com/cdn/js/bootstrap.min.js"></script>
7676
<script>
7777
/*global jQuery, window, document */

load-image.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,55 @@
5858
// as image, else the scaled image:
5959
loadImage.scale = function (img, options) {
6060
options = options || {};
61+
6162
var canvas = document.createElement('canvas'),
6263
width = img.width,
6364
height = img.height,
65+
x = 0, y = 0,
66+
scale;
67+
68+
// if crop in options then we have to get biggest scale factor
69+
if (options.crop || false){
6470
scale = Math.max(
65-
(options.minWidth || width) / width,
66-
(options.minHeight || height) / height
71+
(options.width) / width,
72+
(options.height) / height
73+
);
74+
} else {
75+
// else we have to get lowest
76+
scale = Math.min(
77+
(options.width) / width,
78+
(options.height) / height
6779
);
68-
if (scale > 1) {
69-
width = parseInt(width * scale, 10);
70-
height = parseInt(height * scale, 10);
7180
}
72-
scale = Math.min(
73-
(options.maxWidth || width) / width,
74-
(options.maxHeight || height) / height
75-
);
76-
if (scale < 1) {
81+
82+
// if scale factor is below 1, or more then and upscale options is
83+
// then we going to calculate new image size
84+
if (scale < 1 || (scale > 1 && (options.upscale || false))){
7785
width = parseInt(width * scale, 10);
7886
height = parseInt(height * scale, 10);
7987
}
88+
89+
if (options.crop || false){
90+
var dx = width - Math.min(width, options.width || width),
91+
dy = height - Math.min(height, options.height || height);
92+
93+
// if we have difference with required size and
94+
// currently calculated size, we have to calculate
95+
// image offset to got correct croping
96+
if (dx || dy) {
97+
x = -1 * parseInt(dx / 2, 10);
98+
y = -1 * parseInt(dy / 2, 10);
99+
}
100+
}
101+
80102
if (img.getContext || (options.canvas && canvas.getContext)) {
81103
canvas.width = width;
82104
canvas.height = height;
83105
canvas.getContext('2d')
84-
.drawImage(img, 0, 0, width, height);
106+
.drawImage(img, x, y, width, height);
85107
return canvas;
86108
}
109+
87110
img.width = width;
88111
img.height = height;
89112
return img;

load-image.min.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

package.json

Lines changed: 0 additions & 42 deletions
This file was deleted.

test/index.html

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)