如何用easy-crop创建模态以形式上载React

发布于 2025-02-10 08:39:00 字数 3041 浏览 2 评论 0原文

我正在尝试使用React-Easy-crop裁剪图像,然后通过形式将其上传到Django Rest API。我是React的新手,并且我看到的教程仅显示一个图像,而我想使其适用于形式的3个输入字段。我有一个带有所有农作物工具的儿童组成部分:

export function CropEasy(myImage, setOpenCrop) {
    let [crop, setCrop] = useState({ x: 0, y: 0 })
    let [zoom, setZoom] = useState(1)
    let [rotation, setRotation] = useState(0)
    let [croppedAreaPixels, setCroppedAreaPixels] = useState(null)
    let cropComplete = (croppedArea, croppedAreaPixels) => {
        setCroppedAreaPixels(croppedAreaPixels)

    }
    let cropImage = async () => {

    }
    return (

        <div>
            <DialogContent dividers
                sx={{
                    backgroud: "#333",
                    position: 'relative',
                    height: 400,
                    width: "auto",
                    minWidth: { sm: 500 }
                }}
            >
                <Cropper
                    image={myImage}
                    crop={crop}
                    zoom={zoom}
                    rotation={rotation}
                    aspect={16 / 9}
                    onZoomChange={setZoom}
                    onRotationChange={setRotation}
                    onCropChange={setCrop}
                    onCropComplete={cropComplete}
                />
            </DialogContent>
            <DialogActions sx={{ flexDirection: "column", mx: 3, my: 2 }}>
                <Box sx={{
                    display: 'flex',
                    gap: 2,
                    flexWrap: "wrap"
                }}>
                    <Button
                        variant="outlined"
                        startIcon={<Cancel />}
                        onClick={() => setOpenCrop('d-none')}
                    >
                        Cancel
                    </Button>

                    <Button
                        variant="contained"
                        startIcon={<CropIcon />}
                        onClick={cropImage}
                    >
                        Crop
                    </Button>
                </Box>
            </DialogActions>
        </div>
    )
}

我的问题是我不确定如何将其连接到我的输入,以便选择图像并弹出裁剪,然后我裁剪图像,然后是保留在表格的输入字段中,准备与裁剪图像一起提交。输入看起来像&lt; input type ='file'c​​actect =“ image/*” onChange = {(e)=&gt; imageHandler(e)}名称=“ Image-1” /&gt; < /code> 使用该功能:

let imageHandler = (e) => {
        if (e.target.files && e.target.files.length >0) {//chekck if the file is empty 
            let file = e.target.files[0]
            let reader = new FileReader()
            reader.readAsDataURL(file)
            reader.addEventListener("load", ()=>{
                //setFile(reader.result)
                setPhotoURL(reader.result)
            })
            console.log("image loaded")
            setOpenCrop('d-block')
        }
    }

表格是父组件,而农作物组件是我知道不是最佳方法的子组件。我只是不知道如何正确连接它。到现在为止原因。我希望一些指导,因为我知道我的方法可能无法正常工作。太感谢了。

I am trying to use react-easy-crop to crop an image and then upload it to an Django rest api through a form. I am new to react and the tutorials I have seen only show for one image while I want to make it work for 3 input fields in the form. I have a separate react child component with all the cropper tools:

export function CropEasy(myImage, setOpenCrop) {
    let [crop, setCrop] = useState({ x: 0, y: 0 })
    let [zoom, setZoom] = useState(1)
    let [rotation, setRotation] = useState(0)
    let [croppedAreaPixels, setCroppedAreaPixels] = useState(null)
    let cropComplete = (croppedArea, croppedAreaPixels) => {
        setCroppedAreaPixels(croppedAreaPixels)

    }
    let cropImage = async () => {

    }
    return (

        <div>
            <DialogContent dividers
                sx={{
                    backgroud: "#333",
                    position: 'relative',
                    height: 400,
                    width: "auto",
                    minWidth: { sm: 500 }
                }}
            >
                <Cropper
                    image={myImage}
                    crop={crop}
                    zoom={zoom}
                    rotation={rotation}
                    aspect={16 / 9}
                    onZoomChange={setZoom}
                    onRotationChange={setRotation}
                    onCropChange={setCrop}
                    onCropComplete={cropComplete}
                />
            </DialogContent>
            <DialogActions sx={{ flexDirection: "column", mx: 3, my: 2 }}>
                <Box sx={{
                    display: 'flex',
                    gap: 2,
                    flexWrap: "wrap"
                }}>
                    <Button
                        variant="outlined"
                        startIcon={<Cancel />}
                        onClick={() => setOpenCrop('d-none')}
                    >
                        Cancel
                    </Button>

                    <Button
                        variant="contained"
                        startIcon={<CropIcon />}
                        onClick={cropImage}
                    >
                        Crop
                    </Button>
                </Box>
            </DialogActions>
        </div>
    )
}

The problem I have is that I am not sure how to connect this to my inputs so that the image is selected and the crop pops up then I crop the image and then it is kept in the form's input field ready to be submitted with the cropped images. The input looks something like this <input type='file' accept="image/*" onChange={(e) => imageHandler(e)} name="image-1" />
With the function:

let imageHandler = (e) => {
        if (e.target.files && e.target.files.length >0) {//chekck if the file is empty 
            let file = e.target.files[0]
            let reader = new FileReader()
            reader.readAsDataURL(file)
            reader.addEventListener("load", ()=>{
                //setFile(reader.result)
                setPhotoURL(reader.result)
            })
            console.log("image loaded")
            setOpenCrop('d-block')
        }
    }

The form is the parent component while the cropper component is the child component which I am aware is not the best approach. I just do not know how to connect it at the moment correctly. So far I tried adding the image to the state of the parent component where the form is and then change the display of the cropper component to block but although the component shows the image does not and it triggers a get request to my Django server for some reason. I would like some guidance as I know my approach probably won't work. Thank you so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文