Skip to content

Commit 8fdd0a1

Browse files
committed
gpfup-limit-aspect-ratio-of-mp4-mov-files.js: Added snippet to limit Aspect Ratio of uploaded video files.
1 parent 7d2ea71 commit 8fdd0a1

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Gravity Perks // File Upload Pro // Limit Aspect Ratio of MP4/MOV Files
3+
* https://gravitywiz.com/documentation/gravity-forms-file-upload-pro/
4+
*
5+
* Instruction Video: https://www.loom.com/share/b05a322bbf204e49b23ae366a123be96
6+
*
7+
* Instructions:
8+
* 1. Install our free Custom Javascript for Gravity Forms plugin.
9+
* Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/
10+
* 2. Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin.
11+
*/
12+
window.gform.addAction('gpfup_before_upload', (formId, fieldId, file, up, gpfupInstance) => {
13+
// Update "4:3" to the desired video aspect ratio.
14+
const targetAspectRatio = '4:3';
15+
16+
const [width, height] = targetAspectRatio.split(':').map(Number);
17+
const numericalAspectRatio = width / height;
18+
19+
const videoMimeTypes = ['video/mp4', 'video/quicktime'];
20+
21+
if (videoMimeTypes.indexOf(file.type) !== -1) {
22+
var fileURL = URL.createObjectURL(file.getNative());
23+
var vid = document.createElement('video');
24+
vid.src = fileURL;
25+
26+
// check the video aspect ratio
27+
vid.onloadedmetadata = function () {
28+
const aspectRatio = this.videoWidth / this.videoHeight;
29+
30+
if (numericalAspectRatio != aspectRatio) {
31+
gpfupInstance.handleFileError(up, file, {
32+
code: 'does_not_meet_aspect_ratio',
33+
message: `Video duration must be of ${targetAspectRatio} aspect ratio.`,
34+
});
35+
}
36+
};
37+
}
38+
});

0 commit comments

Comments
 (0)