简体   繁体   中英

how to insert user-glyphicon in img tag

The problem is :- In below code I want to make it, if there is no photo uploaded then there will be a photo size bootstrap's glyphicon-user should be placed and when photo uploaded this will overwrite by the uploaded image.

//please add necessary files of bootstrap

<div class="row">
    <div class="row">
        <div class="col-md-4">
            <h4 class="heading">Upload Patient's Photo :-</h4>
        </div>
        <div class="col-md-4">
        <input type="file" class="form-control" name="patientpic" id="patientpic" onchange="readURL(this)"/>
        </div>
    </div>  

//this is the script code for preview image when uploaded.

    <script>
        function readURL(input) {
            if (input.files && input.files[0]) {
            var reader = new FileReader();  
            reader.onload = function (e) {
            $('#patientimg').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
            }
         }
        </script>                               
    </div>

//this is image code here changes takes place to add bootstrap's glyphicon-user

    <div class="col-md-2 col-sm-12">
        <div class="thumbnail">
          <img src="" alt="Select Patient Image" class="img-responsive" id="patientimg" name="patientimg" style="height:140px;width:140px;font-size:30px;text-align:center;color:gray;"/>
        </div>
    </div>  

I tried this but the glyphicon-user in span tag reside below patient image.

just suggestion here, why not convert the bootstrap's glyphicon-user to png then add it on since when the user upload the image it will be appended according to your code.

check a sample here http://jsfiddle.net/ws3bb7sr/1/

  function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { $('#patientimg').attr('src', e.target.result); } reader.readAsDataURL(input.files[0]); } } 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <div class="row"> <div class="row"> <div class="col-md-4"> <h4 class="heading">Upload Patient's Photo :-</h4> </div> <div class="col-md-4"> <input type="file" class="form-control" name="patientpic" id="patientpic" onchange="readURL(this)"/> </div> </div> <div class="col-md-2 col-sm-12"> <div class="thumbnail"> <img src="http://s5.postimg.org/4xpbh5oo3/user_000000_128.png" alt="" class="img-responsive" id="patientimg" name="patientimg" style="height:140px;width:140px;font-size:30px;text-align:center;color:gray;"/> </div> </div> 

Not the best idea, but you can use this code:

<html>
<head>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="//code.jquery.com/jquery-2.1.3.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
#img1 {
    width: 100px;
    height: 100px;
    margin-left: 25px;
}
#img1::before {
    position: absolute;
    font-size: 100px;
}
</style>
<script>
    $(function() {
        setTimeout(function() {
            $('#img1').attr('src', 'http://dummyimage.com/100x100/0ac72a/0011ff');
        }, 2500);
    });
</script>
</head>
<body>
<img src="" id="img1" class="glyphicon glyphicon-asterisk">
</body>
</html>

You might have some problems with transparent png files, so in better check it. If you have some problem with it you can remove the glyphicon classes when you update the src of your image.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM