在HTML和CSS表单中添加个人资料图片

12
我正在创建一个表单,需要用户的个人资料照片,并希望该图片是圆形或矩形的。默认情况下,图片区域应为黑色或虚拟图像。当用户点击该区域时,允许其选择一张图片,就像在 Facebook 或 Twitter 上上传个人资料照片一样。

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>

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/


你需要研究 Twitter/Facebook 的 API 并找出如何准确地实现它,这是一个多步骤的过程,无法用一句话回答。 - mike510a
不,我只是给你举了Twitter和Facebook的例子,我想要图片上传器。 - Nakhhhh
这也是一个多步骤的过程 - 您需要使用PHP或其他服务器端语言实现后端来存储照片...这将需要通过FORM或ajax提交到后端,然后您还可能需要一个数据库来存储所有不同的用户配置文件。 - mike510a
是的,但兄弟我只需要设计,我是PHP和MySQL专家。 - Nakhhhh
所以,我想问您是否能帮助我提供仅个人资料上传器的设计? - Nakhhhh
显示剩余3条评论
4个回答

24

您可以做的是隐藏输入,并在单击个人资料图像时表现得好像已单击该输入

$("#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]) );
    }
}

正如您在MDN上看到的那样,URL.createObjectUrl将仅为文件生成URL,而无需将其加载到DOM中,这对于大型文件肯定更可取。

圆形图片剪裁

要在圆形中显示已剪裁的图像,您需要为其提供一个外部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>


兄弟,它正在显示默认图片,但它无法显示对话框窗口,在其中我可以选择图片。 - Nakhhhh
选择图像后,该图像保持不变,显示默认图像。 - Nakhhhh
1
尝试一下我刚刚添加到答案中的完整解决方案。第一个代码片段仅用于文件上传触发。最终解决方案包含预览。 - Martin Zikmund
你能帮我解决这个问题吗?谢谢! - Nakhhhh
优秀的代码.. 刚在我的网站上使用,运行完美无缺!! - Durairaj Veera Sinnaiah
如何在服务器端访问所选文件名。 - Mohd Kashif

3

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


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 ]
    });

JSFiddle链接: https://jsfiddle.net/7ao1qxLe/1/


0

你可以使用以下代码来应用此图片选择器

<img id="idcardfront" src="images/UploadImgPlaceholder.png" data-type="editable" />
<script>
$("#idcardfront").each(function (i, e) {
var _inputFile = $('<input/>')
.attr('type', 'file')
.attr('hidden', 'hidden')
.attr('id', 'idCardFrontFile')
.attr('onchange', 'readImage()')
.attr('data-image-placeholder', e.id)
.attr('Class', 'hidden');
 $(e.parentElement).append(_inputFile);
 $(e).on("click", _inputFile, triggerClick);
 });
</script>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接