⑴ 怎麼用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後,高度會自動按比例擴大。