ldc-legaulois/resources/js/components/bo/ImageInputComponent.vue
Jérémy Munsch 1d10226439
npm i cropperjs
add title to breeding-page
Add image resizer to breeding page
2021-10-10 15:33:44 +02:00

288 lines
12 KiB
Vue

<template>
<div class="form-group ImageInput" :id="id">
<h6>Image</h6>
<span :id="id + 'Span'">Visuel * de type JPEG/PNG/SVG - 2Mo max. - Dimensions largeur {{ width }} px, hauteur {{ height }} px</span>
<div class="input-group mb-3">
<div class="custom-file">
<input ref="actualImage" @change="hasImage = true" :id="id + 'File'" type="file" class="custom-file-input" :name="name" accept="image/png,image/jpeg,.svg" :aria-describedby="id + 'Span'">
<label :for="id + 'File'" class="custom-file-label">Choisir un fichier</label>
</div>
<div class="input-group-append">
<button @click="prepareImageEditor" :disabled="!hasImage" class="btn btn-info" type="button" data-toggle="modal" :data-target="'#'+id+'Modal'">Modifier</button>
</div>
</div>
<br>
<label :for="id+ 'Title'"><small>Titre *</small></label>
<input :id="id+ 'Title'" type="text" class="form-control" :name="nameTitle" placeholder="Titre de l'image" :value="valueTitle">
<br>
<label :for="id+ 'Alt'"><small>Texte alternatif *</small></label>
<input :id="id+ 'Alt'" type="text" class="form-control" :name="nameAlt" placeholder="Texte alternatif à l'image" :value="valueAlt">
<br>
<img v-if="value" :src="value" alt="image">
<div :id="id+'Modal'" class="modal" tabindex="-1" role="dialog" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-xl" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Modification d'image avant import</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="row">
<div class="col d-flex justify-content-center">
<div class="btn-group">
<a class="btn btn-primary" title="Zoom avant" @click="zoom(0.1)">
<i class="fa fa-search-plus"></i>
</a>
<a class="btn btn-primary" title="Zoom arrière" @click="zoom(-0.1)">
<i class="fa fa-search-minus"></i>
</a>
</div>
<div class="btn-group ml-4">
<a class="btn btn-primary" title="Déplacer vers la gauche" @click="move(-10, 0)">
<i class="fa fa-arrow-left"></i>
</a>
<a class="btn btn-primary" title="Déplacer vers la droite" @click="move(10, 0)">
<i class="fa fa-arrow-right"></i>
</a>
<a class="btn btn-primary" title="Déplacer vers le haut" @click="move(0, -10)">
<i class="fa fa-arrow-up"></i>
</a>
<a class="btn btn-primary" title="Déplacer vers le bas" @click="move(0, 10)">
<i class="fa fa-arrow-down"></i>
</a>
</div>
<div class="btn-group ml-4">
<a class="btn btn-primary" title="Mirroir Horizontal" @click="scale(true)">
<i class="fa fa-arrows-alt-h"></i>
</a>
<a class="btn btn-primary" title="Mirroir Vertical" @click="scale(false)">
<i class="fa fa-arrows-alt-v"></i>
</a>
</div>
</div>
</div>
<div class="row">
<div class="col">
<div class="form-group d-flex flex-row">
<div class="rotateButton">
<span class="text-dark">Rotation : </span>
<div class="btn-group">
<a class="btn btn-primary" title="Rotation sens anti-horaire" @click="rotate(-45)">
<i class="fa fa-undo-alt"></i>
</a>
<a class="btn btn-primary" title="Rotation sens horaire" @click="rotate(45)">
<i class="fa fa-redo-alt"></i>
</a>
</div>
</div>
<div class="ml-4 rotateDrag">
<label for="range" class="form-label">{{ rotationText }} degrès</label>
<input ref="rotationSlider" @input="rotationChange($event)" @change="rotateTo($event.target.value)" type="range" class="form-range" id="range" min="0" max="360" value="0">
</div>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-6 p-0">
<img class="imageModification" ref="imageModification" src="">
</div>
<div class="col-6 p-0 bg-light previewIMG">
<div ref="toot" :id="id+'Preview'" :style="previewStyle"></div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button @click="exportToBlob" type="button" class="btn btn-primary">Valider</button>
<button ref="closeModal" type="button" class="btn btn-secondary" data-dismiss="modal">Fermer</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import Cropper from "cropperjs/dist/cropper.js";
export default {
props: [
'id',
'name',
'value',
'nameAlt',
'valueAlt',
'nameTitle',
'valueTitle',
'width',
'height'
],
computed: {
previewStyle() {
return 'width:' + (this.width / 1.5) +'px;height:'+ (this.height / 1.5) +'px;'
}
},
data() {
return {
rotationText: 0,
rotationValue: 0,
scaleX: 1,
scaleY: 1,
hasImage: false,
cropper: null,
cropperOption: {
viewMode: 1, // full crop box
dragMode: 'move',
movable: true,
rotatable: true,
scalable: true,
zoomable: true,
zoomOnTouch: false,
zoomOnWheel: false,
cropBoxMovable: true,
cropBoxResizable: false,
toggleDragModeOnDblclick: false,
minContainerHeight: this.height,
minContainerHeight: this.width,
minCropBoxWidth: this.width,
minCropBoxHeight: this.height,
aspectRatio: this.width / this.height,
preview: '#'+this.id+'Preview',
crop(event) {
// console.log(event.detail.x);
// console.log(event.detail.y);
// console.log(event.detail.width);
// console.log(event.detail.height);
// console.log(event.detail.rotate);
// console.log(event.detail.scaleX);
// console.log(event.detail.scaleY);
},
}
}
},
watch: {
rotationValue(value) {
this.cropper.rotateTo(value)
}
},
methods: {
zoom(v) {
this.cropper.zoom(v)
},
move(x, y) {
this.cropper.move(x, y)
},
scale(horizontal) {
if(horizontal) {
this.scaleX = -1/this.scaleX
this.cropper.scaleX(this.scaleX)
} else {
this.scaleY = -1/this.scaleY
this.cropper.scaleY(this.scaleY)
}
},
rotate(value) {
let res = Number.parseInt(this.rotationValue) + Number.parseInt(value)
res = res < 0 ? 0 : res
res = res > 360 ? 360 : res
this.rotationValue = res
this.rotationText = this.rotationValue
this.$refs.rotationSlider.value = this.rotationValue
},
rotateTo(value) {
value = value < 0 ? 0 : value
value = value > 360 ? 360 : value
this.rotationValue = Number.parseInt(value)
this.rotationText = this.rotationValue
},
rotationChange(event) {
let value = event.target.value
this.rotateTo(value)
},
prepareImageEditor() {
console.log(this.$refs.toot.style)
let image = this.$refs.actualImage.files[0]
this.$refs.imageModification.src = URL.createObjectURL(image)
if(this.cropper) {
this.cropper.destroy()
}
this.cropper = new Cropper(this.$refs.imageModification, this.cropperOption);
window.cropper = this.cropper
},
dataURLtoBlob(dataurl) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {type:mime});
},
exportToBlob() {
let self = this
let originalFile = self.$refs.actualImage.files[0]
let container = new DataTransfer();
let blob = this.dataURLtoBlob(this.cropper.getCroppedCanvas({
width: this.width,
height: this.height,
minWidth: this.width,
minHeight: this.height,
maxWidth: this.width,
maxHeight: this.height,
imageSmoothingEnabled: false,
imageSmoothingQuality: 'high',
}).toDataURL('image/png'))
let file = new File([blob], originalFile.name, {type: 'image/png', lastModified: new Date() })
container.items.add(file)
this.$refs.actualImage.files = container.files
this.$refs.closeModal.click()
}
}
}
</script>
<style lang="scss">
@import "~cropperjs/dist/cropper.css";
.ImageInput {
& > img{
display:block;
max-height:200px;
max-width:100%;
object-fit:contain;
margin:0 auto;
}
/* Ensure the size of the image fit the container perfectly */
.imageModification {
display: block;
/* This rule is very important, please don't ignore this */
max-width: 100%;
}
.rotateButton {
display: flex;
flex-direction: column;
}
.rotateDrag {
display: flex;
flex-direction: column-reverse;
width: 100%;
& > input {
width: 100%;
}
}
.previewIMG {
position: relative;
& > div {
overflow: hidden;
border: 1px solid black;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
}
}
</style>