Add a Profile Picture in form in HTML and CSS

前端 未结 4 1975
遇见更好的自我
遇见更好的自我 2020-12-29 12:55

I am creating a form in which I need a Profile picture of the user and I want that the picture is in a circle or a ectangular form. By default the area of image should be bl

相关标签:
4条回答
  • 2020-12-29 13:40

    Insert Image tag separately with default image then read the URL while selecting image through input tag.

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

    Fiddle Link: https://jsfiddle.net/7ao1qxLe/1/

    0 讨论(0)
  • 2020-12-29 13:43

    $("#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 讨论(0)
  • 2020-12-29 13:45

    What you can do is to hide the input and just act as if it was clicked when the profile image is clicked:

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

    Image preview

    You can also give the user a preview of the uploaded image:

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

    Efficiency note: You can make it more efficient if you use createObjectURL instead of reading data as URL.

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

    As you can see here in MDN, the URL.createObjectUrl will just generate the URL for the file instead of having to load it into the DOM, which is definitely preferable for large files.

    Circular image crop

    To display the image cropped in a circle, you will need to give it an outer div and apply border-radius to it:

    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%;
    }
    

    Complete solution

    This snippet puts together all three steps:

    $("#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 讨论(0)
  • 2020-12-29 13:53

    You can use the below code to apply this picture selector

    <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>
    
    0 讨论(0)
提交回复
热议问题