Image Preview (Thumbnails) before uploading – jQuery

If you are creating an image uploader, you can provide thumbnail preview of currently selected images in file input field. We don’t need to upload entire file to server to do that. File API is supported by all modern browsers, we just need to take advantage of this feature, it provides us with information about files and allows to access their content.

Mark UP

Let’s start by dropping HTML code within the body of the page. HTML contains an input field and a DIV element which will output the thumbnails.

HTML
1234

<input type="file" id="file-input" multiple />
<div id="thumb-output"></div>

jQuery

When user selects file for upload, we will make sure user browser supports File API, and then we loop though all queued files reading their contents and appending them to the output element.

JQUERY
123456789101112131415161718192021222324

$(document).ready(function(){
	$('#file-input').on('change', function(){ //on file input change
		if (window.File && window.FileReader && window.FileList && window.Blob) //check File API supported browser
    	{
			$('#thumb-output').html(''); //clear html of output element
			var data = $(this)[0].files; //this file data
			$.each(data, function(index, file){ //loop though each file
				if(/(\.|\/)(gif|jpe?g|png)$/i.test(file.type)){ //check supported file type
					var fRead = new FileReader(); //new filereader
					fRead.onload = (function(file){ //trigger function on successful read
					return function(e) {
						var img = $('<img/>').addClass('thumb').attr('src', e.target.result); //create image element
						$('#thumb-output').append(img); //append image to output element
					};
				  	})(file);
					fRead.readAsDataURL(file); //URL representing the file's data.
				}
			});
		}else{
			alert("Your browser doesn't support File API!"); //if File API is absent
		}
	});
});

CSS

Finally using CSS we need to scale down images output, because the images are still full size and giving them new width or height will resize images to thumbnail version.

CSS
12345

.thumb{
	margin: 10px 5px 0 0;
	width: 100px;
}

Demo

  • 12 Comments

    Add Comment
    • Ravi
      it is not working when file selected from google drive
    • Machasin
      If after this thumbnail i want to upload the image to database, what have i do to do that? please help me
      • Saran Machasin
        You can do that by converting image into base64 or storing them as blobs, but your database size will grow bigger and cause problems over time, hence I suggest storing images in a separate folder. But it's upto you, there are several tutorials on the internet regarding that.
    • Vishu Pandit
      1234567891011121314151617181920212223242526
      &lt;?php
      define(&#039;DB_SERVER&#039;, &#039;localhost&#039;);
      define(&#039;DB_USERNAME&#039;, &#039;sdcm&#039;);
      define(&#039;DB_PASSWORD&#039;, &#039;sdcm@Lalitpur&#039;);
      define(&#039;DB_DATABASE&#039;, &#039;svsacademy&#039;);
      $db = mysqli_connect(&quot;localhost&quot;,&quot;sdcm&quot;,&quot;sdcm@Lalitpur&quot;,&quot;svsacademy&quot;);
      
      
      if(isset($_POST) &amp;&amp; !empty($_FILES[&#039;image&#039;][&#039;name&#039;])){
      	
      
      	$name = $_FILES[&#039;image&#039;][&#039;name&#039;];
      	list($txt, $ext) = explode(&quot;.&quot;, $name);
      	$image_name = &quot;&quot;.&quot;.&quot;.$ext;
      	$tmp = $_FILES[&#039;image&#039;][&#039;tmp_name&#039;];
      
      
      	if(move_uploaded_file($tmp, &#039;upload/&#039;.$image_name)){
      		mysqli_query($db,&quot;INSERT INTO `images` (title)
      		VALUES (&#039;&quot;.$image_name.&quot;&#039;)&quot;);
      		echo &quot;&lt;img width=&#039;200px&#039; src=&#039;upload/&quot;.$image_name.&quot;&#039; class=&#039;preview&#039;&gt;&quot;;
      	}else{
      		echo &quot;image uploading failed&quot;;
      	}
      }
      ?&gt;
      This is my code to upload image it is working but it is not going to database Below is my html code: UPLOAD
    • Crisostomo Ibarra
      It is working perfectly. Thank you. It would be great help
    • Mark
      what library do you use sir?
    • Ajay
      How to put loading? while image is not ready.
    • Gabin
      Wow.. thats working..!! thanks dude.
    • Rock star
      nice work buddy............
    • Test
      I was just Testing