Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to make multiple instance ? #69

Open
spham opened this issue Sep 27, 2019 · 2 comments
Open

how to make multiple instance ? #69

spham opened this issue Sep 27, 2019 · 2 comments

Comments

@spham
Copy link

spham commented Sep 27, 2019

i wish multiple instance (4) from croppies in same page.
and wish upload all images loaded.

@pintoflager
Copy link

pintoflager commented Oct 10, 2019

This is what I've come up with since yesterday, you need to modify and change vuetify components to what you use e.g. input, button..
Check out axios for example to upload. Or upload files with the form submit.

<template>
<layout>
    <template v-slot:content>
        <v-row cols="12" class="mt-4 mb-8">
            <template v-for="image in loaded">
                <v-col v-if="image.active" cols="12" sm="6" md="4" xl="3" align-self="center">
                    <vue-croppie
                        :key="image.index"
                        :ref="'croppie' + image.index"
                        :boundary="{ width: 300, height: 300}"
                        :viewport="{ width: 230, height: 230 , type: 'circle'}"
                        :enableResize="false"
                        :enableOrientation="true"
                    >
                    </vue-croppie>
                    <v-icon @click="rotate(image.index, 90)">{{ icon.rotate }}</v-icon>
                    <v-icon @click="remove(image.index)">{{ icon.remove }}</v-icon>

                </v-col>
            </template>
        </v-row>

        <v-row cols="12" align="center" justify="center" class="mt-3 mb-4">
            <v-col cols="12" sm="6">
                <v-file-input
                    multiple
                    accept="image/jpeg, image/png"
                    @change="manager"
                    color="primary"
                    label="File Upload"
                    v-model="files"
                    placeholder="Add files"
                >
                </v-file-input>
            </v-col>
        </v-row>

        <v-btn type="button" @click.stop="crop">crop</v-btn>

        <!-- Live preview of cropped images
        <template v-for="image in cropped">
            <img :key="image.index" v-bind:src="image.file">
        </template>
        -->

    </template>
</layout>

</template>

<script>
import Layout from '@/Shared/Layout'
import { VFileInput } from 'vuetify/lib'

import { mdiTrashCanOutline, mdiRotateRight } from '@mdi/js'

import VueCroppie from 'vue-croppie';
import 'croppie/croppie.css'
import Vue from 'vue'
Vue.use(VueCroppie);

export default {
    components: {
        Layout,
        VFileInput
    },
    data: function() {
        return {
            files: [],
            loaded: [],
            cropped: [],
            icon: {
                remove: mdiTrashCanOutline,
                rotate: mdiRotateRight
            }
        }
    },
    methods: {
        manager() {
            this.files.forEach(file => {
                this.loaded.push({
                    index: this.loaded.length + 1,
                    active: true,
                    loaded: false,
                    file: file
                })
            })
            this.files = []
            this.$nextTick(function () {
                this.init()
            })
        },
        init() {
            this.loaded.forEach(image => {
                if (!image.loaded) {
                    this.$refs['croppie' + image.index][0].bind({
                        url: window.URL.createObjectURL(image.file)
                    })
                    image.loaded = true
                }
            })
        },
        rotate(cid, degrees) {
            this.$refs['croppie' + cid][0].rotate(degrees)
        },
        crop() {
            let options = {
                format: 'png',
                circle: true
            }

            // Loop through loaded, override edits push new.
            this.loaded.forEach(image => {
                if (image.active) {
                    this.$refs['croppie' + image.index][0].result(options, (output) => {
                        if (this.cropped.find(item => item.index === image.index)) {
                            // Photo in loaded with current index is in cropped => Edit.
                            for (let i = 0; i < this.cropped.length; i++) {
                                if (this.cropped[i].index === image.index) {
                                    this.cropped[i] = {
                                        index: image.index,
                                        file: output
                                    }
                                    // For some reason cropped[i] = {edited} is not updating
                                    // preview if you want 'live' preview of cropped images use this:

                                    // this.cropped.splice(i, 1)
                                    // this.cropped.push({
                                    //     index: image.index,
                                    //     file: output
                                    // })
                                }
                            }
                        } else {
                            // Photo index not found in cropped => Add new.
                            this.cropped.push({
                                index: image.index,
                                file: output
                            })
                        }
                    })
                }
            })
        },
        remove(index) {
            // Destroy coppie.
            this.$refs['croppie' + index][0].destroy()

            // Remove from display.
            for (let i = 0; i < this.loaded.length; i++) {
                if (this.loaded[i].index === index) {
                    this.loaded[i].active = false

                }
            }

            // Remove from cropped.
            for (let i = 0; i < this.cropped.length; i++) {
                if (this.cropped[i].index === index) {
                    this.cropped.splice(i, 1)
                }
            }
        }
    }
}
</script>

@spham
Copy link
Author

spham commented Oct 23, 2019

it seems not work. https://jsfiddle.net/spham92/mybutwgo/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants