2016-12-31 97 views
2

我正在創建一個我需要的圖片用戶的個人資料圖片,我希望圖片爲圓形或矩形形式,默認情況下,圖像區域爲黑色或虛擬圖片,當用戶點擊該區域時,允許他/她選擇圖像。例如Facebook,Twitter上的Profile Picture Uploader。在HTML和CSS中添加個人資料圖片

Like This

我的形式

HTML

<div class="signup-w3ls"> 
    <div class="signup-agile1"> 
     <form action="#" method="post"> 

      <div class="form-control"> 
       <label class="header">Profile Photo:</label> 

       <input id="image" type="file" name="profile_photo" placeholder="Photo" required="" capture> 
      </div> 

      <div class="form-control"> 
       <label class="header">Store Name :</label> 
       <input type="text" id="store_name" name="store_name" placeholder="Store Name" title="Please enter your First Name" required=""> 
      </div> 

      <div class="form-control"> 
       <label class="header">Store Type :</label> 
       <input type="text" id="store_type" name="store_type" placeholder="Store Type" title="Please enter your Last Name" required=""> 
      </div> 

      <div class="form-control"> 
       <label class="header">Owner Type :</label> 
       <input type="text" id="owner_type" name="owner_type" placeholder="Owner Type" title="Please enter a valid email" required=""> 
      </div> 

      <div class="form-control"> 
       <label class="header">Website :</label> 
       <input type="url" id="website" name="website" placeholder="Website" id="password1" required=""> 
      </div> 

      <div class="form-control"> 
       <label class="header">Contact Number :</label> 
       <input type="text" id="contact_number" name="contact_number" placeholder="Contact Number" required=""> 
      </div> 

      <div class="form-control"> 
       <label class="header">Contact Email :</label> 
       <input type="email" id="contact_email" name="contact_email" placeholder="Contact Email" required=""> 
      </div> 

      <input type="submit" class="register" value="Register"> 
     </form> 

    </div> 
</div> 
</div> 

CSS

.signup-w3ls { 
    width: 50%; 
    margin: 70px 25% 80%; 
    padding: 0; 
    display: table; 
    position: relative; 
} 
.signup-agile1{ 
    width:100%; 
    float:center; 
} 

.signup-w3ls .signup-agile1 .form-control { 
    margin-bottom: 20px; 
} 
label.header { 
    font-size: 16px; 
    font-weight: 500; 
    width: 215px; 
    color: grey; 
    margin-right:10px; 
    text-align:justify; 
    letter-spacing: 1px; 
    text-transform: uppercase; 
    display: inline-block; 
    font-family: 'Nunito', sans-serif; 
} 
input#image,input#store_name, input#store_type,input#owner_type, input#website,input#contact_number,input#contact_email { 
    padding:0 40px; 
    width:40%; 
    height:55px; 
    border: 1px solid #dadada; 
    color: grey; 
    text-align:justify; 
    outline: none; 
    letter-spacing: 1px; 
    font-size: 16px; 
    font-weight:normal; 
    font-family: 'Muli', sans-serif; 
    border-radius:30px; 
    -webkit-border-radius:30px; 
    -moz-border-radius:30px; 
    -o-border-radius:30px; 
    -ms-border-radius:30px; 
} 
input#image:focus,input#store_name:focus, input#store_type:focus,input#owner_type:focus, input#website:focus,input#contact_number:focus,input#contact_email:focus { 
    background-color:#f5f8fa !important; 
    border:1px solid #dadada; 
} 
input::-webkit-input-placeholder { 
color: grey; 
} 
input:-moz-placeholder { /* Firefox 18- */ 
color: grey; 
} 
input::-moz-placeholder { /* Firefox 19+ */ 
color: grey; 
} 
input:-ms-input-placeholder { 
color: grey; 
} 
.register { 
    background-color: lightgreen; 
    width: 52%; 
    height: 55px; 
    border: none; 
    margin-left: 233px; 
    cursor: pointer; 
    color: #fff; 
    outline: none; 
    font-size: 20px; 
    font-weight: normal; 
    text-transform: uppercase; 
    transition: all 0.5s ease-in-out; 
    -webkit-transition: all 0.5s ease-in-out; 
    -moz-transition: all 0.5s ease-in-out; 
    -o-transition: all 0.5s ease-in-out; 
    border-radius: 30px; 
    -webkit-border-radius: 30px; 
    -moz-border-radius: 30px; 
    -o-border-radius: 30px; 
    -ms-border-radius: 30px; 
    font-family: 'Muli', sans-serif; 
} 
.register:hover { 
    background-color:#36b051; 
    color:#fff; 
} 

的jsfiddle: -https://jsfiddle.net/7ao1qxLe/

+0

你必須研究嘰嘰喳喳/ Facebook的API,並弄清楚究竟如何做那是 - 一步到位的過程,無法在一個單一的思路回答 – mike510a

+0

不,我只是給你的twitter和臉譜的例子,我想圖像上傳 – Nakhhhh

+0

這也是一個多步驟的過程 - 你需要有一個後端實現使用PHP或其他服務器端語言來存儲照片...必須先通過FORM或通過'ajax'提交到後端,那麼您可能還需要一個'數據庫'來存儲所有的不同的用戶配置文件 – mike510a

回答

4

你可以做的是隱藏輸入,只是行爲,就好像它被點擊點擊簡介圖像時:

$("#profileImage").click(function(e) { 
 
    $("#imageUpload").click(); 
 
});
#imageUpload 
 
{ 
 
    display: none; 
 
} 
 

 
#profileImage 
 
{ 
 
    cursor: pointer; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<image id="profileImage" src="http://lorempixel.com/100/100" /> 
 
<input id="imageUpload" type="file" 
 
     name="profile_photo" placeholder="Photo" required="" capture>

圖片預覽

你也可以給用戶上傳的圖片的預覽:

function previewProfileImage(uploader) { 
    //ensure a file was selected 
    if (uploader.files && uploader.files[0]) { 
     var imageFile = uploader.files[0]; 
     var reader = new FileReader();  
     reader.onload = function (e) { 
      //set the image data as source 
      $('#profileImage').attr('src', e.target.result); 
     }  
     reader.readAsDataURL(imageFile); 
    } 
} 

$("#imageUpload").change(function(){ 
    previewProfileImage(this); 
}); 

效率注:可以使其更有效率,如果你使用createObjectURL不是讀取數據的URL。

function fasterPreview(uploader) { 
    if (uploader.files && uploader.files[0]){ 
      $('#profileImage').attr('src', 
      window.URL.createObjectURL(uploader.files[0])); 
    } 
} 

正如你可以看到這裏MDNURL.createObjectUrl將剛生成的文件,而不必把它加載到DOM,這絕對是最好大型文件的URL。

圓形圖像裁剪

顯示圖像裁剪成一個圈,你需要給它一個外div和應用border-radius它:

HTML:

<div id="profile-container"> 
    <image id="profileImage" src="aDefaultImage.png" /> 
</div> 

CSS:

#profile-container { 
    width: 150px; 
    height: 150px; 
    overflow: hidden; 
    -webkit-border-radius: 50%; 
    -moz-border-radius: 50%; 
    -ms-border-radius: 50%; 
    -o-border-radius: 50%; 
    border-radius: 50%; 
} 

完整的解決方案

這個片段將所有三個步驟一起:

$("#profileImage").click(function(e) { 
 
    $("#imageUpload").click(); 
 
}); 
 

 
function fasterPreview(uploader) { 
 
    if (uploader.files && uploader.files[0]){ 
 
      $('#profileImage').attr('src', 
 
      window.URL.createObjectURL(uploader.files[0])); 
 
    } 
 
} 
 

 
$("#imageUpload").change(function(){ 
 
    fasterPreview(this); 
 
});
#imageUpload 
 
{ 
 
    display: none; 
 
} 
 

 
#profileImage 
 
{ 
 
    cursor: pointer; 
 
} 
 

 
#profile-container { 
 
    width: 150px; 
 
    height: 150px; 
 
    overflow: hidden; 
 
    -webkit-border-radius: 50%; 
 
    -moz-border-radius: 50%; 
 
    -ms-border-radius: 50%; 
 
    -o-border-radius: 50%; 
 
    border-radius: 50%; 
 
} 
 

 
#profile-container img { 
 
    width: 150px; 
 
    height: 150px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="profile-container"> 
 
    <image id="profileImage" src="http://lorempixel.com/100/100" /> 
 
</div> 
 
<input id="imageUpload" type="file" 
 
     name="profile_photo" placeholder="Photo" required="" capture>

+0

兄弟,它是顯示我的默認圖片,但它不能顯示我的對話窗口,其中我可以選擇圖片 – Nakhhhh

+0

上傳圖片functionallity不工作 – Nakhhhh

+0

現在圖片未更新 – Nakhhhh

1

插入圖像的默認圖像分別標籤然後讀取URL而經由輸入標籤選擇圖像。

<img id="profile" src="default.png" alt="profile-image" /> 

function readURL(input) { 
 
     if (input.files && input.files[0]) { 
 
      var reader = new FileReader(); 
 
      
 
      reader.onload = function (e) { 
 
       $('#profile').attr('src', e.target.result); 
 
      } 
 
      
 
      reader.readAsDataURL(input.files[0]); 
 
     } 
 
    } 
 
    
 
    $("#image").change(function(){ 
 
     readURL(this); 
 
     //other uploading proccess [server side by ajax and form-data ] 
 
    });

小提琴鏈接:https://jsfiddle.net/7ao1qxLe/1/

+0

兄弟不工作 – Nakhhhh

+0

它的工作檢查小提琴:https://jsfiddle.net/7ao1qxLe/1/ –

+0

謝謝tou這麼多的幫助,它的決心。 – Nakhhhh

2

$("#profileImage").click(function(e) { 
 
    $("#imageUpload").click(); 
 
}); 
 

 
function fasterPreview(uploader) { 
 
    if (uploader.files && uploader.files[0]){ 
 
      $('#profileImage').attr('src', 
 
      window.URL.createObjectURL(uploader.files[0])); 
 
    } 
 
} 
 

 
$("#imageUpload").change(function(){ 
 
    fasterPreview(this); 
 
});
#imageUpload 
 
{ 
 
    display: none; 
 
} 
 

 
#profileImage 
 
{ 
 
    cursor: pointer; 
 
} 
 

 
#profile-container { 
 
    width: 150px; 
 
    height: 150px; 
 
    overflow: hidden; 
 
    -webkit-border-radius: 50%; 
 
    -moz-border-radius: 50%; 
 
    -ms-border-radius: 50%; 
 
    -o-border-radius: 50%; 
 
    border-radius: 50%; 
 
} 
 

 
#profile-container img { 
 
    width: 150px; 
 
    height: 150px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="profile-container"> 
 
    <image id="profileImage" src="http://lorempixel.com/100/100" /> 
 
</div> 
 
<input id="imageUpload" type="file" 
 
     name="profile_photo" placeholder="Photo" required="" capture>