2016-12-31 11 views
2

私はユーザーのプロフィール画像が必要なフォームを作成しています。画像は円形または長方形のフォームにする必要があります。デフォルトでは画像の領域は黒です。ユーザがその領域をクリックすると、画像を選択することができる。 Facebook、Twitterのプロフィール写真アップローダー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

それは、単一の考えで答えることができない1ステップのプロセス – mike510a

+0

いいえ、私はちょうどtwitterとfacebookの例を挙げています。私は画像のアップローダを望みます。 – Nakhhhh

+0

これはまた、複数のステップのプロセスです - バックエンドの実装が必要ですPHPやその他のサーバーサイド言語で写真を保存することができます。これは、FORMまたは 'ajax'経由でバックエンドに提出する必要があります。次に、' database'をすべて保存する必要があります。別のユーザプロファイル – 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); 
}); 

効率ノート:あなたはURLとして読み取りデータの代わりにcreateObjectURLを使用する場合は、それがより効率的に行うことができますまた、ユーザーにアップロードされた画像のプレビューを与えることができます。

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

あなたがここにMDNで見ることができるように、URL.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%; 
} 

このスニペットは一緒にすべての3つのステップを置く

完全なソリューション:inputタグを介して画像を選択しながら、デフォルトの画像とは別に

$("#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

画像をアップロードできません – 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 ] 
 
    });

フィドルリンク:あなたは、Twitter/FacebookのAPIを調査して行う方法を正確に把握する必要がありますhttps://jsfiddle.net/7ao1qxLe/1/

+0

うまく動作していません – Nakhhhh

+0

それはうまく動作しています:https://jsfiddle.net/7ao1qxLe/1/ –

+0

あまりにも助けてくれてありがとうございます。 – 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>

関連する問題