Skip to content

Instantly share code, notes, and snippets.

@oliverdoetsch
Created June 8, 2014 21:45
Show Gist options
  • Save oliverdoetsch/26a9dfc7bcaaa91d8324 to your computer and use it in GitHub Desktop.
Save oliverdoetsch/26a9dfc7bcaaa91d8324 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
//<![CDATA[
$(document).ready(function() {
var postWidth = $('.post-body').width();
var sAttribut = /\/s\B\d{2,4}/;
$('.post-body').find('img').each(function(n, image) {
var thisHeight = $(this).height();
var thisWidth = $(this).width();
var thisRatio = thisWidth / thisHeight;
var newWidth = postWidth;
var newHeight = Math.round(newWidth / thisRatio);
var newDimension;
if (thisWidth >= thisHeight) {
newDimension = newWidth
} else {
newDimension = newHeight
};
var image = $(image);
image.removeAttr("width height");
image.css({
"margin-left": "0",
"padding": "0",
"box-sizing": "border-box",
"box-shadow": "0 0 0 rgba(0, 0, 0, 0)",
"width": newWidth + "px",
});
image.parent().removeAttr("style");
image.attr({
src: image.attr('src').replace(sAttribut, '/' + 's' + newDimension)
});
image.attr('width', newWidth);
});
})
//]]>
< /script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment