|
| 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 | + * Experimental Snippet 🧪 |
| 6 | + * |
| 7 | + * Instruction Video: https://www.loom.com/share/b05a322bbf204e49b23ae366a123be96 |
| 8 | + * |
| 9 | + * Instructions: |
| 10 | + * 1. Install our free Custom Javascript for Gravity Forms plugin. |
| 11 | + * Download the plugin here: https://gravitywiz.com/gravity-forms-code-chest/ |
| 12 | + * 2. Copy and paste the snippet into the editor of the Custom Javascript for Gravity Forms plugin. |
| 13 | + */ |
| 14 | +window.gform.addAction('gpfup_before_upload', (formId, fieldId, file, up, gpfupInstance) => { |
| 15 | + // Update "4:3" to the desired video aspect ratio. |
| 16 | + const targetAspectRatio = '4:3'; |
| 17 | + |
| 18 | + const [width, height] = targetAspectRatio.split(':').map(Number); |
| 19 | + const numericalAspectRatio = width / height; |
| 20 | + |
| 21 | + const videoMimeTypes = ['video/mp4', 'video/quicktime']; |
| 22 | + |
| 23 | + if (videoMimeTypes.indexOf(file.type) !== -1) { |
| 24 | + var fileURL = URL.createObjectURL(file.getNative()); |
| 25 | + var vid = document.createElement('video'); |
| 26 | + vid.src = fileURL; |
| 27 | + |
| 28 | + // check the video aspect ratio |
| 29 | + vid.onloadedmetadata = function () { |
| 30 | + const aspectRatio = this.videoWidth / this.videoHeight; |
| 31 | + |
| 32 | + if (numericalAspectRatio != aspectRatio) { |
| 33 | + gpfupInstance.handleFileError(up, file, { |
| 34 | + code: 'does_not_meet_aspect_ratio', |
| 35 | + message: `Video duration must be of ${targetAspectRatio} aspect ratio.`, |
| 36 | + }); |
| 37 | + } |
| 38 | + }; |
| 39 | + } |
| 40 | +}); |
0 commit comments