[jQuery] Cropping image function
My cropping image function works somehow but I've still some questions
$.fn.cropImage = function (minW, minH)
I've change the parameters to be minmal values, image won't be cropped
smaller. Yet how do I specify default values so cropImage can be
called without parameters or with just one?
if (!$(this)) return;
How do I test "$(this)" against "undefined"?
var w = $(this).attr('width');
var h = $(this).attr('height');
How can I get the original width/height instead of the actuall width/
heigth? Currently my image only shrinks but never grows.
var winW = window.innerWidth ||
self.innerWidth ||
(document.documentElement &&
document.documentElement.clientWidth) ||
document.body.clientWidth;
var winH = window.innerHeight ||
self.innerHeight ||
(document.documentElement &&
document.documentElement.clientHeight) ||
document.body.clientHeight;
Computing screen size is taken from Thickbox so I guess this must be
okay even if I don't understand every part.
var docW = document.body.scrollWidth ||
document.body.offsetWidth;
var docH = document.body.scrollHeight ||
document.body.offsetHeight;
This seems to compute the needed space but again I'm not sure if it's
okay.
var availW = windowW - (spaceW - w);
var availH = windowH - (spaceH - h);
if (((availW < w) || (availH < h)) && (width < w) && (height < h)) {
var scaleW;
var scaleH;
if (availW < w) scaleW = availW / w;
if (availH < h) scaleH = availH / h;
var scale = Math.min (scaleW, scaleH);
w = w * scale;
h = h * scale;
}
$(this).css ({width:w, height:h});
This code seems to do its job, no problem with that.
O. Wyss