[如何在使用react-image-crop时缩放图像?还是有其他库可以达到相同目的

问题描述 投票:0回答:1

我必须添加裁剪图像功能。我用react-image-crop裁剪了图像的特定部分。但是现在我需要放大图像,然后裁剪图像的特定缩放部分。如何使用react-image-crop添加缩放功能?还是react-image-crop的替代品?

reactjs zoom crop
1个回答
0
投票
import ReactDOM from 'react-dom'; import React, { PureComponent } from 'react'; import ReactCrop from 'react-image-crop'; import 'react-image-crop/dist/ReactCrop.css'; import './App.css'; class CropImage extends PureComponent { state = { src: null, crop: { unit: '%', width: 30, aspect: 16 / 9, }, }; onSelectFile = e => { if (e.target.files && e.target.files.length > 0) { const reader = new FileReader(); reader.addEventListener('load', () => this.setState({ src: reader.result }) ); reader.readAsDataURL(e.target.files[0]); } }; // If you setState the crop in here you should return false. onImageLoaded = image => { this.imageRef = image; }; onCropComplete = crop => { this.makeClientCrop(crop); }; onCropChange = (crop, percentCrop) => { // You could also use percentCrop: // this.setState({ crop: percentCrop }); this.setState({ crop }); }; async makeClientCrop(crop) { if (this.imageRef && crop.width && crop.height) { const croppedImageUrl = await this.getCroppedImg( this.imageRef, crop, 'newFile.jpeg' ); this.setState({ croppedImageUrl }); } } getCroppedImg(image, crop, fileName) { const canvas = document.createElement('canvas'); const scaleX = image.naturalWidth / image.width; const scaleY = image.naturalHeight / image.height; canvas.width = crop.width; canvas.height = crop.height; const ctx = canvas.getContext('2d'); ctx.drawImage( image, crop.x * scaleX, crop.y * scaleY, crop.width * scaleX, crop.height * scaleY, 0, 0, crop.width, crop.height ); return new Promise((resolve, reject) => { canvas.toBlob(blob => { if (!blob) { //reject(new Error('Canvas is empty')); console.error('Canvas is empty'); return; } blob.name = fileName; window.URL.revokeObjectURL(this.fileUrl); this.fileUrl = window.URL.createObjectURL(blob); resolve(this.fileUrl); }, 'image/jpeg'); }); } render() { const { crop, croppedImageUrl, src } = this.state; return ( <div className="App"> <div> <input type="file" accept="image/*" onChange={this.onSelectFile} /> </div> {src && ( <ReactCrop src={src} crop={crop} ruleOfThirds onImageLoaded={this.onImageLoaded} onComplete={this.onCropComplete} onChange={this.onCropChange} /> )} {croppedImageUrl && ( <img alt="Crop" style={{ maxWidth: '100%' }} src={croppedImageUrl} /> )} </div> ); } } export default CropImage

用于缩放图像的示例:

import Cropper from 'react-easy-crop' import React from "react"; class ZoomAndCropImage extends React.Component { state = { image: 'https://ec.europa.eu/programmes/creative-europe/sites/creative-europe/files/covid19-cdc-unsplash.jpg', crop: {x: 10, y: 10}, zoom: 1, aspect: 4 / 3, }; onCropChange = crop => { this.setState({crop}) }; onCropComplete = (croppedArea, croppedAreaPixels) => { console.log(croppedArea, croppedAreaPixels) }; onZoomChange = zoom => { console.log(zoom, 'zoom'); this.setState({zoom}) }; render() { return ( <div> <Cropper image={this.state.image} crop={this.state.crop} zoom={this.state.zoom} aspect={this.state.aspect} onCropChange={this.onCropChange} onCropComplete={this.onCropComplete} onZoomChange={this.onZoomChange} /> </div> ) } } export default ZoomAndCropImage

© www.soinside.com 2019 - 2024. All rights reserved.