Skip to content

Commit 2578f65

Browse files
Merge pull request #367 from ProgramEquity/ahn-nath/crop-representatives-photo
Ahn nath/crop representatives photo: [Low Priority] Crop representative photos using Cicero API data
2 parents 95b3b56 + baaa1e5 commit 2578f65

2 files changed

Lines changed: 76 additions & 3 deletions

File tree

server/routes/api/representatives.js

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
132203
function getOfficialSocialMediaPages(identifiers) {
133204
var social_media_pages = []
134205

src/components/RepresentativeCard.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232
</div>
3333

3434
<v-img
35-
class="mx-auto rep-img"
35+
class="mx-auto text-align-left rep-img"
36+
v-bind="member"
3637
:src="member.photoUrl"
3738
height="75"
3839
width="75"
40+
:position="member.photoCroppingCSS"
3941
/>
4042
<v-card-subtitle class="text-center rep-img" v-text="member.address_city" />
4143
</v-card>

0 commit comments

Comments
 (0)