0
私は画像アップロードのサインアップフォームを持っています。ユーザーが画像を選択すると、画像のプレビューが表示されます。私はそれが正方形に見えるようにしたい。これは、画像が横長の場合はうまく動作しますが、縦長の画像は正方形ではありません。CSSスクエアアップロード前のサムネイルプレビュー
画像がポートレートの場合、プレビュー画像(.portrait)にクラスを追加したいと思います。そして、ユーザーが画像をランドスケープに変更することを決定した場合、このクラスを削除します。
このコードは変更されるクラスを除いて問題なく動作しています。
ありがとうございます。
function readURL(input) {
if (input.files && input.files[0]) {
var i;
for (i = 0; i < input.files.length; ++i) {
var reader = new FileReader();
reader.onload = function(e) {
$('#profile-image-preview').html('<img src="' + e.target.result + '">');
}
reader.readAsDataURL(input.files[i]);
}
}
}
function imageOrientation() {
$('#profile-image-preview img').each(function() {
if ($(this).width() > $(this).height()) {
$(this).addClass('landscape');
} else {
$(this).removeClass('landscape');
}
});
};
$("#imageUpload").change(function() {
readURL(this);
imageOrientation();
});
#profile-image-preview {
position: relative;
width: 150px;
height: 150px;
overflow: hidden;
}
#profile-image-preview img {
position: absolute;
left: 50%;
top: 50%;
height: 100%;
width: auto;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
#profile-image-preview img.portrait {
width: 100%;
height: auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="file" id="imageUpload" style="margin: 15px 0; display: block;" />
<div id="profile-image-preview"></div>
@SSAM問題なし:) –