【发布时间】:2021-04-08 12:59:22
【问题描述】:
????问题
我正在制作一个编辑图像的应用程序,但在应用过滤器后下载图像时我被卡住了。正如预期的那样,用户可以拖放(或仅上传)图像(在 Cloudinary 上创建动态 URL)并将 CSS 过滤器应用于上传的图像。
所以,基本上,我想将图像保存到 Cloudinary API 中,同时还要应用过滤器。
????️ 架构
Edite web app architecture 或更多关于here
⚙️重现问题
您可以通过克隆Edite GitHub repository 并关注the guide 来设置服务来重现此问题。
????代码
注意: 看到src是根
components/Toolbar/Right/options.json
[
{
"property": "brightness",
"value": 100,
"range": {
"min": 0,
"max": 100
},
"unit": "%"
},
{
"property": "contrast",
"value": 100,
"range": {
"min": 0,
"max": 200
},
"unit": "%"
},
{
"property": "saturate",
"value": 100,
"range": {
"min": 0,
"max": 200
},
"unit": "%"
},
{
"property": "grayscale",
"value": 0,
"range": {
"min": 0,
"max": 100
},
"unit": "%"
},
{
"property": "sepia",
"value": 0,
"range": {
"min": 0,
"max": 100
},
"unit": "%"
},
{
"property": "invert",
"value": 0,
"range": {
"min": 0,
"max": 100
},
"unit": "%"
},
{
"property": "hue-rotate",
"value": 0,
"range": {
"min": 0,
"max": 360
},
"unit": "deg"
}
]
components/FileUploader/index.js
function FileUploader() {
// Image uploading states
const dndRef = useRef(); // Access DnD element reference and its current state
const [isDragging, setIsDragging] = useState(false);
const [isUploading, setIsUploading] = useState(false);
const [uploadedImageUrl, setUploadedImageUrl] = useState('');
const [src, { blur }] = useProgressiveImg(
'',
uploadedImageUrl
);
const [uploadedImageName, setUploadedImageName] = useState('image');
// CSS Filters
const { activeTool } = useContext(ToolsContext);
const [options, setOptions] = useState(DEFAULT_OPTIONS);
const selectedFilter = options[activeTool];
// Get the file's data and send to clodinary
const onFileChange = async e => {
let formData = new FormData();
formData.append('file', e.target.files[0]);
formData.append('upload_preset', 'Edite_App');
setIsUploading(true);
let data = await api.post('/image/upload', formData);
const file = data.data;
setIsDragging(false);
setIsUploading(false);
setUploadedImageUrl(file.secure_url);
setUploadedImageName(file.original_filename);
}
// Get slider value according to the tools
const handleSliderChange = ({ target }) => {
setOptions(prevOptions => {
return prevOptions.map((option, index) => {
if (index !== activeTool) return option
return { ...option, value: target.value }
})
})
}
// Get CSS filters and return as a object
const handleImageStyling = async () => {
const filters = options.map(option => {
return `${option.property}(${option.value}${option.unit})`
})
return filters.join(' ');
I }
}
注意:为了更好地解释而不只是给出太多代码,你在上面看到,基本上,我得到了一个 CSS 过滤器列表(JSON 文件),并且还使用 Cloudinary API 来发布数据和创建动态 URL。虽然 Cloudinary 支持图像转换,但我总是卡在这个话题上。
【问题讨论】:
标签: javascript css reactjs web-applications cloudinary