⑴ 怎么用js实现图片的缩小
一般来说,实现图片的放大缩小功能都用到了比较大的封装插件,特别是以jQuery插件居多,而实际上单纯实现对原图本身的放大缩小,用简单几行原生JS代码就可以做到。在今天分享的这个实例中,点击放大按钮不松鼠标,图片会不断的逐渐放大,当然也可以点一下放大一点,点击缩小按钮则反之,有需要的朋友可以考虑收藏备用哦
以下为全部代码:
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>javascript控制图片缩小或者放大</title>
</head>
<body>
<scripttype="text/javascript">
varoTime;
functionchangeSize(id,action){
varobj=document.getElementById(id);
obj.style.zoom=parseInt(obj.style.zoom)+(action=='+'?+10:-10)+'%';
oTime=window.setTimeout('changeSize(''+id+'',''+action+'')',100);
}
document.onmouseup=function(){
window.clearTimeout(oTime);
}
</script>
<divstyle="height:350px;overflow:auto;">
<imgid="headImg"src="
<buttononmousedown="changeSize('headImg','+');"onmouseup="window.clearTimeout(oTime);">放大</button>
<buttononmousedown="changeSize('headImg','-');"onmouseup="window.clearTimeout(oTime);">缩小</button>
</body>
</html>
⑵ 怎么用JavaScript在线压缩图片
主要用了两个html5的 API,一个file,一个canvas,压缩主要使用cnavas做的,file是读取文件,之后把压缩好的照片放入内存,最后内存转入表单下img.src,随着表单提交。
照片是自己用单反拍的,5M多,压缩下面3张分别是600多kb,400多kb,300kb的最后那张失真度很大了,压缩效率蛮高的。
<!DOCTYPE html>
<html><head> <meta charset="utf-8"/> <title>File API Test</title> <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script> <script type="text/javascript" src="js/JIC.js"></script> <style> #test{ display: none; } </style></head><body><input type="file" id="fileImg" ><form> <img src="" id="test" alt=""></form><script> function handleFileSelect (evt) { // var filebtn = document.getElementById(id); // console.log(filebtn); // var files = filebtn.target.files; // console.log(filebtn.target); // console.log(files); var files = evt.target.files; for (var i = 0, f; f = files[i]; i++) { // Only process image files. if (!f.type.match('image.*')) { continue; } var reader = new FileReader(); // Closure to capture the file information. reader.onload = (function(theFile) { return function(e) { // Render thumbnail. // console.log(evt.target.files[0]); // console.log(e.target); console.log(e.target.result); var i = document.getElementById("test"); i.src = event.target.result; console.log($(i).width()); console.log($(i).height()); $(i).css('width',$(i).width()/10+'px'); //$(i).css('height',$(i).height()/10+'px'); console.log($(i).width()); console.log($(i).height()); var quality = 50; i.src = jic.compress(i,quality).src; console.log(i.src); i.style.display = "block"; }; })(f); // Read in the image file as a data URL. reader.readAsDataURL(f); } } document.getElementById('fileImg').addEventListener('change', handleFileSelect, false);</script></body></html>
var jic = { /** * Receives an Image Object (can be JPG OR PNG) and returns a new Image Object compressed * @param {Image} source_img_obj The source Image Object * @param {Integer} quality The output quality of Image Object * @return {Image} result_image_obj The compressed Image Object */ compress: function(source_img_obj, quality, output_format){ var mime_type = "image/jpeg"; if(output_format!=undefined && output_format=="png"){ mime_type = "image/png"; } var cvs = document.createElement('canvas'); //naturalWidth真实图片的宽度 cvs.width = source_img_obj.naturalWidth; cvs.height = source_img_obj.naturalHeight; var ctx = cvs.getContext("2d").drawImage(source_img_obj, 0, 0); var newImageData = cvs.toDataURL(mime_type, quality/100); var result_image_obj = new Image(); result_image_obj.src = newImageData; return result_image_obj; }, function ****(***)
⑶ 图片自动缩小的JS代码
var flag=false;
function DrawImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= 670/380){
if(image.width>670){
ImgD.width=670; ImgD.height=(image.height*380)/image.width;
}else{
ImgD.width=image.width; ImgD.height=image.height;
}
}else{
if(image.height>380){
ImgD.height=380; ImgD.width=(image.width*380)/image.height;
}else{
ImgD.width=image.width; ImgD.height=image.height;
}
} } }</script><img src="图片" border=0 width="670" height="380" onload="javascriptrawImage(this);">
⑷ JavaScript等比例缩放图片控制超出范围的图片
js等比例缩放图片,这个功能非常实用,当网页加载一个尺寸比较大的图片时,往往会把一个网页撑的变形,页面变得很难看,于是我们就想到了用JS去控制超出一定范围的图片,脚本之家以稳定页面布局,本代码段就是完成了此功能,而且代码非常简洁,效果很好。
复制代码
代码如下:
<html><head><title>等比例缩放图片</title><script>function
DrawImage(ImgD,iwidth,iheight){
//参数(图片,允许的宽度,允许的高度)
var
image=new
Image();
image.src=ImgD.src;
if(image.width>0
&&
image.height>0){
if(image.width/image.height>=
iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
}
}
}
</script></head><body><img
src=http://www.jb51.net/uploadfile/2013/0803/20130803034531502.jpg"
alt="自动缩放后的效果"
width="100"
height="100"
onload="javascript:DrawImage(this,80,80)"
/></body></html>
⑸ 求一个简单的点击图片放大缩小的JS代码
1、准备好需要用到的图标。
⑹ jS控制图片的放大和缩小
图片按比例缩放
function DrawImage(Img,WIDTH,HEIGHT){
var image=new Image();
image.src=Img.src;
width=WIDTH;//预先设置的所期望的宽的值
height=HEIGHT;//预先设置的所期望的高的值
if(image.width>width||image.height>height){//现有图片只有宽或高超了预设值就进行js控制
w=image.width/width;
h=image.height/height;
if(w>h){//比值比较大==>宽比高大
//定下宽度为width的宽度
Img.width=width;
//以下为计算高度
Img.height=image.height/w;
}else{//高比宽大
//定下宽度为height高度
img.height=height;
//以下为计算高度
Img.width=image.width/h;
}
}
}
<img src="xxxx" onload=javascript:DrawImage(this,290,215);>
⑺ 怎样在客户端 利用js 压缩图片 大小,然后上传至服务器比如2M压缩成几十KB
无法实现,js没有权限去修改本地文件的,只能是将大图上传到服务器后再压缩
⑻ js 压缩图片
不可能,貌似你说的这个用单纯的脚本没有能做到的
这个...我还真是不知道,因为对js不是很熟悉,我所知道的是可以用第三方组件,比如aspjpeg之类的可以实现
⑼ 当前使用JS在前端完成图片压缩的有哪些方法
这个base64的编码并不能减小图片,反而增大了,大概增大了1/3。至于有没有其他的方法我就不知道了,不过直接构造Blob对象上传就行了,为什么要上传dataurl
⑽ JS控制图片放大和缩小怎么改
用js控制图片额大小。主要是修改图片的宽度和高度。下面是简单的代码实现:
HTML 代码:
<imgsrc='../1.jgp'id='img'/>
这个时候img的图片自身是多大,就会显示多大。100px*100px的图。
js代码:
varoImg=document.getElementById('img');
oImg.width='50px';//当给img标签的宽度设置为50px后,高度会自动按比例缩小。
oImg.width='200px'//当给img标签的宽度设置为200px后,高度会自动按比例扩大。