Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option for resizing image to crop box's longest side (on image load) #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions croppic-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 100 additions & 3 deletions croppic.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
loadPicture:'',
onReset: null,
enableMousescroll: false,
fitToLongest: false,
fitToCover: false,

//callbacks
onBeforeImgUpload: null,
Expand Down Expand Up @@ -238,7 +240,7 @@

that.obj.append(img);

img.load(function(){
img.on('load', function(){
that.initCropper();
that.hideLoader();
if (that.options.onAfterImgUpload) that.options.onAfterImgUpload.call(that);
Expand Down Expand Up @@ -271,7 +273,7 @@

var img =$('<img src="'+ that.options.loadPicture +'">');
that.obj.append(img);
img.load(function() {
img.on('load', function() {
that.imgInitW = that.imgW = this.width;
that.imgInitH = that.imgH = this.height;
that.initCropper();
Expand Down Expand Up @@ -355,6 +357,101 @@
that.img.css({'left': -(that.imgW -that.objW)/2, 'top': -(that.imgH -that.objH)/2, 'position':'relative'});
if(that.options.imgEyecandy){ that.imgEyecandy.css({'left': -(that.imgW -that.objW)/2, 'top': -(that.imgH -that.objH)/2, 'position':'relative'}); }

// Resize image to fit object's longest side
var resize = function(newTop, newLeft, newWidth, newHeight) {
that.imgW = newWidth;
that.img.width(newWidth);

that.imgH = newHeight;
that.img.height(newHeight);

that.img.css({
'top': newTop,
'left': newLeft
});

if (that.options.imgEyecandy){
that.imgEyecandy.width(newWidth);
that.imgEyecandy.height(newHeight);
that.imgEyecandy.css({
'top': newTop,
'left': newLeft
});
}
};

var resizeToWidth = function() {
var ratio = (that.imgW / that.imgH);
var newWidth = that.objW;
var newHeight = (newWidth / ratio);

var newTop = that.objH - (newHeight / 2);
var newLeft = 0;

newTop = (newTop > 0 ? 0 : newTop);

resize(newTop, newLeft, newWidth, newHeight);
};

var resizeToHeight = function() {
var newHeight = that.objH;
var ratio = (newHeight / that.imgH);
var newWidth = (that.imgW * ratio);

var newTop = 0;
var newLeft = (that.objW / 2) - (newWidth / 2);

newLeft = (newLeft < 0 ? 0 : newLeft);

resize(newTop, newLeft, newWidth, newHeight);
};

var resizeToCover = function() {
var sourceWidth = that.imgW;
var sourceHeight = that.imgH;
var destWidth = that.objW;
var destHeight = that.objH;

var result = {
x: 0,
y: 0,
width: destWidth,
height: destHeight
};

var scaleX = (that.objW / that.imgW);
var scaleY = (that.objH / that.imgH);
var scale = ((scaleX > scaleY) ? scaleX : scaleY);

sourceWidth *= scale;
sourceHeight *= scale;

result.width = Math.round(sourceWidth);
result.height = Math.round(sourceHeight);

result.x = Math.round((destWidth / 2) - (sourceWidth / 2));
result.y = Math.round((destHeight / 2) - (sourceHeight / 2));

resize(
result.y,
result.x,
result.width,
result.height
);
};

if (that.options.fitToCover) {
that.options.fitToLongest = false;
resizeToCover();
}

if (that.options.fitToLongest) {
if (that.objW >= that.objH) {
resizeToWidth();
} else {
resizeToHeight();
}
}
},

createCropControls: function(){
Expand Down Expand Up @@ -677,4 +774,4 @@
that.obj.html('');
}
};
})(window, document);
})(window, document);
Loading