working on it ...

Filters

Explore Public Snippets

Sort by

Found 669 snippets matching: scaling

    public by pplant modified Jul 25, 2014  2884  2  6  2

    Scale Image to fit dimensions

    This snippet of code allows you to scale a image proportional such that it fits the provided dimensions.
    public static ImageIcon getScaledImage(ImageIcon srcImg, int w, int h){
    		int imgWidth = srcImg.getIconWidth();
    		int imgHeight = srcImg.getIconHeight();
    		
    		//Find the right width
    		if(imgWidth > w){
    			float sWidth = imgWidth-w;
    			float sFactor = (sWidth/imgWidth);
    			imgWidth = w;
    			imgHeight = (int) (imgHeight-(imgHeight*sFactor));
    		}
    		
    		//Find the right hight 
    		if(imgHeight > h){
    			float sHeight = imgHeight-h;
    			float sFactor = sHeight/imgHeight;
    			imgHeight = h;
    			imgWidth = (int) (imgWidth-(imgWidth*sFactor));
    		}
    		
    		BufferedImage resizedImg = new BufferedImage(imgWidth, imgHeight, BufferedImage.TYPE_INT_ARGB);
    		Graphics2D g2 = resizedImg.createGraphics();
    		g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    		g2.drawImage(srcImg.getImage(), 0, 0, imgWidth, imgHeight, null);
    		g2.dispose();
    		return new ImageIcon(resizedImg);
    	}

    public by wolke modified Apr 10, 2014  1896  1  6  1

    html-Image like scaling fullsize background-image

    /* http://css-tricks.com/perfect-full-page-background-image/
    Works in:
        Any version of good browsers: Safari / Chrome / Opera / Firefox
        IE 6: Borked - but probably fixable if you use some kind of fixed positioning shim
        IE 7/8: Mostly works, doesn't center at small sizes but fills screen fine
        IE 9: Works
    */
    
    img.body-bg-img {
    	/* Set rules to fill background */
    	min-height: 100%;
    	min-width: 1024px;
    
    	/* Set up proportionate scaling */
    	width: 100%;
    	height: auto;
    
    	/* Set up positioning */
    	position: fixed;
    	top: 0;
    	left: 0;
    }
    
    /* Specific to this particular image */
    @media screen and (max-width: 1024px) { 
    	img.body-bg-img {
    		left: 50%;
    		margin-left: -512px;   /* 50% */
    	}
    }
    
    

    public by msdn modified Jan 12, 2015  1101  0  7  0

    CreateNewBitmapFrom: Creates a copy of the source image by scaling it with the specified scale value.

    Creates a copy of the source image by scaling it with the specified scale value. The source image. The scaling factor to use when generating the target image. The new Bitmap.
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    
    /// <summary>Creates a copy of the source image by scaling it with the specified scale value.</summary>
    /// <param name="source">The source image.</param>
    /// <param name="scalingFactor">The scaling factor to use when generating the target image.</param>
    /// <returns>The new Bitmap.</returns>
    public static Bitmap CreateNewBitmapFrom(Image source, float scalingFactor)
    {
        return CreateNewBitmapFrom(source, (int)(source.Width*scalingFactor), (int)(source.Height*scalingFactor));
    }

    public by msdn modified Jan 12, 2015  1807  0  7  0

    PadBorderPixels

    // To avoid filtering artifacts when scaling or rotating fonts that do not use premultiplied alpha, // make sure the one pixel border around each glyph contains the same RGB values as the edge of the // glyph itself, but with zero alpha. This processing is an elaborate no-op when using premultiplied // alpha, because the premultiply conversion w
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.Runtime.InteropServices;
    
    // To avoid filtering artifacts when scaling or rotating fonts that do not use premultiplied alpha,
    // make sure the one pixel border around each glyph contains the same RGB values as the edge of the
    // glyph itself, but with zero alpha. This processing is an elaborate no-op when using premultiplied
    // alpha, because the premultiply conversion will change the RGB of all such zero alpha pixels to black.
    public static void PadBorderPixels(Bitmap bitmap, Rectangle region)
    {
        using (var bitmapData = new PixelAccessor(bitmap, ImageLockMode.ReadWrite))
        {
            // Pad the top and bottom.
            for (int x = region.Left; x < region.Right; x++)
            {
                CopyBorderPixel(bitmapData, x, region.Top, x, region.Top - 1);
                CopyBorderPixel(bitmapData, x, region.Bottom - 1, x, region.Bottom);
            }
    
            // Pad the left and right.
            for (int y = region.Top; y < region.Bottom; y++)
            {
                CopyBorderPixel(bitmapData, region.Left, y, region.Left - 1, y);
                CopyBorderPixel(bitmapData, region.Right - 1, y, region.Right, y);
            }
    
            // Pad the four corners.
            CopyBorderPixel(bitmapData, region.Left, region.Top, region.Left - 1, region.Top - 1);
            CopyBorderPixel(bitmapData, region.Right - 1, region.Top, region.Right, region.Top - 1);
            CopyBorderPixel(bitmapData, region.Left, region.Bottom - 1, region.Left - 1, region.Bottom);
            CopyBorderPixel(bitmapData, region.Right - 1, region.Bottom - 1, region.Right, region.Bottom);
        }
    }

    external by tombennet modified Mar 25, 2015  26465  0  3  0

    Preserve pixelation when scaling pixel art with CSS

    Preserve pixelation when scaling pixel art with CSS: preserve-pixelation.scss
    @mixin pixelated {
      -ms-interpolation-mode: nearest-neighbor;  // IE 7+ (non-standard property)
      image-rendering: -webkit-optimize-contrast; // Safari 6, UC Browser 9.9
      image-rendering: -webkit-crisp-edges; // Safari 7+
      image-rendering: -moz-crisp-edges; // Firefox 3.6+
      image-rendering: -o-crisp-edges; // Opera 12
      image-rendering: pixelated; // Chrome 41+ and Opera 26+
    }
    
    

    external by patwonder modified Jun 1, 2014  194  1  2  0

    百度贴吧图片缩放增强脚本 Baidu Tieba Image Scaling Enhancer

    百度贴吧图片缩放增强脚本 Baidu Tieba Image Scaling Enhancer: baidu-tieba-image-scaling-enhancer.user.js
    // ==UserScript==
    // @id             tieba.baidu.com-709c0fe7-e313-44bd-9dbd-752bbd80259d@patwonder@163.com
    // @name           百度贴吧图片缩放增强脚本
    // @version        0.63
    // @namespace      patwonder@163.com
    // @author         patwonder
    // @description    增强百度贴吧图片缩放,看大图无需开新标签页。
    // @updateURL      https://gist.githubusercontent.com/patwonder/99835e0cece2ac9928ad/raw/baidu-tieba-image-scaling-enhancer.user.js
    // @downloadURL    https://gist.githubusercontent.com/patwonder/99835e0cece2ac9928ad/raw/baidu-tieba-image-scaling-enhancer.user.js
    // @include        http://tieba.baidu.com/club/*/p/*
    // @include        http://tieba.baidu.com/f?kz=*
    // @include        http://tieba.baidu.com/p/*
    // @include        http://tieba.baidu.com/f?*ct=*z=*
    // @include        http://tieba.baidu.com.cn/f?kz=*
    // @include        http://tieba.baidu.com.cn/p/*
    // @include        http://tieba.baidu.com.cn/f?*ct=*z=*
    // @include        http://post.baidu.com/f?kz=*
    // @include        http://post.baidu.com/p/*
    // @include        http://post.baidu.com/f?*ct=*z=*
    // @include        http://post.baidu.com.cn/f?kz=*
    // @include        http://post.baidu.com.cn/p/*
    // @include        http://post.baidu.com.cn/f?*ct=*z=*
    // @run-at         document-end
    // @grant          none
    // ==/UserScript==
    
    (function(d, w) {
    var B_WIDESCREEN_ENABLED = true;
    var B_FLOORNUM_ENABLED = true;
    var B_SWITCH_ENABLED = true;
    
    var btise = { wideScreenEnabled : true, floorNumEnabled : true };
    var localStorage = w.localStorage;
    if (localStorage) {
      localStorage = localStorage.wrappedJSObject || localStorage;
      if (typeof(localStorage.btise) == 'string') {
        try {
          btise = JSON.parse(localStorage.btise);
        } catch (ex) { }
      }
    
      // Assign default configs
      if (typeof(btise.wideScreenEnabled) == 'undefined' && typeof(btise.floorNumEnabled) == 'undefined') {
        btise.wideScreenEnabled = B_WIDESCREEN_ENABLED;
        btise.floorNumEnabled = B_FLOORNUM_ENABLED;
      }
      if (typeof(localStorage.btise) != 'string')
        localStorage.btise = JSON.stringify(btise);
    } else {
      B_SWITCH_ENABLED = false;
    }
    
    B_WIDESCREEN_ENABLED = !!btise.wideScreenEnabled;
    B_FLOORNUM_ENABLED = !!btise.floorNumEnabled;
     
    var STR_SCRIPT_NAME = '百度贴吧图片缩放增强脚本';
    
    var common = {
        matchesSelector: function(element, selector) {
            if (element.mozMatchesSelector) {
                return element.mozMatchesSelector(selector);
            } else if (element.webkitMatchesSelector) {
                return element.webkitMatchesSelector(selector);
            } else if (element.matchesSelector) {
                return element.matchesSelector(selector);
            } else {
                try {
                    var elems = element.parentElement ? element.parentElement.querySelectorAll(selector) : [];
                    for (var i = 0, l = elems.length; i < l; i++) {
                        if (elems[i] === element) return true;
                    }
                } catch (ex) { }
                return false;
            }
        },
        // re-entrance guard for image wrapping, avoid re-entering the DOM mutation event handlers
        wrapping: false
    };
    
    (function() {
        var IMG_RETRIEVING_SIZE = '正在获取原始大小……';
        var IMG_ORG_SIZE_DESC = '原图:';
        var IMG_CUR_PERCENT = "比例:";
        var IMG_MAG_DESC = '点击放大,按住Shift全部放大';
        var IMG_MIN_DESC = '点击缩小,按住Shift全部缩小';
        var SIGN_SELECTOR = 'div.d_sign_split + img';
        var IMG_INSIDE_EDITOR_SELECTOR = '#editor img, #tb_rich_poster img';
        var IMG_SELECTOR = 'img.BDE_Image, div.p_content img.BDE_Smiley, img.d_content_img, ' + SIGN_SELECTOR;
        var REG_SIGN = /w%3D580.*\/sign=.*?(?=\/)/;
        var images = [];
        
        var matchesSelector = common.matchesSelector;
        
        var prefilterImages = function() {
            var imageNodes = d.querySelectorAll(IMG_SELECTOR);
            for (var i = 0; i < imageNodes.length; i++) {
                var image = imageNodes[i];
                if (shouldAdd(image))
                    prefilterImage(image);
            }
        }
        
        var prefilterImage = function(image) {
            // Check whether we should reload with original src
            if (REG_SIGN.test(image.src)) {
                var newimg = d.createElement('img');
                newimg.src = image.src.replace(REG_SIGN, "pic/item");
                newimg.className = image.className;
                if (image.parentElement) {
                    common.wrapping = true;
                    image.parentElement.insertBefore(newimg, image);
                    image.parentElement.removeChild(image);
                    common.wrapping = false;
                    return newimg;
                }
            }
            // Check for passively loaded images
            if (image.hasAttribute("data-passive")) {
                var passiveSrc = image.getAttribute("data-passive");
                image.setAttribute("data-passive", passiveSrc.replace(REG_SIGN, "pic/item"));
            } else if (image.hasAttribute("data-tb-lazyload")) {
                var passiveSrc = image.getAttribute("data-tb-lazyload");
                image.setAttribute("data-tb-lazyload", passiveSrc.replace(REG_SIGN, "pic/item"));
            }
            return image;
        }
        
        var obtainImages = function() {
            var imageNodes = d.querySelectorAll(IMG_SELECTOR);
            for (var i = 0; i < imageNodes.length; i++) {
                var image = imageNodes[i];
                if (shouldAdd(image))
                    images.push(image);
            }
        };
        var shouldAdd = function(image) {
            // don't process images inside the editor
            if (matchesSelector(image, IMG_INSIDE_EDITOR_SELECTOR))
                return false;
            return true;
        };
        var addImages = function(new_images) {
            for (var i = 0; i < new_images.length; i++) {
                var image = new_images[i];
                if (!shouldAdd(image)) continue;
                image = prefilterImage(image);
                
                images.push(image);
                initImage(image);
                adjustScaling(image);
            }
        };
        var removeImages = function(del_images) {
            for (var i = 0; i < del_images.length; i++) {
                var image = del_images[i];
                for (var j = 0; j < images.length; j++) {
                    if (images[j] == image) {
                        images.splice(j, 1);
                        break;
                    }
                }
            }
        };
        w.addEventListener('DOMNodeInserted', function(event) {
            if (common.wrapping) return;
            if (matchesSelector(event.target, IMG_SELECTOR)) {
                addImages([event.target]);
            } else if (event.target.querySelectorAll) {
                var new_images = event.target.querySelectorAll(IMG_SELECTOR);
                if (new_images.length != 0) {
                    addImages(new_images);
                }
            }
        }, false);
        w.addEventListener('DOMNodeRemoved', function(event) {
            if (common.wrapping) return;
            if (matchesSelector(event.target, IMG_SELECTOR)) {
                removeImages([event.target]);
            } else if (event.target.querySelectorAll) {
                var del_images = event.target.querySelectorAll(IMG_SELECTOR);
                if (del_images.length != 0) {
                    removeImages(del_images);
                }
            }
        }, false);
        // Stops everything from bubbling up and doing anything else
        var stopListener = function(e) {
          if (e.stopImmediatePropagation) e.stopImmediatePropagation();
          else if (e.stopPropagation) e.stopPropagation();
          
          if (e.preventDefault) e.preventDefault();
          
          return false;
        };
        // Image click handler that handles image size switching
        var listener = function(e) {
            // Only handle left clicks
            if (e.button != 0) return;
            
            var image = (e && e.target) || (w.event && w.event.srcElement);
            // We are expecting clicks on detected images
            if (!image || image.localName != 'img' || image.getAttribute('data-detected') != 'true' || !shouldAdd(image))
              return;
            
            if (image && image.getAttribute('data-disabled') != 'true') {
                if (image.getAttribute('data-fullsized') == 'true') {
                    image.setAttribute('data-fullsized', 'false');
                    setTitle(image);
                    if (e.shiftKey) {
                        var isSign = matchesSelector(image, SIGN_SELECTOR);
                        for (var i = 0; i < images.length; i++) {
                            var img = images[i];
                            // separate scale all images for sign and non-sign images
                            if (isSign != matchesSelector(img, SIGN_SELECTOR))
                                continue;
                            if (img.getAttribute('data-fullsized') == 'true') {
                                img.setAttribute('data-fullsized', 'false');
                                setTitle(img);
                            }
                        }
                    }
                } else {
                    image.setAttribute('data-fullsized', 'true');
                    setTitle(image);
                    if (e.shiftKey) {
                        var isSign = matchesSelector(image, SIGN_SELECTOR);
                        for (var i = 0; i < images.length; i++) {
                            var img = images[i];
                            // separate scale all images for sign and non-sign images
                            if (isSign != matchesSelector(img, SIGN_SELECTOR))
                                continue;
                            if (img.getAttribute('data-fullsized') != 'true') {
                                img.setAttribute('data-fullsized', 'true');
                                setTitle(img);
                            }
                        }
                    }
                }
                if (e.shiftKey) image.scrollIntoView();
            }
            return stopListener(e);
        };
        
        var isDisplayingFullsize = function(image, callback) {
            if (image.hasAttribute('data-owidth')) {
                var owidth = image.getAttribute('data-owidth');
                callback(image.parentElement.offsetWidth >= owidth);
                return;
            }
            
            var newImg = new Image();
    
            newImg.onload = function() {
                var owidth = newImg.width;
                image.setAttribute('data-owidth', owidth);
                image.setAttribute('data-oheight', newImg.height);
                // image may have detached from the document, thus visiting "parentElement" may fail
                if (image.parentElement && image.parentElement.offsetWidth)
                  callback(image.parentElement.offsetWidth >= owidth);
            };
            
            var passiveSrc = image.getAttribute('data-passive') || image.getAttribute('data-tb-lazyload');
            newImg.src = (passiveSrc != "loaded" && passiveSrc) || image.src;
        };
    
        var adjustScaling = function(image) {
            isDisplayingFullsize(image, function(isFullsize) {
                if (isFullsize) {
                    if (image.getAttribute('data-disabled') != 'true') {
                        image.setAttribute('data-disabled', 'true');
                    }
                } else {
                    if (image.getAttribute('data-disabled') == 'true') {
                        image.removeAttribute('data-disabled');
                    }
                }
                setTitle(image);
            });
        };
    
        var initImage = function(image) {
            image.removeAttribute('width');
            image.removeAttribute('height');
            image.setAttribute('data-detected', 'true');
            setTitle(image);
            image.onclick = undefined;
            
            image.addEventListener("load", function() {
                var passiveSrc = image.getAttribute('data-passive') || image.getAttribute('data-tb-lazyload');
                if (passiveSrc && passiveSrc != "loaded")
                  return;
                var width = image.naturalWidth;
                var height = image.naturalHeight;
                image.setAttribute('data-owidth', width);
                image.setAttribute('data-oheight', height);
                adjustScaling(image);
            }, false);
            
            if (matchesSelector(image, SIGN_SELECTOR)) {
                // wrap sign images into a fixed size container
                common.wrapping = true;
                try {
                    var divWrapper = d.createElement('div');
                    divWrapper.className = 'd_sign_wrapper';
                    image.parentElement.insertBefore(divWrapper, image.previousSibling);
                    divWrapper.appendChild(image.previousSibling);
                    divWrapper.appendChild(image);
                } finally {
                    common.wrapping = false;
                }
            }
        };
    
        var setTitle = function(image) {
            var title_a = [];
            if (image.getAttribute('data-disabled') != 'true') {
                var isFullsized = image.getAttribute('data-fullsized') == 'true';
                if (image.hasAttribute('data-owidth')) {
                    var owidth = image.getAttribute('data-owidth')
                    title_a = [IMG_ORG_SIZE_DESC, owidth, '*', image.getAttribute('data-oheight'), '  ',
                    IMG_CUR_PERCENT, isFullsized ? 100 : Math.floor(100 * image.parentElement.offsetWidth / owidth), '%\n'];
                } else {
                    title_a = [IMG_RETRIEVING_SIZE, '\n'];
                }
                title_a.push(isFullsized ? IMG_MIN_DESC : IMG_MAG_DESC);
            }
            image.setAttribute('title', title_a.join(''));
        };
    
        var doImageOpAll = function(op) {
            for (var i = 0; i < images.length; i++) {
                op(images[i]);
            }
        };
        
        var adjustScalingAll = function() { doImageOpAll(adjustScaling); };
        var initImageAll = function() { doImageOpAll(initImage); };
    
        prefilterImages();
        obtainImages();
        initImageAll();
        w.addEventListener('resize', adjustScalingAll, false);
    
        var loadListener = function() {
            for (var i = 0; i < images.length; i++) {
                var image = images[i];
                adjustScaling(image);
                image.onclick = undefined;
            }
        };
        w.addEventListener('DOMContentLoaded', loadListener, false);
        w.addEventListener('load', loadListener, false);
        w.addEventListener('click', listener, true);
        
    		var rightPanelWidth = (function(rightSection) {
            return rightSection ? rightSection.offsetWidth : 0;
        })(d.querySelector('div.right_section'));
        
        var style = d.createElement('style');
        style.setAttribute('type','text/css');
        
        var aInnerHTML = ['img[data-detected=true] { max-width: 100% !important; margin-top: 0 !important; width: auto !important; height: auto !important; cursor: url("data:image/gif;base64,R0lGODlhIAAgAKEAAP///wAAAP///////yH5BAEAAAIALAAAAAAgACAAAAJMlBUZx+2PApggwesk3Qt7XYGdB4EhR5aToqzpo7GJ+zbmTI31IYp7tkH9bDfFMGOM6I4wC/OSfDaXUl71is1qt9yu9wsOi8fkstlQAAA7") 6 6, pointer !important; }\n',
                          'img[data-detected=true][data-fullsized=true]:not([data-disabled=true]) { max-width: none !important; cursor: url("data:image/gif;base64,R0lGODlhIAAgAKEAAP///wAAAP///////yH5BAEAAAIALAAAAAAgACAAAAJLlBUZx+2PApggwesk3Qt7vU2dB4GhSJaikqBptrLuy5jnSB82hefGPvPpTCxhRvGzGDHI5aXpfECjjR71is1qt9yu9wsOi8fk8rgAADs=") 6 6, pointer !important; z-index: 9999999; position: relative; }\n',
                          'img[data-detected=true][data-disabled=true]  { cursor: default !important; }\n',
                          'div.replace_div { width: auto !important; height: auto !important; overflow: visible !important; position: static !important; border: none !important; }\n',
                          'div.replace_tip { display: none !important; }\n',
                          'div.d_sign_wrapper { max-width: 568px; margin: 0 5px; }\n',
                          'div.d_sign_split, div.d_sign_split + img { margin-left: 0 !important; margin-right: 0 !important; }\n',
                          '#pic_to_album_tip { display: none !important; }\n',
                          'div.d_post_content_main, div.d_post_content { overflow: visible !important; }\n',
                          'div.j_lzl_container img.BDE_Smiley { max-width: 30px !important; max-height: 30px !important; }\n',
                          'ul.nav_right a { height: 36px; float: right; color: #5C6573; margin: 5px 5px; padding: 10px 5px; }\n',
                          'div.p_content.p_content_nameplate > img[width="1"][height="1"] { display: none !important; }'];
        if (B_WIDESCREEN_ENABLED) {
            aInnerHTML = aInnerHTML.concat(['\n',
                '#container, div.content, #tb_nav, div.p_thread, #pb_content, div.left_section, #j_core_title_wrap, div.l_post, div.d_post_content_main, div.core_reply_wrapper, div.pb_footer, div.d_sign_split, blockquote.d_quote, blockquote.d_quote fieldset, .d_quote .quote_content, div.core { width: auto !important; }\n',
                'div.pb_content { background: none !important; }\n',
                'div.d_post_content_main { padding: 15px 10px 5px !important; }\n',
                'div.p_content { padding: 0 !important; }\n',
                'div.left_section {\n',
                '    -moz-box-sizing: border-box;\n',
                '    -webkit-box-sizing: border-box;\n',
                '    box-sizing: border-box;\n',
                '    width: -moz-calc(100% - ', rightPanelWidth, 'px) !important;\n',
                '    width: -webkit-calc(100% - ', rightPanelWidth, 'px) !important;\n',
                '    width: calc(100% - ', rightPanelWidth, 'px) !important;\n',
                '    border-right: 1px solid #E5E5E5;\n',
                '}\n',
                'div.l_post {\n',
                '    background-color: white;\n',
                '    background-image:    -moz-linear-gradient(left, #F7F7F7, #F7F7F7 130px, white 130px, white);\n',
                '    background-image: -webkit-linear-gradient(left, #F7F7F7, #F7F7F7 130px, white 130px, white);\n',
                '    background-image:     -ms-linear-gradient(left, #F7F7F7, #F7F7F7 130px, white 130px, white);\n',
                '    background-image:      -o-linear-gradient(left, #F7F7F7, #F7F7F7 130px, white 130px, white);\n',
                '    background-image:         linear-gradient(to right, #F7F7F7, #F7F7F7 130px, white 130px, white);\n',
                '}\n',
                'div.core_reply_tail, div.core_reply_wrapper { margin-right: 0px !important; }\n',
                'div.core_reply, div.d_content { margin-left: 0px !important; max-width: 568px; margin-right: auto !important; }\n',
                '#tb_rich_poster { margin-left: 0px !important; }\n',
                '#tb_rich_poster_container { margin-left: 10px !important; }\n',
                'div.d_main_section, div.d_post_content_main, div.d_content, div.core_reply, h1.core_title_txt { float: none !important; }\n',
                'div.d_post_content_main { margin-left: 130px !important; }\n',
                'div.right_section { margin-left: 0 !important; }\n',
                'h1.core_title_txt { display: inline; }\n',
                '.card_top_theme2 { margin-right: 0 !important; }'
            ])
        }
        style.innerHTML = aInnerHTML.join('');
        d.querySelector('head').appendChild(style);
    
        adjustScalingAll();
        w.setTimeout(adjustScalingAll, 1000);
    })();
    
    (function() {
        if (!B_FLOORNUM_ENABLED) return;
        
        var POST_SELECTOR = 'div.l_post';
        var LZL_POST_SELECTOR = 'span.lzl_time';
        
        var pint = function(str) {
            var num = parseInt(str, 10);
            return (isNaN(num) || !isFinite(num)) ? 0 : num;
        };
        
        var getPostTimeSummary = function(postTimeString) {
            // calculate diff between post time and now
            var postTimeMillis = 0;
            if (postTimeString) {
                var match = /^\s*([0-9]{2,4})[\/-]([0-9]{1,2})[\/-]([0-9]{1,2})[ tT\,]*([0-9]{1,2})\:([0-9]{1,2})(?:\:([0-9]{1,2}))?\s*$/.exec(postTimeString);
                if (match) {
                    postTimeMillis = new Date(pint(match[1]), pint(match[2]) - 1, pint(match[3]), pint(match[4]), pint(match[5]), pint(match[6])).getTime();
                }
            }
            var postTimeSummary = '';
            if (postTimeMillis) {
                //alert(postTimeMillis + " ~ " + Date.now());
                var mins = Math.floor((Date.now() - postTimeMillis) / 60000);
                if (mins < 1)
                    postTimeSummary = '刚刚';
                else if (mins < 60)
                    postTimeSummary = mins + '分钟前';
                else {
                    var hours = Math.floor(mins / 60);
                    if (hours < 24)
                        postTimeSummary = hours + '小时前';
                    else {
                        var days = Math.floor(hours / 24);
                        if (days < 31)
                            postTimeSummary = days + '天前';
                        else {
                            var months = Math.floor(days / 30.436875);
                            if (months < 12)
                                postTimeSummary = months + '个月前';
                            else {
                                var years = Math.floor(months / 12);
                                months -= years * 12;
                                postTimeSummary = years + '年' + ((months > 0 && years < 3) ? months + '个月' : '') + '前';
                            }
                        }
                    }
                }
                postTimeSummary += ' ';
            }
            return postTimeSummary;
        };
    
        var style = d.createElement('style');
        style.setAttribute('type','text/css');
        style.innerHTML = ['div.d_floor { font-family: sans-serif; font-size: 16px; float: right; margin: 5px; color: #261CDC; }\n',
                           'span.s_lzl_time_summary { color: rgb(153, 153, 153); }\n',
                           'div.louzhubiaoshi { right: auto !important; left: 0px; top: 0px !important; transform: rotate(-90deg); -moz-transform: rotate(-90deg); -webkit-transform: rotate(-90deg); }\n',
                           'div.louzhubiaoshi_wrap { position: static !important; }\n',
                           'div.l_post { position: relative; }'
                          ].join('');
        d.querySelector('head').appendChild(style);
        
        var updatePost = function(post) {
            var field = JSON.parse(post.getAttribute('data-field'));
            var floorNum = field && (field.content && (field.content.floor || field.content.post_no));
            if (floorNum && !isNaN(parseFloat(floorNum)) && isFinite(floorNum)) {
                var div = post.querySelector('div.d_floor') || d.createElement('div');
                div.className = 'd_floor';
                div.innerHTML = getPostTimeSummary(field.content.date) + '#' + floorNum;
                common.wrapping = true;
                post.insertBefore(div, post.querySelector('*'));
                common.wrapping = false;
            }
        };
        var updateLzlPost = function(lzlpost) {
            var postTimeSummary = getPostTimeSummary(lzlpost.innerHTML);
            var span = lzlpost.parentElement.querySelector('span.s_lzl_time_summary') || d.createElement('span');
            span.className = 's_lzl_time_summary';
            span.innerHTML = postTimeSummary;
            common.wrapping = true;
            lzlpost.parentElement.insertBefore(span, lzlpost);
            common.wrapping = false;
        };
        var updateFloorNumField = function() {
            var posts = d.querySelectorAll(POST_SELECTOR);
            for (var i = 0; i < posts.length; i++)
                updatePost(posts[i]);
            
            var lzlposts = d.querySelectorAll(LZL_POST_SELECTOR);
            for (var i = 0; i < lzlposts.length; i++)
                updateLzlPost(lzlposts[i]);
        };
        
        updateFloorNumField();
        setInterval(updateFloorNumField, 30000);
        
        // update time summary when elements are dynamically inserted
        var matchesSelector = common.matchesSelector;
        w.addEventListener('DOMNodeInserted', function(event) {
            if (common.wrapping) return;
            if (matchesSelector(event.target, LZL_POST_SELECTOR)) {
                updateLzlPost(event.target);
            } else {
                if (matchesSelector(event.target, POST_SELECTOR)) {
                    updatePost(event.target);
                }
                if (event.target.querySelectorAll) {
                    var posts = event.target.querySelectorAll(POST_SELECTOR);
                    for (var i = 0; i < posts.length; i++)
                        updatePost(posts[i]);
                    
                    var lzlposts = event.target.querySelectorAll(LZL_POST_SELECTOR);
                    for (var i = 0; i < lzlposts.length; i++)
                        updateLzlPost(lzlposts[i]);
                }
            }
        }, false);
        
    })();
    
    (function() {
        if (!B_SWITCH_ENABLED) return;
        
        var prefs = [
            { name: '宽屏样式', id: 'wsenable', prefName: 'wideScreenEnabled', value: B_WIDESCREEN_ENABLED },
            { name: '楼层计数', id: 'fnenable', prefName: 'floorNumEnabled', value: B_FLOORNUM_ENABLED }
        ];
        var elemNavRight = d.querySelector('ul.nav_right');
        if (!elemNavRight) {
            var navbar = document.querySelector("#tb_nav");
            if (navbar) {
                elemNavRight = d.createElement("ul");
                elemNavRight.className = "nav_right";
                navbar.appendChild(elemNavRight);
            }
        }
        if (elemNavRight) {
            prefs.forEach(function(pref) {
                var strEnableDisable = pref.value ? '禁用' : '启用';
                var strText = strEnableDisable + pref.name;
                var liPrefItem = d.createElement('li');
                var aPrefItem = d.createElement('a');
                aPrefItem.setAttribute('id', pref.id);
                aPrefItem.setAttribute('title', strText + '\n(' + STR_SCRIPT_NAME + ')');
                aPrefItem.setAttribute('href', '#');
                aPrefItem.innerHTML = strText;
                aPrefItem.addEventListener('click', function() {
                    btise[pref.prefName] = !pref.value;
                    localStorage.btise = JSON.stringify(btise);
                    w.location.reload();
                    return false;
                });
                liPrefItem.appendChild(aPrefItem);
                elemNavRight.appendChild(liPrefItem);
            });
        }
    })();
    
    })(document, typeof(unsafeWindow) !== "undefined" ? unsafeWindow : window);
    
    
    

    external by Github modified Apr 18, 2016  125  0  2  0

    Best Site good looking <a href=" http://www.toadsplace.com/wp/buy-singulair-cheap.pdf ">where can i get singulair cheap</a> Expectations that a stronger U.S. economy will give the Fedroom to begin scaling back its bond-buying, most likely inSeptember,...

    Best Site good looking where can i get singulair cheap Expectations that a stronger U.S. economy will give the Fedroom to begin scaling back its bond-buying, most likely inSeptember, have sparked a near 5 percent rally in the dollar andsome 50-basis point rise in the benchmark 10-year U.S. bondyield since mid-June. : Ignacio
    Best Site good looking <a href=" http://www.toadsplace.com/wp/buy-singulair-cheap.pdf ">where can i get singulair cheap</a>  Expectations that a stronger U.S. economy will give the Fedroom to begin scaling back its bond-buying, most likely inSeptember, have sparked a near 5 percent rally in the dollar andsome 50-basis point rise in the benchmark 10-year U.S. bondyield since mid-June.
     
    
    

    external by khacanh modified Feb 26, 2015  102  0  3  0

    Scaling with push notification

    Scaling with push notification: gistfile1.md
    [Diagram](https://drive.google.com/file/d/0B3SzvmCFNh4cUWN6TF9pdVFYdUk/view)
    
    We'll keep one main firebase app where we keep `/rooms, /user-rooms, /presences` and use multiple shard apps for `/chat-messages`. 
    Here is some main nodes we are using: 
    ```javascript
    "rooms": {
      "authorizedUsers": {...}
    },
    
    "chat-messages": {
      "$roomId": {...}
    },
    
    "user-rooms": {
      "$userId": {
        "$roomId": {...}
      },
    "presences": {}
    ```
    
    We'll keep a `mainFirebaseRef` to point to the main firebase app, and a `shardFirebaseRef` to one of the sharded apps. This is the java code, we use 'enterRoom/leaveRoom` when user enter/leave a chat. 
    ```java
    ValueEventListener mRoomListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
          // Insert chat message
        }
    
        @Override public void onCancelled(FirebaseError error) { }
      };
    
    Firebase mainFirebaseRef = new Firebase("https://mychat.firebaseio.com");
    Firebase shardFirebaseRef = null;
    
    public void enterRoom(String roomId) {
      int server = roomId.hashCode() % N;
      String baseRef = "https://mychat-" + server + ".firebaseio.com";
      rootRef.auth(token);
      shardFirebaseRef = new Firebase(baseRef);
      shardFirebaseRef.authWithCustomToken(token, new Firebase.AuthResultHandler() {
        @Override
        public void onAuthenticated(AuthData authData) {
          mainFirebaseRef.child("user-rooms").child(currentUser.getId()).child("status").setValue(true);
    
          shardFirebaseRef.child("chat-messages").child(roomId).addChildEventListener("child_added", mRoomListener);
        }
      });
    }
    
    public void leaveRoom(String roomId) {
      mainFirebaseRef.child("user-rooms").child(currentUser.getId()).child("status").setValue(false);
      shardFirebaseRef.child("chat-messages").child(roomId).removeEventListener(mRoomListener);
      shardFirebaseRef.unauth();
    }
    ```
    
    On the push server side, we'll have an army of workers to watch sharded firebase apps and send push when it's needed. We can have several workers to watch over a firebase app. For each room in `/chat-messages/$roomdId` we'll need a listener to detect newly added chat messages. This code is implemented with node SDK. 
    ```javascript
    mainFirebaseRef.child("user-rooms").child(user.id).on("child_changed", function(snapshot) {
      var roomId = snapshot(snapshot.key());
      var status = snapshot.val().status;
      users[user.id].rooms[roomId] = status; // Or save to DB
    });
    
    shardFirebaseRef.child("chat-messages").child(roomId).on("child_added", function(snapshot) {
      for (var userId in room.authorizedUsers) {
        if (!users[userId].rooms[roomId]) {
          // User is not active in this room, send push to client via push Protocol
          sendPushToUser(userId, snapshot.val());
        }
      }
    });
    ```
    
    This structure can solve the problem with sharding and also keep users updated with what's happening outside of the active room.  This will push stress to the push server but that should be fine since we can just add more hardware to it. Here are my concerns:
    
    1. Does the structure make sense? If yes then:
    2. Is it ok to have one listener with javascript SDK for each `/chat-messages/$roomId`?
    3. `/chat-messages` is the heaviest node because it's where users send/receive msgs, applying sharding to it should be enough? We are going to optimize other nodes to reduce stress on the main firebase app.
    
    

    external by Efemer modified Feb 10, 2016  4228  39  3  0

    Custom Android ImageView for top-crop scaling of the contained drawable.

    Custom Android ImageView for top-crop scaling of the contained drawable.: TopCropImageView.java
    import android.content.Context;
    import android.graphics.Matrix;
    import android.widget.ImageView;
    
    /**
     * ImageView to display top-crop scale of an image view.
     *
     * @author Chris Arriola
     */
    public class TopCropImageView extends ImageView {
    
        public TopCropImageView(Context context) {
            super(context);
            setScaleType(ScaleType.MATRIX);
        }
        
        @Override
        protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
            super.onLayout(changed, left, top, right, bottom);
            recomputeImgMatrix();
        }
    
        @Override
        protected boolean setFrame(int l, int t, int r, int b) {
            recomputeImgMatrix();
            return super.setFrame(l, t, r, b);
        }      
        
        private void recomputeImgMatrix() {
            final Matrix matrix = getImageMatrix();
                
            float scale;
            final int viewWidth = getWidth() - getPaddingLeft() - getPaddingRight();
            final int viewHeight = getHeight() - getPaddingTop() - getPaddingBottom();
            final int drawableWidth = getDrawable().getIntrinsicWidth();
            final int drawableHeight = getDrawable().getIntrinsicHeight();
            
            if (drawableWidth * viewHeight > drawableHeight * viewWidth) {
                scale = (float) viewHeight / (float) drawableHeight;
            } else {
                scale = (float) viewWidth / (float) drawableWidth;
            }
                
            matrix.setScale(scale, scale);
            setImageMatrix(matrix);
        }
    }
    
    

    external by Hidetaka Kojo modified Jul 4, 2016  80  0  1  0

    elixirのdeployとauto scaling

    elixirのdeployとauto scaling: elixir_basic.md
    ### 構成検討
    
    + packaging
      + ansible / exrm
    + release location
      + S3
    + deploy
      + consul + ??
    
    ### 参考資料
    
    + PXC & consul
      + https://www.percona.com/blog/2014/12/05/streamlined-pxc-anything-testing-consul-vagrant/
    + autoscaling + consul
      + https://speakerdeck.com/fujiwara3/consultozi-zuo-osswohuo-yong-sita100tai-gui-mo-falsewebsabisuyun-yong
    + exrm + drone.io
      + http://qiita.com/xtity/items/f84c40cb5e9b8258e3ed
    + autoscalingの勘所
      + https://speakerdeck.com/fujiwara3/awsfalseotosukerutonakayokufu-kihe-u
    + streacher
      + https://speakerdeck.com/fujiwara3/stretcher-1
    
    
    • Public Snippets
    • Channels Snippets