Skip to content

Instantly share code, notes, and snippets.

@ncr
Created January 11, 2010 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncr/274699 to your computer and use it in GitHub Desktop.
Save ncr/274699 to your computer and use it in GitHub Desktop.
/*
Author: Jacek Becela (http://github.com/ncr)
License: MIT
$("img").sqrop() // square crop with side length equal to shorter side of image
$("img").sqrop(200) // square crop with 200px side length, image scaled accordingly
Inspiration: http://www.seifi.org/css/creating-thumbnails-using-the-css-clip-property.html
*/
$.fn.sqrop = function(l){
return this.each(function(){
var e = $(this),
width = e.width(),
height = e.height(),
min = Math.min(width, height),
max = Math.max(width, height),
length = l || min,
ratio = length / min;
newWidth = width * ratio,
newHeight = height * ratio,
deltaX = (newWidth - length) / 2,
deltaY = (newHeight - length) / 2,
outer = $("<span />").css({
position: "relative",
width: length,
height: length,
display: "inline-block"
}),
inner = $("<span />").css({
position: "absolute",
clip: "rect(" + deltaY + "px " + (length + deltaX) + "px " + (length + deltaY) + "px " + deltaX + "px)",
top: -deltaY,
left: -deltaX
});
e.css({width: newWidth, height: newHeight}).wrap(inner).parent().wrap(outer);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment