mirror of
https://github.com/danbee/myimages
synced 2025-03-04 08:39:09 +00:00
1 line
3.6 KiB
JavaScript
1 line
3.6 KiB
JavaScript
(function(b){function a(i,l,j,c,k){var e,d,h,g,f;e=document.createElement("canvas");e.style.display="none";document.body.appendChild(e);d=e.getContext("2d");h=new Image();h.onload=function(){var o,m,n;f=Math.min(l/h.width,j/h.height);if(f<1){o=Math.round(h.width*f);m=Math.round(h.height*f)}else{o=h.width;m=h.height}e.width=o;e.height=m;d.drawImage(h,0,0,o,m);g=e.toDataURL(c);g=g.substring(g.indexOf("base64,")+7);g=atob(g);e.parentNode.removeChild(e);k({success:true,data:g})};h.src=i}b.runtimes.Html5=b.addRuntime("html5",{init:function(g,h){var c={},e;function f(m){var k,j,l=[],n;for(j=0;j<m.length;j++){k=m[j];n=b.guid();c[n]=k;l.push(new b.File(n,k.fileName,k.fileSize))}g.trigger("FilesAdded",l)}function d(){var i;if(window.XMLHttpRequest){i=new XMLHttpRequest();return !!(i.sendAsBinary||i.upload)}return false}if(!d()){h({success:false});return}g.bind("Init",function(n){var r,p=[],m,q,k=n.settings.filters,l,o,j=document.body;r=document.createElement("div");r.id=n.id+"_html5_container";for(m=0;m<k.length;m++){l=k[m].extensions.split(/,/);for(q=0;q<l.length;q++){o=b.mimeTypes[l[q]];if(o){p.push(o)}}}b.extend(r.style,{position:"absolute",background:g.settings.shim_bgcolor||"transparent",width:"100px",height:"100px",overflow:"hidden",opacity:g.settings.shim_bgcolor?"":0});r.className="plupload html5";if(g.settings.container){j=document.getElementById(g.settings.container);j.style.position="relative"}j.appendChild(r);r.innerHTML='<input id="'+g.id+'_html5" style="width:100%;" type="file" accept="'+p.join(",")+'" '+(g.settings.multi_selection?'multiple="multiple"':"")+" />";document.getElementById(g.id+"_html5").onchange=function(){f(this.files);this.value=""}});g.bind("PostInit",function(){var i=document.getElementById(g.settings.drop_element);if(i){b.addEvent(i,"dragover",function(j){j.preventDefault()});b.addEvent(i,"drop",function(k){var j=k.dataTransfer;if(j&&j.files){f(j.files)}k.preventDefault()})}});g.bind("Refresh",function(i){var j,k,l;j=document.getElementById(g.settings.browse_button);k=b.getPos(j,document.getElementById(i.settings.container));l=b.getSize(j);b.extend(document.getElementById(g.id+"_html5_container").style,{top:k.y+"px",left:k.x+"px",width:l.w+"px",height:l.h+"px"})});g.bind("UploadFile",function(i,l){var p=new XMLHttpRequest(),k=p.upload,j=i.settings.resize,o,n=0;function m(q){var u="----pluploadboundary"+b.guid(),s="--",t="\r\n",r="";if(i.settings.multipart){p.setRequestHeader("Content-Type","multipart/form-data; boundary="+u);b.each(i.settings.multipart_params,function(w,v){r+=s+u+t+'Content-Disposition: form-data; name="'+v+'"'+t+t;r+=w+t});r+=s+u+t+'Content-Disposition: form-data; name="file"; filename="'+l.name+'"'+t+"Content-Type: application/octet-stream"+t+t+q+t+s+u+s+t;n=r.length-q.length;q=r}p.sendAsBinary(q)}if(l.status==b.DONE||l.status==b.FAILED||i.state==b.STOPPED){return}if(k){k.onprogress=function(q){l.loaded=q.loaded-n;i.trigger("UploadProgress",l)}}p.onreadystatechange=function(){if(p.readyState==4){l.status=b.DONE;l.loaded=l.size;i.trigger("UploadProgress",l);i.trigger("FileUploaded",l,{response:p.responseText,status:p.status})}};p.open("post",b.buildUrl(i.settings.url,{name:l.target_name||l.name}),true);p.setRequestHeader("Content-Type","application/octet-stream");o=c[l.id];if(p.sendAsBinary){if(j&&/\.(png|jpg|jpeg)$/i.test(l.name)){a(o.getAsDataURL(),j.width,j.height,/\.png$/i.test(l.name)?"image/png":"image/jpeg",function(q){if(q.success){l.size=q.data.length;m(q.data)}else{m(o.getAsBinary())}})}else{m(o.getAsBinary())}}else{p.send(o)}});e=!!(File&&File.prototype.getAsDataURL);g.features={dragdrop:window.mozInnerScreenX!==undefined,jpgresize:e,pngresize:e};h({success:true})}})})(plupload); |