-
Notifications
You must be signed in to change notification settings - Fork 15
/
cropper.js
169 lines (150 loc) · 5.96 KB
/
cropper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { Platform, Dimensions } from 'react-native'
import RNVideoHelper from 'react-native-video-helper';
import ImageEditor from '@react-native-community/image-editor'
const { width } = Dimensions.get('window')
export const VidoUri = (video) => {
let videoUri = video.uri;
if (Platform.OS === "ios" && !videoUri.includes("file:///")) {
const appleId = video.uri.substring(5, 41);
const fil = video.filename.split('.');
const ext = fil[1];
videoUri = `assets-library://asset/asset.${ext}?id=${appleId}&ext=${ext}`;
}
return videoUri;
}
const getPercentFromNumber = (percent, numberFrom) => {
return (numberFrom / 100) * percent;
};
const getPercentDiffNumberFromNumber = (number, numberFrom) => { return (number / numberFrom) * 100; };
const setSize = (image) => {
const imageWidth = image.width;
const imageHeight = image.height;
const cropWidth = width;
const cropHeight = width;
const areaWidth = cropWidth;
const areaHeight = cropHeight;
const srcSize = { width: imageWidth, height: imageHeight };
const fittedSize = { width: 0, height: 0 };
let scale = 1;
if (imageWidth > imageHeight) {
const ratio = width / imageHeight;
fittedSize.width = imageWidth * ratio;
fittedSize.height = width;
} else if (imageWidth < imageHeight) {
const ratio = width / imageWidth;
fittedSize.width = width;
fittedSize.height = imageHeight * ratio;
} else if (imageWidth === imageHeight) {
fittedSize.width = width;
fittedSize.height = width;
}
if (areaWidth < areaHeight || areaWidth === areaHeight) {
if (imageWidth < imageHeight) {
if (fittedSize.height < areaHeight) {
scale = Math.ceil((areaHeight / fittedSize.height) * 10) / 10;
} else {
scale = Math.ceil((areaWidth / fittedSize.width) * 10) / 10;
}
} else {
scale = Math.ceil((areaHeight / fittedSize.height) * 10) / 10;
}
}
scale = scale < 1 ? 1 : scale;
return {
scale,
srcSize,
fittedSize,
};
};
export const ImageCropper = async (params) => {
var positionX = params.cropperParams.positionX, positionY = params.cropperParams.positionY, scale = params.cropperParams.scale, image = params.image, cropSize = params.cropSize, cropAreaSize = params.cropAreaSize;
if (!positionX) positionX = 0;
if (!positionY) positionY = 0;
const exp = setSize(image);
var srcSize = exp.srcSize, fittedSize = exp.fittedSize;
var offset = {
x: 0,
y: 0,
};
var cropAreaW = cropAreaSize ? cropAreaSize.width : width;
var cropAreaH = cropAreaSize ? cropAreaSize.height : width;
var wScale = cropAreaW / scale;
var hScale = cropAreaH / scale;
var percentCropperAreaW = getPercentDiffNumberFromNumber(wScale, fittedSize.width);
var percentRestW = 100 - percentCropperAreaW;
var hiddenAreaW = getPercentFromNumber(percentRestW, fittedSize.width);
var percentCropperAreaH = getPercentDiffNumberFromNumber(hScale, fittedSize.height);
var percentRestH = 100 - percentCropperAreaH;
var hiddenAreaH = getPercentFromNumber(percentRestH, fittedSize.height);
var x = hiddenAreaW / 2 - positionX;
var y = hiddenAreaH / 2 - positionY;
offset.x = x <= 0 ? 0 : x;
offset.y = y <= 0 ? 0 : y;
var srcPercentCropperAreaW = getPercentDiffNumberFromNumber(offset.x, fittedSize.width);
var srcPercentCropperAreaH = getPercentDiffNumberFromNumber(offset.y, fittedSize.height);
var offsetW = getPercentFromNumber(srcPercentCropperAreaW, srcSize.width);
var offsetH = getPercentFromNumber(srcPercentCropperAreaH, srcSize.height);
var sizeW = getPercentFromNumber(percentCropperAreaW, srcSize.width);
var sizeH = getPercentFromNumber(percentCropperAreaH, srcSize.height);
offset.x = Math.floor(offsetW);
offset.y = Math.floor(offsetH);
var cropData = {
offset: offset,
size: {
width: Math.round(sizeW),
height: Math.round(sizeH),
},
displaySize: {
width: Math.round(cropSize.width),
height: Math.round(cropSize.height),
},
};
return await ImageEditor.cropImage(image.uri, cropData);
};
export const HandleCrop = async (param) => {
const { selected, photos, cropperParams, cropSize, HEADER_MAX_HEIGHT } = param;
let { videoMaxLen, videoQuality } = param;
videoMaxLen = videoMaxLen ? videoMaxLen : 0;
videoQuality = videoQuality ? videoQuality : "low"
const cropAreaSize = {
width: HEADER_MAX_HEIGHT,
height: HEADER_MAX_HEIGHT,
};
try {
let result = [];
await Promise.all(selected.map(async t => {
const image = photos[t].node.image;
const videoUri = VidoUri(image);
if (image.playableDuration > 0) {
try {
const vr = await RNVideoHelper.compress(videoUri, {
startTime: 0,
endTime: videoMaxLen,
quality: videoQuality,
defaultOrientation: 0
});
result.push({ type: 'video', assest: vr })
} catch (error) {
console.log(error);
}
}
else {
const crpIndex = cropperParams.findIndex(t => t.filename == image.filename);
let crp = { scale: 1, positionX: 0, positionY: 0 };
if (crpIndex !== -1) {
crp = cropperParams[crpIndex].cropperParams
}
const eresult = await ImageCropper({
cropperParams: crp,
image: image,
cropSize,
cropAreaSize,
});
result.push({ type: 'photo', assest: eresult })
}
}))
return result;
} catch (error) {
console.log(error);
}
};