Resize and crop images on the fly, while preserving their aspect ratio. A placeholder image is used initially; the actual image loading, cropping and resizing happens in the background. Optimized for rendering hundreds of images on the same page without blocking the browser event loop; images are loaded in batches with a pause in between each batch. This maximizes UI responsiveness while ensuring image loading is not blocking AJAX calls.
bower install jquery-resizeandcrop
Include jquery.resizeandcrop.css
and jquery.resizeandcrop.js
files on the page and call the plugin on any image:
<html>
<head>
<link rel="stylesheet" type="text/css" href="jquery.resizeandcrop.css">
</head>
<body>
<img class="crop-me" src="placeholder.png" realsrc="imagetoloadandcrop.png" />
<script type="text/javascript" src="jquery.resizeandcrop.js"></script>
<script>
$("img.crop-me").resizeAndCrop();
</script>
</body>
</html>
In the above example, the placeholder.png
image will display, while the image in the realsrc
attribute of the <img>
tag gets loaded, resized, and cropped. The image will be resized to match the size of placeholder.png
by default, but
you can override this by specifying the size in the <img>
tag via a CSS class, in-line CSS style, or width
/ height
attributes:
<img src="placeholder.png" realsrc="imagetoloadandcrop.png" style="width: 200px; height: 200px;">
By default, if the original image is smaller than the placeholder, it won't get resized. You can change this by setting
forceResize
to true
. The full list of options are below.
Several examples of using the plugin can be found in the examples/index.html
file.
The plugin sets options via a key-value object parameter:
$('img.cropme').resizeAndCrop( { center: false, imgClass: "foo" } );
Available Options:
Copyright (c) 2013 Traackr Licensed under the MIT license.