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