Skip to content

Commit

Permalink
* imageTransform.type -> imageTransform.strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
RubaXa committed Nov 20, 2013
1 parent c763739 commit e7a952b
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 42 deletions.
23 changes: 21 additions & 2 deletions README.md
Expand Up @@ -765,12 +765,12 @@ FileAPI.Image(imageFile)
---

<a name="FileAPI.Image.resize"></a>
### resize(width`:Number`, height`:Number`[, type`:String`])`:FileAPI.Image`
### resize(width`:Number`, height`:Number`[, strategy`:String`])`:FileAPI.Image`
Resize image.

* width — new image width
* height — new image height
* type — enum: `min`, `max`, `preview`. By default `undefined`.
* strategy — enum: `min`, `max`, `preview`. By default `undefined`.

```js
FileAPI.Image(imageFile)
Expand Down Expand Up @@ -1377,6 +1377,25 @@ Button like link.
<a name="Changelog"></a>
## Changelog
<ul>
<li>* resize: `imageTransform.type` rename to `imageTransform.strategy` (!!!)</li>
</ul>
### 2.0.2
<ul>
<li>+ test: upload headers</li>
<li>+ test: upload + camanjs</li>
<li>+ test: upload + autoOrientation</li>
<li>FileAPI.class.php: + HTTP header Content-Type: application/json</li>
<li>#143: + `FileAPI.flashWebcamUrl` option</li>
<li>* merge v1.2.7</li>
<li>+ `FileAPI.formData: true` option</li>
</il>
### 2.0.1
<ul>
<li>+ support 'filter' prop in imageTransform</li>
</il>
### 2.0.0
<ul>
Expand Down
4 changes: 2 additions & 2 deletions README.ru.md
Expand Up @@ -747,12 +747,12 @@ FileAPI.Image(imageFile)
---

<a name="FileAPI.Image.resize"></a>
### resize(width`:Number`, height`:Number`[, type`:String`])`:FileAPI.Image`
### resize(width`:Number`, height`:Number`[, strategy`:String`])`:FileAPI.Image`
Ресайз.

* width — новая ширина
* height — новая высота
* type — enum: `min`, `max`, `preview`. По умолчанию `undefined`.
* strategy — enum: `min`, `max`, `preview`. По умолчанию `undefined`.

```js
FileAPI.Image(imageFile)
Expand Down
18 changes: 9 additions & 9 deletions dist/FileAPI.html5.js
Expand Up @@ -1832,13 +1832,13 @@
return this.set({ sx: x, sy: y, sw: w, sh: h || w });
},

resize: function (w, h, type){
if( typeof h == 'string' ){
type = h;
resize: function (w, h, strategy){
if( /min|max/.test(h) ){

This comment has been minimized.

Copy link
@Prinzhorn

Prinzhorn Nov 20, 2013

How about /(^(?:min|max)$/ ?

This comment has been minimized.

Copy link
@Mithgol

Mithgol Nov 29, 2013

…or /(?:min|max)/ at least, since non-capturing parenthes are regarded as faster than capturing.

This comment has been minimized.

Copy link
@Prinzhorn

Prinzhorn Nov 29, 2013

The first parenthesis is a typo (there's no counterpart). The point was the ^ and $ anchor, because only exact matches are relevant.

/^(?:min|max)$/

strategy = h;
h = w;
}

return this.set({ dw: w, dh: h, resize: type });
return this.set({ dw: w, dh: h || w, resize: strategy });
},

preview: function (w, h){
Expand Down Expand Up @@ -1990,10 +1990,10 @@
, dw = m.dw = m.dw || sw
, dh = m.dh = m.dh || sh
, sf = sw/sh, df = dw/dh
, type = m.resize
, strategy = m.resize
;

if( type == 'preview' ){
if( strategy == 'preview' ){
if( dw != sw || dh != sh ){
// Make preview
var w, h;
Expand All @@ -2014,12 +2014,12 @@
}
}
}
else if( type ){
else if( strategy ){
if( !(sw > dw || sh > dh) ){
dw = sw;
dh = sh;
}
else if( type == 'min' ){
else if( strategy == 'min' ){
dw = round(sf < df ? min(sw, dw) : dh*sf);
dh = round(sf < df ? dw/sf : min(sh, dh));
}
Expand Down Expand Up @@ -2099,7 +2099,7 @@
params(img, ImgTrans);
}
else if( params.width ){
ImgTrans[params.preview ? 'preview' : 'resize'](params.width, params.height, params.type);
ImgTrans[params.preview ? 'preview' : 'resize'](params.width, params.height, params.strategy);
}
else {
if( params.maxWidth && (img.width > params.maxWidth || img.height > params.maxHeight) ){
Expand Down
2 changes: 1 addition & 1 deletion dist/FileAPI.html5.min.js

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions dist/FileAPI.js
Expand Up @@ -1832,13 +1832,13 @@
return this.set({ sx: x, sy: y, sw: w, sh: h || w });
},

resize: function (w, h, type){
if( typeof h == 'string' ){
type = h;
resize: function (w, h, strategy){
if( /min|max/.test(h) ){
strategy = h;
h = w;
}

return this.set({ dw: w, dh: h, resize: type });
return this.set({ dw: w, dh: h || w, resize: strategy });
},

preview: function (w, h){
Expand Down Expand Up @@ -1990,10 +1990,10 @@
, dw = m.dw = m.dw || sw
, dh = m.dh = m.dh || sh
, sf = sw/sh, df = dw/dh
, type = m.resize
, strategy = m.resize
;

if( type == 'preview' ){
if( strategy == 'preview' ){
if( dw != sw || dh != sh ){
// Make preview
var w, h;
Expand All @@ -2014,12 +2014,12 @@
}
}
}
else if( type ){
else if( strategy ){
if( !(sw > dw || sh > dh) ){
dw = sw;
dh = sh;
}
else if( type == 'min' ){
else if( strategy == 'min' ){
dw = round(sf < df ? min(sw, dw) : dh*sf);
dh = round(sf < df ? dw/sf : min(sh, dh));
}
Expand Down Expand Up @@ -2099,7 +2099,7 @@
params(img, ImgTrans);
}
else if( params.width ){
ImgTrans[params.preview ? 'preview' : 'resize'](params.width, params.height, params.type);
ImgTrans[params.preview ? 'preview' : 'resize'](params.width, params.height, params.strategy);
}
else {
if( params.maxWidth && (img.width > params.maxWidth || img.height > params.maxHeight) ){
Expand Down
2 changes: 1 addition & 1 deletion dist/FileAPI.min.js

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions lib/FileAPI.Image.js
Expand Up @@ -67,13 +67,13 @@
return this.set({ sx: x, sy: y, sw: w, sh: h || w });
},

resize: function (w, h, type){
if( typeof h == 'string' ){
type = h;
resize: function (w, h, strategy){
if( /min|max/.test(h) ){
strategy = h;
h = w;
}

return this.set({ dw: w, dh: h, resize: type });
return this.set({ dw: w, dh: h || w, resize: strategy });
},

preview: function (w, h){
Expand Down Expand Up @@ -225,10 +225,10 @@
, dw = m.dw = m.dw || sw
, dh = m.dh = m.dh || sh
, sf = sw/sh, df = dw/dh
, type = m.resize
, strategy = m.resize
;

if( type == 'preview' ){
if( strategy == 'preview' ){
if( dw != sw || dh != sh ){
// Make preview
var w, h;
Expand All @@ -249,12 +249,12 @@
}
}
}
else if( type ){
else if( strategy ){
if( !(sw > dw || sh > dh) ){
dw = sw;
dh = sh;
}
else if( type == 'min' ){
else if( strategy == 'min' ){
dw = round(sf < df ? min(sw, dw) : dh*sf);
dh = round(sf < df ? dw/sf : min(sh, dh));
}
Expand Down Expand Up @@ -334,7 +334,7 @@
params(img, ImgTrans);
}
else if( params.width ){
ImgTrans[params.preview ? 'preview' : 'resize'](params.width, params.height, params.type);
ImgTrans[params.preview ? 'preview' : 'resize'](params.width, params.height, params.strategy);
}
else {
if( params.maxWidth && (img.width > params.maxWidth || img.height > params.maxHeight) ){
Expand Down
2 changes: 1 addition & 1 deletion statics/docs.json

Large diffs are not rendered by default.

43 changes: 35 additions & 8 deletions tests/tests.js
Expand Up @@ -382,24 +382,51 @@ module('FileAPI');



FileAPI.html5 && test('upload + imageTransform', function (){
FileAPI.html5 && test('upload + imageTransform (min, max, preview)', function (){
var file = FileAPI.getFiles(uploadForm['image.jpg'])[0];
var queue = FileAPI.queue(start);

stop();

// strategy: 'min'
queue.inc();
FileAPI.upload({
url: 'http://rubaxa.org/FileAPI/server/ctrl.php',
files: { image: file },
imageTransform: {
width: 100,
height: 100,
rotate: 'auto',
preview: true
},
imageTransform: { width: 100, height: 100, strategy: 'min' },
complete: function (err, res){
queue.next();
var res = FileAPI.parseJSON(res.responseText);
equal(res.images['image'].width, 141, 'min.width');
equal(res.images['image'].height, 100, 'min.height');
}
});

// strategy: 'max'
queue.inc();
FileAPI.upload({
url: 'http://rubaxa.org/FileAPI/server/ctrl.php',
files: { image: file },
imageTransform: { width: 100, height: 100, strategy: 'max' },
complete: function (err, res){
queue.next();
var res = FileAPI.parseJSON(res.responseText);
equal(res.images['image'].width, 100, 'max.width');
equal(res.images['image'].height, 71, 'max.height');
}
});

// preview
queue.inc();
FileAPI.upload({
url: 'http://rubaxa.org/FileAPI/server/ctrl.php',
files: { image: file },
imageTransform: { width: 100, height: 100, rotate: 'auto', preview: true },
complete: function (err, res){
var res = FileAPI.parseJSON(res.responseText);

imageEqual(res.images.image.dataURL, 'files/samples/'+browser+'-image-auto-100x100.jpeg', 'image auto 100x100.png', function (){
start();
queue.next();
});
}
});
Expand Down

0 comments on commit e7a952b

Please sign in to comment.