From 8fdd0a1da888418ab89ae1f525704fd0f3c65d81 Mon Sep 17 00:00:00 2001 From: Saif Sultan Date: Thu, 21 Nov 2024 23:44:10 +0530 Subject: [PATCH] `gpfup-limit-aspect-ratio-of-mp4-mov-files.js`: Added snippet to limit Aspect Ratio of uploaded video files. --- ...fup-limit-aspect-ratio-of-mp4-mov-files.js | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 gp-file-upload-pro/gpfup-limit-aspect-ratio-of-mp4-mov-files.js diff --git a/gp-file-upload-pro/gpfup-limit-aspect-ratio-of-mp4-mov-files.js b/gp-file-upload-pro/gpfup-limit-aspect-ratio-of-mp4-mov-files.js new file mode 100644 index 00000000..93d48754 --- /dev/null +++ b/gp-file-upload-pro/gpfup-limit-aspect-ratio-of-mp4-mov-files.js @@ -0,0 +1,38 @@ +/** + * Gravity Perks // File Upload Pro // Limit Aspect Ratio of MP4/MOV Files + * https://gravitywiz.com/documentation/gravity-forms-file-upload-pro/ + * + * Instruction Video: https://www.loom.com/share/b05a322bbf204e49b23ae366a123be96 + * + * Instructions: + * 1. Install our free Custom Javascript for Gravity Forms plugin. + * Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/ + * 2. Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin. + */ +window.gform.addAction('gpfup_before_upload', (formId, fieldId, file, up, gpfupInstance) => { + // Update "4:3" to the desired video aspect ratio. + const targetAspectRatio = '4:3'; + + const [width, height] = targetAspectRatio.split(':').map(Number); + const numericalAspectRatio = width / height; + + const videoMimeTypes = ['video/mp4', 'video/quicktime']; + + if (videoMimeTypes.indexOf(file.type) !== -1) { + var fileURL = URL.createObjectURL(file.getNative()); + var vid = document.createElement('video'); + vid.src = fileURL; + + // check the video aspect ratio + vid.onloadedmetadata = function () { + const aspectRatio = this.videoWidth / this.videoHeight; + + if (numericalAspectRatio != aspectRatio) { + gpfupInstance.handleFileError(up, file, { + code: 'does_not_meet_aspect_ratio', + message: `Video duration must be of ${targetAspectRatio} aspect ratio.`, + }); + } + }; + } +});