HackingUniversity - Hacks . Tricks . How-To's

13 December 2012

Automatic Post Image Resizing to 100% in Blogger Blogs

Post design is always the major key to great readership, how you add text, images and videos to your posts really matters, their size, colors and everything depends on your creativity. So the one thing you can do is to get your images in good quality so that when ever a person comes to read your posts he should be satisfies with you post design so I have posted on how you can make your image fit to your blog’s width using CSS, so using this css trick you can automatically make every image fir in your blog’s width so if the image is too large than your blog’s width it will be still kept in the boundary while maintaining its overall dimensions.

But many people requested me that if I can provide them with some script that can automatically make every image fit to blog width even if its small width and height, well I just found a script that can do exactly what you are seeking out. With this script added to your blog every latest and older posts images will get 100% blogs width that means automatic image resizing to the edge, so if you have some smaller images then they will also get expanded to the 100% + every image will get caption in their footer with the alt text you will add in them.

  1. Open Blogger > Template > Edit HTML
  2. Now press Ctrl + F and search for </head> and paste below code above it.

    <script src='http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' type='text/javascript'/>
    <script type='text/javascript'>//<![CDATA[
         var areawidth = $('#Blog1').width();

        //$('.sidebar').find('.clear').each(function(n, layer){   
             //var layer = $(layer);
             //if (layer.css('clear') == "both") layer.css('clear','none');
         $('#Blog1').find('div').each(function(n, layer){   
             var layer = $(layer);
             if (layer.css('clear') == "both") layer.css('clear','none');

        $('.post-body').find('img').each(function(n, image){
             var image = $(image);
             alt = image.attr('alt');
             id = image.index();
             imageAltId = 'imagealt'+n;
             var originalWidth = image.width();
             var originalHeight = image.height();
             image.attr({src : image.attr('src').replace(/s\B\d{3,4}/,'s' + 1200)});
             //var newHeight = (originalHeight/originalWidth * areawidth).toFixed(0);
             if (alt) {
                 image.after($('<div>').css('padding','5px 5px 8px 5px').css('background-color','#ffffff').css('opacity','0.6').css('filter','alpha(opacity=60)').attr('id',imageAltId).text(alt));
                 var divHeight = $('#'+imageAltId).height() + 13;


    // the following values are in milliseconds
    var speedOut     = 2200;
    var speedIn        = 2200;
    var pauseShow    = 6000;

    function goSlideshow(imageDestination, captionDestination, picasaEmail, picasaAlbum, picasaResults, picasaDimension){
         var url = 'http://picasaweb.google.com/data/feed/api/user/' + picasaEmail + '/album/' + picasaAlbum + '?kind=photo&imgmax=' + picasaDimension + '&max-results=' + picasaResults + '&alt=json';
         ($.getJSON(url, 'callback=?')).success(function(data){
             var slides     = new Array();
             var captions     = new Array();
             var heights    = new Array();
             var widths    = new Array();
             $(data.feed.entry).each(function(i, entry){
                 slides[i]     = entry.content.src;
                 captions[i]    = entry.summary.$t;
                 heights[i]    = entry.gphoto$height.$t;
                 widths[i]    = entry.gphoto$width.$t;

                 var urlValue = 'url(' + slides[0] + ')';
                 if (heights[0] > widths[0]){
                     $(imageDestination).css('background-size','auto 100%');
                 } else $(imageDestination).css('background-size','100% auto');
                 var counter = 1;

                     if (counter >= slides.length) counter = 0;
                             var urlValue = 'url(' + slides[counter] + ')';
                             if (heights[counter] > widths[counter]){
                                 $(imageDestination).css('background-size','auto 100%');
                             } else $(imageDestination).css('background-size','100% auto');
             console.log('error getting feed data');

  3. Now if you have the blue line stating the jquery already in your blog than there is no need to add it again so you can remove that line and then Save the Template.
  4. Now refresh your blog and see that every image in your blog posts will be resized to full blog’s width even if it is small in size.

Remember Using this script on your blogger blog can make your image blurry as resizing them looses their overall quality but still if you need to resize every image in your blog to 100% that this is the ultimate solution.