@@ -113,8 +113,8 @@ router.get('/:zipCode', async (req, res) => {
113113 photoUrl :
114114 rep . photo_origin_url ||
115115 'https://cdn.pixabay.com/photo/2016/08/08/09/17/avatar-1577909_1280.png' ,
116-
117- socialMediaPages : getOfficialSocialMediaPages ( rep . identifiers ) // call
116+ socialMediaPages : getOfficialSocialMediaPages ( rep . identifiers ) ,
117+ photoCroppingCSS : getPhotoCroppingValues ( rep . photo_cropping )
118118 }
119119
120120 return repInfo
@@ -127,6 +127,77 @@ router.get('/:zipCode', async (req, res) => {
127127 }
128128} )
129129
130+ /*
131+ * Returns string with the following properties:
132+ * - x-value: right or left. Indicates whether the photo should be cropped/positioned from the left or right side.
133+ * - y-value: top or bottom. Indicates whether the photo should be cropped/psoitioned from the top or bottom side.
134+ *
135+ * Example: "top left"
136+ *
137+ * @param {string } photoCropping - The photo cropping object from the CICERO API
138+ * @returns {string } - The photo cropping CSS value
139+ *
140+ *
141+ */
142+ function getPhotoCroppingValues ( photo_cropping_object ) {
143+ // If the photo cropping object is not defined, return default value
144+ if ( ! photo_cropping_object ) {
145+ return 'center center'
146+ }
147+
148+ // args
149+ let x = photo_cropping_object . x
150+ let y = photo_cropping_object . y
151+ let oriHeight = photo_cropping_object . oriHeight
152+ let origWidth = photo_cropping_object . origWidth
153+
154+ // 1. calculate threshold for the x space
155+ // we check if the coordinate starts on the left side of the image (the first half of the left side)
156+ let x_left_threeshold = origWidth / 4
157+ // we check if the coordinate starts on the right side of the image (the first half of the right side) and we reduce a margin of 5% to be flexible
158+ let substract_percentage = 0.05
159+ let x_right_threeshold =
160+ origWidth / 2 - substract_percentage * ( origWidth / 2 )
161+ // 2. calculate threeshold for the y space
162+ // we check if the coordinate starts on the top side of the image (the first half of the top side)
163+ let y_top_threeshold = oriHeight / 4
164+ // we check if the coordinate starts on the bottom side of the image (the first half of the bottom side) and we reduce a margin of 5% to be flexible
165+ let y_bottom_threeshold = oriHeight / 2 - ( 5 / 100 ) * ( oriHeight / 2 )
166+
167+ // 3. determine the css values for the cropping
168+ // if the coordinate does not reach any of the threesholds, we set the value to center
169+ let x_value = 'center'
170+ // left threshold met
171+ if ( x <= x_left_threeshold ) {
172+ x_value = 'left'
173+ }
174+ // right threshold met
175+ if ( x >= x_right_threeshold ) {
176+ x_value = 'right'
177+ }
178+ // we apply the same logic for the y or vertical direction
179+ let y_value = 'center'
180+ // top threshold met
181+ if ( y <= y_top_threeshold ) {
182+ y_value = 'top'
183+ }
184+ // bottom threshold met
185+ if ( y >= y_bottom_threeshold ) {
186+ y_value = 'bottom'
187+ }
188+
189+ // final css value with background position
190+ let css_value = x_value + ' ' + y_value
191+
192+ return css_value
193+ }
194+
195+ /*
196+ * Function used to extract social media pages and format them as JSON objects by using an array from another JSON object.
197+ * @param {array } identifiers - array of identifiers from the JSON object.
198+ * @return {array } socialMediaPages - array of social media pages.
199+ */
200+
130201function getOfficialSocialMediaPages ( identifiers ) {
131202 var social_media_pages = [ ]
132203
0 commit comments