{"id":573,"date":"2024-03-22T04:01:34","date_gmt":"2024-03-22T04:01:34","guid":{"rendered":"http:\/\/www.phpgang.com\/?p=573---1e1de008-7f03-4f33-9d3f-b8636a1d29b6"},"modified":"2024-03-22T04:01:34","modified_gmt":"2024-03-22T04:01:34","slug":"how-to-show-image-thumbnail-before-upload-with-jquery","status":"publish","type":"post","link":"https:\/\/www.phpgang.com\/how-to-show-image-thumbnail-before-upload-with-jquery_573.html","title":{"rendered":"How to show image thumbnail before upload with jQuery"},"content":{"rendered":"

Today I am going to show you how to show image thumbnail before uploading image with jQuery, we wrote many tutorials on image upload on PHPGang.com. If you want to give your users an option that they can see image preview before uploading that image to server then you must use this script. We used jQuery to show you preview of thumbnail not complete procedure to upload image using PHP you can read our previous tutorial for upload image to server.<\/p>\n

\"How<\/p>\n

[wpdm_file id=88]DEMO<\/a><\/div>\n

Read More about Image Upload:<\/strong><\/p>\n

How to Upload multiple images jQuery Ajax using PHP
\n<\/a><\/p>\n

How to Upload Image using jQuery with progress in PHP
\n<\/a><\/p>\n

File uploading with PHP<\/a><\/p>\n<\/blockquote>\n

jQuery:<\/strong><\/p>\n

<script type=\"text\/javascript\">\r\n$(function() {\r\n    $(\"#uploadFile\").on(\"change\", function()\r\n    {\r\n        var files = !!this.files ? this.files : [];\r\n        if (!files.length || !window.FileReader) return; \/\/ no file selected, or no FileReader support\r\n\r\n        if (\/^image\/.test( files[0].type)){ \/\/ only image file\r\n            var reader = new FileReader(); \/\/ instance of the FileReader\r\n            reader.readAsDataURL(files[0]); \/\/ read the local file\r\n\r\n            reader.onloadend = function(){ \/\/ set image data as background of div\r\n                $(\"#imagePreview\").css(\"background-image\", \"url(\"+this.result+\")\");\r\n            }\r\n        }\r\n    });\r\n});\r\n<\/script><\/pre>\n

Above jQuery work on onChange<\/em> event of your file upload field and add image in background of give div.<\/p>\n

HTML:<\/strong><\/p>\n

<div id=\"imagePreview\"><\/div>\r\n<input id=\"uploadFile\" type=\"file\" name=\"image\" class=\"img\" \/><\/pre>\n

CSS used for image div in which we preview thumbnail:<\/strong><\/p>\n

<style>\r\n#imagePreview {\r\n    width: 180px;\r\n    height: 180px;\r\n    background-position: center center;\r\n    background-size: cover;\r\n    -webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, .3);\r\n    display: inline-block;\r\n}\r\n<\/style><\/pre>\n

That’s all:<\/strong><\/p>\n

[wpdm_file id=88]DEMO<\/a><\/div>\n

So that’s how you can show image thumbnail before uploading image,I hope you like this tutorial please don’t forget to give us your feedback in comments, you can also download code for free and test it on our demo page.<\/p>\n

Facebook<\/a><\/blockquote><\/div><\/div>
Tutorial Categories:<\/strong>
\n \"jQuery\"<\/a><\/div><\/div>","protected":false},"excerpt":{"rendered":"

Today I am going to show you how to show image thumbnail before uploading image with jQuery, we wrote many tutorials on image upload on PHPGang.com. If you want to give your users an option that they can see image preview before uploading that image to server then you must use this script. We used jQuery to show you preview of thumbnail not complete procedure to upload image using PHP you can read our previous tutorial for upload image to server.<\/p>\n

\"How<\/p>\n","protected":false},"author":1,"featured_media":574,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"twitterCardType":"","cardImageID":0,"cardImage":"","cardTitle":"","cardDesc":"","cardImageAlt":"","cardPlayer":"","cardPlayerWidth":0,"cardPlayerHeight":0,"cardPlayerStream":"","cardPlayerCodec":"","footnotes":""},"categories":[38],"tags":[407,121,408,627,409],"_links":{"self":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts\/573"}],"collection":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/comments?post=573"}],"version-history":[{"count":0,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/posts\/573\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/media\/574"}],"wp:attachment":[{"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/media?parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/categories?post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.phpgang.com\/wp-json\/wp\/v2\/tags?post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}