Center Initial Crop

Instructions

  1. Enable Cropping on your File Upload Pro field.
  2. Install this snippet with our free Code Chest plugin. https://gravitywiz.com/gravity-forms-code-chest/

Code

Filename: gpfup-center-initial-crop.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
/**
 * Gravity Perks // File Upload Pro // Center Initial Crop
 * https://gravitywiz.com/documentation/gravity-forms-file-upload-pro/
 *
 * Instructions:
 *
 * 1. Enable Cropping on your File Upload Pro field.
 * 2. Install this snippet with our free Code Chest plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 */
window.gform.addFilter( 'gpfup_cropper_options', function( options ) {
	const previousPosition = options.defaultPosition;
	const previousSize = options.defaultSize;

	options.defaultPosition = function( { imageSize, coordinates } ) {
		// Preserve the position when reopening an image that has already been cropped.
		if (
			previousPosition &&
			(
				previousPosition.left !== 0 ||
				previousPosition.top !== 0 ||
				(
					previousSize &&
					(
						previousSize.width !== imageSize.width ||
						previousSize.height !== imageSize.height
					)
				)
			)
		) {
			return previousPosition;
		}

		return {
			left: ( imageSize.width - coordinates.width ) / 2,
			top: ( imageSize.height - coordinates.height ) / 2,
		};
	};

	return options;
} );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.