1/** 2 MyImgScale v0.2 3 4 MyImgScale is a jQuery plugin to scale images to fit or fill their parent container. 5 Note: The parent container of the image must have a defined height and width in CSS. 6 7 It is actually a merger/improvement from two existing plugins: 8 1) Image Scale v1.0 by Kelly Meath (http://imgscale.kjmeath.com/), and 9 2) CJ Object Scaler v3.0.0 by Doug Jones (http://www.cjboco.com/projects.cfm/project/cj-object-scaler/) 10 11 The reasons for this merger are: 12 . CJ Object Scaler has a conciser image resizing algorithm while Image Scale has a clearer layout. 13 . CJ Object Scaler has an overflow issue, ie. the image scaled is not confined in parent container. 14 . Both have the wrong calculation when parent container is invisible. 15 16 If the parent container has padding, the scaled image might still cross boundary. 17 One of the solutions is to insert a wrapper div with the same height and width as the parent container, eg: 18 <div id="parent" style="height: 120px; width: 90px; padding: 10px"> 19 <div id="interimWrapper" style="height: 120px; width: 90px;"> 20 <img src="<Your img link here>" /> 21 </div> 22 </div> 23 I prefer to do this in application rather than in plugin as it is somewhat obtrusive. 24 25 Web: https://bitbucket.org/marshalking/myimgscale 26 Updated: Apr 15, 2011 by Marshal 27 28 ----------------------------------------------------------------------- 29 MIT License 30 31 Copyright (c) 2011 Doug Jones, Kelly Meath, Marshal 32 33 Permission is hereby granted, free of charge, to any person obtaining 34 a copy of this software and associated documentation files (the 35 "Software"), to deal in the Software without restriction, including 36 without limitation the rights to use, copy, modify, merge, publish, 37 distribute, sublicense, and/or sell copies of the Software, and to 38 permit persons to whom the Software is furnished to do so, subject to 39 the following conditions: 40 41 The above copyright notice and this permission notice shall be 42 included in all copies or substantial portions of the Software. 43 44 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 45 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 46 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 47 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 48 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 49 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 50 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 51 */ 52 53(function($) { 54 $.fn.scaleImage = function(options) { 55 var opts = $.extend({parent: false, scale: 'fill', center: true, fade: 0}, options); // merge default options with user's 56 57 return this.each(function() { 58 var $img = $(this); 59 var $parent = opts.parent ? $img.parents(opts.parent) : $img.parent(); // if not supplied, use default direct parent 60 $parent.css({opacity: 0, overflow: 'hidden'}); // keep the img inside boundaries 61 62 if ($parent.length > 0) { 63 $img.removeAttr('height').removeAttr('width'); 64 if (this.complete) { // img already loaded/cached 65 scale($img, $parent); 66 } else { 67 $img.load(function() { 68 scale($img, $parent); 69 }); 70 } 71 } 72 }); 73 74 function scale($img, $parent) { 75 var imgSize = getOriginalImgSize($img), 76 imgW = imgSize.width, 77 imgH = imgSize.height, 78 destW = $parent.width(), 79 destH = $parent.height(), 80 borderW = parseInt($img.css("borderLeftWidth"), 10) + parseInt($img.css("borderRightWidth"), 10), 81 borderH = parseInt($img.css("borderTopWidth"), 10) + parseInt($img.css("borderBottomWidth"), 10), 82 ratioX, ratioY, ratio, newWidth, newHeight; 83 84 if (destH === 0 || destW === 0) { // parent is invisible, eg. display: none 85 var parentSize = getHiddenElemSize($parent); 86 destW = parentSize.width; 87 destH = parentSize.height; 88 } 89 90 // check for valid border values. IE takes in account border size when calculating width/height so just set to 0 91 borderW = isNaN(borderW) ? 0 : borderW; 92 borderH = isNaN(borderH) ? 0 : borderH; 93 94 // calculate scale ratios 95 ratioX = destW / imgW; 96 ratioY = destH / imgH; 97 98 // Determine which algorithm to use 99 if (opts.scale === "fit") { 100 ratio = ratioX < ratioY ? ratioX : ratioY; 101 } else if (opts.scale === "fill") { 102 ratio = ratioX > ratioY ? ratioX : ratioY; 103 } 104 105 // calculate our new image dimensions 106 newWidth = parseInt(imgW * ratio, 10) - borderW; 107 newHeight = parseInt(imgH * ratio, 10) - borderH; 108 109 // Set new dimensions to both css and img's attributes 110 $img.css({ 111 "width": newWidth, 112 "height": newHeight 113 }).attr({ 114 "width": newWidth, 115 "height": newHeight 116 }); 117 118 if (opts.center) { // set offset if center is true 119 $img.css("margin-left", Math.floor((destW - newWidth) / 2)); 120 $img.css("margin-top", Math.floor((destH - newHeight) / 2)); 121 } 122 123 if (opts.fade > 0) { // fade-in effect 124 $parent.animate({opacity: 1}, opts.fade); 125 } else { 126 $parent.css("opacity", 1); 127 } 128 } 129 130 /** 131 * To calculate the correct scale ratio, we need the image's original size rather than some preset values, 132 * which were set either manually in code or automatically by browser. 133 * Thanks FDisk for the solution: 134 * http://stackoverflow.com/questions/318630/get-real-image-width-and-height-with-javascript-in-safari-chrome 135 */ 136 function getOriginalImgSize($img) { 137 var t = new Image(); 138 t.src = $img.attr("src"); 139 return {width: t.width, height: t.height}; 140 } 141 142 /** 143 * If the element is invisible, jQeury .height() and .width() return 0 in IE. 144 * This function returns the hidden element's correct width and height. 145 * Thanks elliotlarson for the solution: 146 * http://stackoverflow.com/questions/2345784/jquery-get-height-of-hidden-element-in-jquery-1-4-2 147 */ 148 function getHiddenElemSize(element) { 149 var copy = element.clone().css({visibility: 'hidden', display: 'block', position: 'absolute'}); 150 $("body").append(copy); 151 var size = {width: copy.width(), height: copy.height()}; 152 copy.remove(); 153 return size; 154 } 155 }; 156})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.