zoukankan      html  css  js  c++  java
  • js图片压缩

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title></title>
    		<style type="text/css">
    			img{
    				max- 400px;
    				
    			}
    			.select{
    				
    			}
    		</style>
    	</head>
    	<body>
    		<div class="head_img pr">  
                <em class="pa"></em>  
                 
                <div class="select">
                	选择压缩的文件<input id="photo" type="file" accept="image/*" />  
                </div>
                <a href="" download="" id="down">
                	<img src='' alt="" class="modify_img" />
                </a> 
            </div>  
    	</body>
    	<script type="text/javascript" src="js/jquery.js"></script>
    	<script type="text/javascript">
    		$('#photo').change(function(){  
                    var _this = $(this)[0],  
                        _file = _this.files[0],  
                        fileType = _file.type;  
                        console.log(_file.size);  
                    if(/image/w+/.test(fileType)){  
                       
                        var fileReader = new FileReader();  
                        fileReader.readAsDataURL(_file);  
                        fileReader.onload = function(event){  
                            var result = event.target.result;   //返回的dataURL  
                            var image = new Image();  
                            image.src = result;  
                            image.onload = function(){  //创建一个image对象,给canvas绘制使用  
                                var cvs = document.createElement('canvas');  
                                var scale = 0.8;    
                                if(this.width > 1000 || this.height > 1000){  //1000只是示例,可以根据具体的要求去设定    
                                    if(this.width > this.height){    
                                        scale = 1000 / this.width;  
                                    }else{    
                                        scale = 1000 / this.height;    
                                    }    
                                }  
                                cvs.width = this.width*scale;    
                                cvs.height = this.height*scale;     //计算等比缩小后图片宽高  
                                var ctx = cvs.getContext('2d');    
                                ctx.drawImage(this, 0, 0, cvs.width, cvs.height);     
                                var newImageData = cvs.toDataURL(fileType, 0.8);   //重新生成图片,<span style="font-family: Arial, Helvetica, sans-serif;">fileType为用户选择的图片类型</span>  
                                var sendData = newImageData.replace("data:"+fileType+";base64,",'');  
                               	$('.modify_img').attr('src',newImageData);
                                $("#down").attr("href",newImageData);
                            }  
                              
                        }  
                    }else{  
                        $.notify.show('请选择图片格式文件', {placement: 'center'});  
                    }  
                });  
    	</script>
    </html>
    

      

  • 相关阅读:
    Python中的traceback模块
    硬链接和软连接的区别
    Python中的fileinput模块和tempfile模块
    SQL Relay
    使用python+cron对php状态进行定时监控
    给想学习asp小朋友的建议
    emerg]: bind() to 0.0.0.0:80 failed (98: Address already in use)
    python非贪婪、多行匹配正则表达式例子[转载]
    python中eval, exec, execfile,和compile [转载]
    url extracting 未测试
  • 原文地址:https://www.cnblogs.com/muamaker/p/8431465.html
Copyright © 2011-2022 走看看