Advertisement
asimryu

html5 drop image ex - 1

Aug 22nd, 2016
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.90 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Drop Image</title>
  6.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  7.     <style>
  8.         #drophere {
  9.             width: 500px;
  10.             height: 500px;
  11.             position: relative;
  12.         }
  13.         .pink { background-color: pink; }
  14.         .yellow { background-color: yellow; }
  15.         .xbtn {
  16.             width: 50px;
  17.             height: 50px;
  18.             background-color: black;
  19.             border-radius: 50%;
  20.             color: white;
  21.             text-align: center;
  22.             line-height: 50px;
  23.             position: absolute;
  24.             top: 5px;
  25.             right: 5px;
  26.             cursor: pointer;
  27.         }
  28.     </style>
  29. </head>
  30. <body>
  31.     <div class="container-fluid">
  32.         <div class="row">
  33.  
  34.             <div class="col-md-6">
  35.                 <div id="drophere" class="yellow" ondrop="drop(event)" ondragover="dragover(event)" ondragleave="dragleave(event)">
  36.                     drop images here!
  37.                 </div>
  38.                 <form action="" onsubmit="return false">
  39.                     <div class="form-group">
  40.                         <label for="imgtitle">사진제목</label>
  41.                         <input type="text" name="imgtitle" id="imgtitle" class="form-control" placeholder="제목을 입력하세요.">
  42.                     </div>
  43.                     <button type="submit" class="btn-submit btn btn-info">등록하기</button>
  44.                 </form>
  45.             </div>
  46.  
  47.             <div class="col-md-6">
  48.                 <div id="photolist"></div>
  49.             </div>
  50.  
  51.         </div>
  52.        
  53.     </div>
  54.    
  55.  <script src="http://code.jquery.com/jquery-latest.js"></script>
  56.     <script>
  57.  
  58.         var photos = [];
  59.         window.onload = function(){
  60.             localdata = localStorage.getItem("photo");
  61.             if( localdata ) {
  62.                 var photodata = JSON.parse(localdata);
  63.                 for( var i=0; i<photodata.length; i++){
  64.                     photos.push(photodata[i]);
  65.  
  66.                 var img = "<img src='" + photodata[i].src + "' width='" + photodata[i].width + "' height='" + photodata[i].height + "'>";
  67.                 var html = "<div class='pbox'>";
  68.                 html += img;
  69.                 html += "<p>" + photodata[i].title + "</p>";
  70.                 html +="</div>";
  71.  
  72.                 $("#photolist").append(html);
  73.  
  74.                 }
  75.                 console.log(photos);
  76.             }
  77.         };
  78.  
  79.         function drop(e){
  80.             e.preventDefault();
  81.             $("#drophere").removeClass("pink").addClass("yellow");
  82.             var data = e.dataTransfer;
  83.             var files = data.files;
  84.             if( ! files ) return;
  85.             var file = files[0];
  86.             if( file.type == "image/jpeg" || file.type == "image/gif" || file.type == "image/png" ) {
  87.                 view(file);
  88.             }
  89.         }
  90.  
  91.         function view(file){
  92.             if( ! file ) return;
  93.             var reader = new FileReader();
  94.             reader.addEventListener("load",function(){
  95.                 var image = new Image();
  96.                 var ww, hh, ml, mt;
  97.                 image.onload = function(){
  98.                     var w = this.width;
  99.                     var h = this.height;
  100.                     var imginfo = getXY(500, w, h);
  101.                     $(this).attr("width",imginfo.width);
  102.                     $(this).attr("height",imginfo.height);
  103.                     $(this).css({"margin-left":imginfo.leftmargin + "px", "margin-top": imginfo.topmargin + "px"});
  104.                     $("#drophere").html(this);
  105.                     var xbtn = "<div class='xbtn'>X</div>";
  106.                     $("#drophere").append(xbtn);
  107.                 }
  108.                 image.src = reader.result;
  109.             });
  110.             reader.readAsDataURL(file);
  111.         }
  112.  
  113.         $("#drophere").on("click",".xbtn",function(){
  114.             $("#drophere").html("drop images here!");
  115.         });
  116.  
  117.         function dragover(e){
  118.             e.preventDefault();
  119.             $("#drophere").removeClass("yellow").addClass("pink");
  120.         }
  121.         function dragleave(e){
  122.             e.preventDefault();
  123.             $("#drophere").removeClass("pink").addClass("yellow");
  124.         }
  125.  
  126.         function getXY(maxsize, w, h){
  127.                     var imginfo = {};
  128.                     if( w > maxsize || h > maxsize ) {
  129.                         if( w > h ) {
  130.                             ww = maxsize;
  131.                             hh = (maxsize*h)/w;
  132.                             ml = 0;
  133.                             mt = (maxsize/2)-(hh/2);
  134.                         } else if( h > w ){
  135.                             hh = maxsize;
  136.                             ww = (maxsize*w)/h;
  137.                             ml = (maxsize/2)-(ww/2);
  138.                             mt = 0;
  139.                         } else {
  140.                             ww = maxsize;
  141.                             hh = maxsize;
  142.                             ml = 0;
  143.                             mt = 0;
  144.                         }
  145.                     } else {
  146.                         ww = w;
  147.                         hh = h;
  148.                         ml = (maxsize/2)-(ww/2);
  149.                         mt = (maxsize/2)-(hh/2);
  150.                     }  
  151.                     imginfo.width = ww;
  152.                     imginfo.height = hh;
  153.                     imginfo.leftmargin = ml;
  154.                     imginfo.topmargin = mt;
  155.                     return imginfo;
  156.         }
  157.  
  158.  
  159.  
  160.         $(".btn-submit").on("click",function(e){
  161.             e.preventDefault();
  162.             var data = {};
  163.             data.src = $("#drophere img").attr("src");
  164.             data.title = $("#imgtitle").val();
  165.             if( ! data.src || ! data.title ) return;
  166.             var image = new Image();
  167.             image.onload = function(){
  168.                 var imginfo = getXY(150, this.width, this.height);
  169.                 var img = "<img src='" + data.src + "' width='" + imginfo.width + "' height='" + imginfo.height + "'>";
  170.                 var html = "<div class='pbox'>";
  171.                 html += img;
  172.                 html += "<p>" + data.title + "</p>";
  173.                 html +="</div>";
  174.  
  175.                 $("#photolist").append(html);
  176.                 data.width = imginfo.width;
  177.                 data.height = imginfo.height;
  178.                 photos.push(data);
  179.                 localStorage.setItem("photo",JSON.stringify(photos) );
  180.  
  181.             };
  182.             image.src = data.src;
  183.         });
  184.  
  185.     </script>
  186. </body>
  187. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement