图片大小适配方法整理

1、H5 data-fullsrc属性

<img src="small.jpg" data-fullsrc="large.jpg">

Date-fullsrc是html5中的一个定义文件的属性,宽度超过480px的屏幕,就会加载较大分辨率的图片(large.jpg),小屏幕分辨率的就会加载较小的图片(small.jpg)。

Reference:http://filamentgroup.com/lab/responsive_images_experimenting_with_context_aware_image_sizing/

DEMO:https://www.filamentgroup.com/examples/responsive-images/

主要代码:

JS(https://www.filamentgroup.com/examples/responsive-images/responsiveimgs.js

HTML: <img src="sample-content/running-sml.jpg?full=sample-content/running-lrg.jpg" />  "?"前后分别是小图大图

2、其他 Here are 6 different takes on Responsive Images. (http://bechster.com/responsive-images-6-techniques/)

1. Picturefill

A little history: In November 2011, Bruce Lawson got frustrated over the lack of standards for marking up adaptive images. Therefore, he proposed to re-use the syntax of the HTML5<video> element in the form of a <picture> element. The <picture> element makes it possible to define multiple image sources. By using media queries you can then control under which circumstances an images should be displayed to the user.

The <picture> element is still on the drawing board, unsupported in all browsers, and therefore as of yet unusable.

However, Scott Jehl thought it was such a good idea, that he made the polyfill, Picturefill, which makes a similar syntax work with JavaScript:

<div data-picture="" data-alt="">
  <div data-src="small.jpg"></div>
  <div data-src="medium.jpg" data-media="(min-width: 400px)"></div>
  <div data-src="large.jpg" data-media="(min-width: 800px)"></div>
  <div data-src="extralarge.jpg" data-media="(min-width: 1000px)"></div>
  <!-- Fallback content for non-JS browsers -->
  <noscript>
    <img src="small.jpg" alt="">
  </noscript>
</div>

Advantages: Lightweight, easy implementation of the script.
Drawbacks: Requires a great deal of extra mark up, which makes it hard to implement retrospectively on existing content.
Demo: http://scottjehl.github.io/picturefill/
Download & documentation: https://github.com/scottjehl/picturefill

2. HiSRC

HiSRC is a jQuery plugin for adaptive images, written by Christopher Schmitt. It tries to take into account the screen size as well as the bandwidth. Images are placed in the HTML document with the usual <img> tag and alternative image sources are then defined with data attributes:

<div class="hisrc">
  <img alt="" src="small.jpg" data-1x="medium.jpg" data-2x="large.jpg" />
</div>

Advantages: Clean and semantically valid code, lightweight.
Drawbacks: Requires extra mark up and data attributes on the <img> element, which makes it hard to implement on existing content.
Download & documentation: https://github.com/teleject/hisrc

3. Responsive-Enhance

Responsive Enhance is a tiny JavaScript, written by Josh Emerson. The script works on <img>elements together with data attributes:

<img id="demo" src="medium.jpg" alt="" data-fullsrc="large.jpg">
<script>responsiveEnhance(document.getElementById(‘demo‘), 400);</script>

Advantages: Clean and semantically valid code, lightweight.
Drawbacks: Requires extra mark up and data attributes on the <img> element, which makes it hard to implement on existing content. Downloads 2 images on large displays.
Download & documentation: https://github.com/joshje/Responsive-Enhance

4. Adaptive Images

Adaptive Images is primarily a server-side technique, which “sniffs” the users’ screen size and automatically scales, caches and provides appropriate versions of the websites’ images. It does so by using .htaccess, a php file and a single line of JavaScript, completely without editing any existing markup. Adaptive Images is written by Matt Wilcox.

Advantages: Clean and semantically valid code, easy to implement on existing content.
Drawbacks: Sets a cookie in the user’s browser, requires server-side setup; Apache, PHP and .htaccess.
Demo, download & documentation: http://adaptive-images.com/

5. Compressive Images (my favorite)

More details, fewer kilobytes: This technique is about providing only one version af the image to the user regardless of screen size, but in return compressing it heavily. Daan Jobsis has made experiments which show a very limited loss of quality (sometimes even an improvement), when large, heavily compressed images are squeezed together in just half of their original dimensions. See also Filament Group’s post on the same subject.

Example: A .jpg with the dimensions 1280 × 1024 pixels is squeezed down to 640 × 512 pixels or less.

Practically, you will set the image sizes in CSS with max-width: 100%; (The Fluid Images technique).

Low res 301 × 200 pixels,
jpeg quality 80%, 34kb.
Hi res 602 × 400 pixels,
jpeg quality 25%, 27kb.

In other words, you are not able to see the compression. This method is my favorite so far, because it is so incredibly simple.

Advantages: Very simple. In some cases even better results. Perfect for retina-displays. Small file sizes.
Drawbacks: Implementing in a CMS requires setting up some image compression on the server.
Demo & documentation: http://blog.netvlies.nl/design-interactie/retina-revolution/

6. Responsive Images Clown Car (SVG embedding)

Rather experimental: The Clown Car technique is developed by Estelle Weyl and is about taking advantage of the possibilities in the SVG format. The reference to the SVG file is placed in the usual <img> element:

<img src="image.svg" alt="responsive image">

It is in the SVG file the magic is happening. The technique is based on the fact, that SVG already supports both bitmap graphics and media queries:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 329" preserveAspectRatio="xMidYMid meet">
 <title>Clown Car Technique</title>
 <style>
  svg {
    background-size: 100% 100%;
    background-repeat: no-repeat;
  }
 @media screen and (max-width: 400px) {
  svg {background-image: url(images/small.png");}
 }
 @media screen and (min-width: 401px) and (max-width: 700px) {
  svg {background-image: url(images/medium.png);}
 }
 @media screen and (min-width: 701px) and (max-width: 1000px) {
  svg {background-image: url(images/big.png);}
 }
 @media screen and (min-width: 1001px) {
  svg {background-image: url(images/huge.png);}
 }
 </style>
</svg>

Advantages: Media queries are baked in to SVG, SVG supports bitmap graphics. Only the necessary image is downloaded. All logic lies inside the SVG image.
Drawbacks: SVG is unsupported in IE8. Import of external bitmap images are blocked by the Content Security Policy of most browsers.
Demo: http://estelle.github.io/clowncar/bgonly.html
Documentation: http://www.standardista.com/responsive-images-clown-car-technique/

时间: 2024-10-29 10:46:33

图片大小适配方法整理的相关文章

ios 改变图片大小缩放方法

http://www.cnblogs.com/zhangdadi/archive/2012/11/17/2774919.html http://bbs.csdn.net/topics/390898581 ios 改变图片大小缩放方法 -(UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size{    UIGraphicsBeginImageContext(size);  //size 为CGSize类型,即你所需要的图片尺寸

压缩图片大小的方法介绍

上传图片的时候我们会发现很多网站对图片大小的限制一般都是最大不超过20k,可能这是一个最佳的大小吧,既然规定了,就只能按照规定的大小进行上传,不过超过20k的图片那真的数不胜数了,遇到过大的图片只能进行大小的压缩,下面是压缩图片大小的方法介绍,不知道方法的话可以来学习一下!具体方法如下:图片压缩软件请添加链接描述可以压缩图片1:打开压缩软件,点击图片压缩就好. 2:添加要进行压缩的图片,点击添加文件或者添加文件夹的按钮就可以选择文件,一次可以同时压缩很多张图片.3:在添加文件下面有输出格式以及压

ios 判断相册文件图片大小的方法

获取ios相册图片文件大小,很多人,想到的就是取到文件图片的位置,然后通过NSdata来获取图片大小,殊不知,这样获取到的图片大小并非准确. 正确的方法应是在代理中实现 ALAssetsLibrary* alLibrary = [[ALAssetsLibrary alloc] init]; [alLibrary assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *as

关于前端使用JavaScript获取base64图片大小的方法

base64原理 Base64编码要求把3个8位字节(38=24)转化为4个6位的字节(46=24),之后在6位的前面补两个0,形成8位一个字节的形式. 如果剩下的字符不足3个字节,则用0填充,输出字符使用'=',因此编码后输出的文本末尾可能会出现1或2个'=' 如何获取base64图片大小 通过base64编码原理我们知道,base64的图片字符流中的每8个字符就有两个是用0补充,而且字符流的末尾还可能存在'='号,我们可以通过这个原理计算图片的文件流大小. getImgByteSize(da

通过url动态获取图片大小方法总结

很多时候再项目中,我们往往需要先获取图片的大小再加载图片,但是某些特定场景,如用过cocos2d-js的人都知道,在它那里只能按比例缩放大小,是无法设置指定大小的图片的,这就是cocos2d-js 的坑了,我们必须先获取图片大小,计算比例再对图片进行缩放. 查阅资料,我总结了两种通过url获取图片大小的方法: 1.预加载获取图片大小 var imgLoad = function (url, callback) { var img = new Image(); img.src = url; if

Keil C减小代码编译量大小的方法

keil-C减小代码编译大小的方法整理 方法一:(通过优化代码减小) 1.1少做乘除运算,使用左/右移位来实现乘除 Eg ,普通:a = 0x80*4: 优化:a = 0x80<<2: 1.2在不影响运算条件下,使用短类型代替长类型 Eg ,普通: int a: 优化: char a: 1.3尽量使用无符号类型数据 Eg ,普通:char a = 56; 优化:unsigned char a = 56; 1.4回避使用浮点类型数据做乘除运算,这样代码量很大 Eg ,普通:float a = 5

JavaScript实现判断图片是否加载完成的3种方法整理

JavaScript实现判断图片是否加载完成的3种方法整理 有时候我们在前端开发工作中为了获取图片的信息,需要在图片加载完成后才可以正确的获取到图片的大小尺寸,并且执行相应的回调函数使图片产生某种显示效果.本文主要整理了几种常见的javascipt判断图片加载完成时的方法,并通过代码与实际应用相结合进行解释与说明. onload方法 通过向img标签添加onload属性,并填入相应的函数来执行后续的javascipt代码.如下代码例子中img元素默认是不显示的,通过onload判断加载完成后再将

S实现控制图片显示大小的方法【图片等比例缩放功能】

S实现控制图片显示大小的方法[图片等比例缩放功能] [需求]:读取磁盘中的图片,展示在弹出框中,等比例缩放图片,使图片显示完全. (读取磁盘中的图片展示在前台,请参照我的另一篇文章:) [开发]: 调用说明: 直接调用js函数即可. 我测试是一个image 标签中直接调用,如下: <div> <img id="showImageimg"  src="/sirdifoa/applycorrection/getImage.do?imgName=2017001.j

向服务器上传图片的时候,如果图片大小超过服务器限制,需要对图片进行压缩,下面是压缩方法

//循环压缩,知道服务器限制,我这边的限制是2MB for (int i = 0; i < 10; i++) { imageData = UIImagePNGRepresentation(img); if (imageData.length > 2*1000*1000) { img = [LDUnity compressImageWith:img]; NSLog(@"第%d次压缩 === %lu",i,(unsigned long)imageData.length); co