<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!------ Include the above in your HEAD tag ---------->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>File input multiple show image and change rotate</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
<style type="text/css">
img {
transform-origin: top left; /* IE 10+, Firefox, etc. */
-webkit-transform-origin: top left; /* Chrome */
-ms-transform-origin: top left; /* IE 9 */
/*transition-duration: 0.8s;*/ /* for animation */
}
img.rotate90 {
transform: rotate(90deg) translateY(-100%);
-webkit-transform: rotate(90deg) translateY(-100%);
-ms-transform: rotate(90deg) translateY(-100%);
}
img.rotate180 {
transform: rotate(180deg) translate(-100%,-100%);
-webkit-transform: rotate(180deg) translate(-100%,-100%);
-ms-transform: rotate(180deg) translateX(-100%,-100%);
}
img.rotate270 {
transform: rotate(270deg) translateX(-100%);
-webkit-transform: rotate(270deg) translateX(-100%);
-ms-transform: rotate(270deg) translateX(-100%);
}
.preview {
float: left;
}
.colorblack{
color: black !important;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<h1><a href="https://dante.az/">Dante</a> - file input multiple show image and change rotate</h1>
<form>
<div class=" col-sm-12">
<input type="file" id="fileupload" onchange="readURL(this);" multiple="multiple" required>
<div id="dvPreview" class="row"></div>
</div>
</form>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script type="text/javascript">
$(function () {
var filelenht;
var filename;
$("#fileupload").change(function () {
if (typeof (FileReader) != "undefined") {
var dvPreview = $("#dvPreview");
dvPreview.html("");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
$($(this)[0].files).each(function () {
var file = $(this);
if (regex.test(file[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
filelenht = e.target.result.length;
filename = e.target.result.substring(filelenht - 25,filelenht - 2);
filename=filename.replace(/[_\W]+/g, "");
var img = $("<img />");
img.attr("id",filename);
img.attr("class","col-sm-3");
img.attr("style", "max-height:120px !important;max-width: 120px !important;min-height:120px !important;min-width: 120px !important;margin-top:3%;");
img.attr("src", e.target.result);
var btn = $("<div></div>");
btn.attr("class","btn btn-default rotateRight");
btn.attr("fileid",filename);
btn.attr("angle", "0");
btn.attr("filename", e.target.result);
var i = $("<i></i> <br>");
i.attr("class","fas fa-retweet fa-xs colorblack");
i.attr("aria-hidden", "true");
i.attr("file_id",filename);
var idelete = $("<i></i>");
idelete.attr("onclick",filename);
idelete.attr("class","far fa-trash-alt fa-xs colorblack");
idelete.attr("aria-hidden", "true");
idelete.attr("file_id",filename);
dvPreview.append(img);
dvPreview.append(btn.append(i),btn.append(idelete));
}
reader.readAsDataURL(file[0]);
} else {
alert(file[0].name + " düzgün şəkil formatında deyil.");
dvPreview.html("");
return false;
}
});
} else {
alert("Browser html5 readeri dəstəkləmir.");
}
});
});
$(document).ready(function () {
$('#dvPreview').on('click', '.rotateRight', function () {
let file_id=$(this).attr('fileid');
let imgPrew = $(this).parent().find('#'+file_id);
let imgPrewWidth;
let imgPrewHeight;
// let imgPrewWidth = imgPrew.width();
// let imgPrewHeight = imgPrew.height();
let imgPrewContainer = imgPrew.parent();
let currentAngle = this.getAttribute('angle');
let angle = (+currentAngle + 90) % 360;
imgPrew.attr("class", "rotate" + angle);
$(this).attr("angle", angle);
if (angle == 90 || angle == 270) {
imgPrewContainer.animate({'height': imgPrewWidth + 'px'}, 100);
}
if (angle == 0 || angle == 180 || angle==360) {
imgPrewContainer.animate({'height': imgPrewHeight + 'px'}, 100);
}
let hiddenInput = $(this).next();
let filename = this.getAttribute('filename');
let rotateObject = {
angle: this.getAttribute('angle'),
fileid: this.getAttribute('fileid'),
filename: this.getAttribute('filename'),
};
if (angle == '0') { hiddenInput.val(''); }else{ hiddenInput.val(JSON.stringify(rotateObject)); }
});
});
</script>
</body>
</html>