如何用JS改变图像的原始大小

JavaScript08

如何用JS改变图像的原始大小,第1张

用JS改变图像的原始大小方法:

var canvas = document.getElementById("canvas")

var context = canvas.getContext("2d")

$('input[type=file]').change(function(){

var file=this.files[0]

var reader=new FileReader()

var image=new Image()

reader.readAsDataURL(file)

reader.onload=function(){

// 通过 reader.result 来访问生成的 DataURL

var url=reader.result

image.src=url

alert(image.width)

alert(image.height)

image.height /=4

image.width /=4

canvas.setAttribute("width", image.width+"px")

canvas.setAttribute("height", image.height+"px")

alert(image.naturalWidth)

alert(image.naturalHeight)

context.drawImage(image,0,0,image.width,image.height)

}

})

如何获取图片的原始尺寸大小?

如下,当给 img 设置一个固定的大小时,要怎样获取图片的原始尺寸呢?

#oImg{

width: 100px

height: 100px

}

<img src="images/test.jpg" id="oImg" alt="">

方法一:

HTML5提供了一个新属性 naturalWidth / naturalHeight 可以直接获取图片的原始宽高。这两个属性在Firefox/Chrome/Safari/Opera及IE9里已经实现。

w = document.getElementsByTagName("img")[0].naturalWidth

h = document.getElementsByTagName("img")[0].naturalHeight

console.log(w + '  ' + h)

打印出来的结果与原始尺寸相符。但有个前提是,必须在图片完全下载到客户端浏览器才能判断。

如果是不支持的版本浏览器,那可以用传统方法判断,如下:

var img = document.getElementById("oImg"),

w,hif (oImg.naturalWidth) {// HTML5 browsersw = oImg.naturalWidth

h = oImg.naturalHeight

} else {// IE 6/7/8var nImg = new Image()//      nImg.src = oImg.srcnImg.onload = function () {

w = nImg.width

h = nImg.height

console.log(w + "  " + h)

}

nImg.src = oImg.src}

此时为什么要加 onload 的原因是,图片可能没加载完成,导致图片的 width 、height 无法获取到。

这里还有有个点要注意, nImg.src = oImg.src 这段代码为什么要放在 nImg.onload 函数后面? 这样做是为了兼容 ie 。ie 除了第一次加载图片时候获取正常,之后再刷新就没有反应了。其他大部分浏览器获取正常。这是什么原因呢?

原因也挺简单的,就是因为浏览器的缓存了。当图片加载过一次以后,如果再有对该图片的请求时,由于浏览器已经缓存住这张图片了,不会再发起一次新的请求,而是直接从缓存中加载过来。对于大部分浏览器,它们视图使这两种加载方式对用户透明,同样会引起图片的onload事件,而 ie 则忽略了这种同一性,不会引起图片的onload事件。。。一般情况下,可以用 complete 来判断图片是否加载完毕。对于 complete 属性来讲,IE是根据图片是否显示过来判断,就是说当加载的图片显示出来后,complete 属性的值才为true ,否则一直是false ,和以前是否加载过该张图片没有关系,即和缓存没有关系!可以写如下的函数来做到各个浏览器中预加载图片的兼容性。如下:

var img = document.getElementById("oImg"),

w,hif (oImg.naturalWidth) {

// HTML5 browsersw = oImg.naturalWidth

h = oImg.naturalHeight

} else {

// IE 6/7/8var nImg = new Image()

nImg.src = oImg.src

if(nImg.complete) { // 图片已经存在于浏览器缓存console.log(nImg.width + "  " + nImg.height)

}else{

nImg.onload = function () {

w = nImg.width

h = nImg.height

console.log(w + "  " + h)

}

}

}

最后封装成函数,如下:

function getImgNaturalDimensions(oImg, callback) {var nWidth, nHeight

if (!oImg.naturalWidth) { // 现代浏览器nWidth = oImg.naturalWidth

nHeight = oImg.naturalHeight

callback({w: nWidth, h:nHeight})

} else { // IE6/7/8var nImg = new Image()

       nImg.onload = function() {             var nWidth = nImg.width,

                nHeight = nImg.height

          callback({w: nWidth, h:nHeight})

}

nImg.src = oImg.src

}

}

var img = document.getElementById("oImg")getImgNaturalDimensions(img, function(dimensions){

console.log(dimensions.w)

})

方法二:(图片不需要再页面中展示)

将 img 放在页面中不可见的位置上,缺点是:这种方法需要浏览器加载这张图片

.imgbox{// img 盒子width: 0

overflow: hidden

}

然后在去取图片宽高等信息:

function getImgNaturalDimensions2(oImg) {var nWidth, nHeightif (!oImg.naturalWidth) { // 现代浏览器nWidth = oImg.naturalWidth

nHeight = oImg.naturalHeightreturn ({w: nWidth, h:nHeight})

} else { // IE6/7/8nWidth = oImg.width

nHeight = oImg.heightreturn ({w: nWidth, h:nHeight})

}

}var getImg = getImgNaturalDimensions2(img)

console.log(getImg)

然而,到这里,是不是万事大吉了呢?答案当然是否定的。

你用这种方法做后,会发现你有时可以取得 img 的 width、height ,但有时会是个 0。原因见下节分析。

我们先看如何正常去取吧!

var img = document.getElementById("oImg")

img.onload = function(){    //方法一    getImgNaturalDimensions(img, function(dimensions){

console.log(dimensions.w)

})   //方法二

var getImg = getImgNaturalDimensions2(img)

console.log(getImg)

}

只需在 img.onload 函数内去调用函数。

项目的需求是上传一张图片,然后验证该图片的尺寸,如果跟预期的不符,就不给上传

首先,可以通过new FileReader(),再通过将上传的图片文件传给实例的readAsDataUrl(),当文件加载完成触发onload事件,传递event,通过event.target.result来获取图片的链接(base64格式的)

接着,new Image()实例,将上面获取的链接设置为图片的src属性,当图片加载完成触发onload事件就可以通过width、height属性来读取图片的长宽啦~~