diff --git a/event_sync/eventbrite_api.go b/event_sync/eventbrite_api.go index 418d7ee..4d52819 100644 --- a/event_sync/eventbrite_api.go +++ b/event_sync/eventbrite_api.go @@ -132,7 +132,10 @@ func createEventbriteImage(event model.ExternalEvent, chapter model.ChapterWithT return "", err } - imageId, err := notifyEventbriteOfNewImage(chapter.EventbriteToken, uploadData.UploadToken) + imageId, err := notifyEventbriteOfNewImage(chapter.EventbriteToken, uploadData.UploadToken, image.Width, image.Height) + if err != nil { + return "", err + } return imageId, nil } @@ -374,7 +377,7 @@ func uploadImageToEventbrite(image Image, data EventbriteUploadData) error { return nil } -func notifyEventbriteOfNewImage(token string, uploadToken string) (string, error) { +func notifyEventbriteOfNewImage(token string, uploadToken string, width int, height int) (string, error) { path := eventbriteAPIBaseURL + "/media/upload/?token=" + token type topLeft struct { @@ -397,15 +400,31 @@ func notifyEventbriteOfNewImage(token string, uploadToken string) (string, error ID string `json:"id"` } + // Eventbrite requires an image with a 2:1 aspect ratio, but that may not always be what Facebook provides. + // So we must crop the image vertically or horizontally using the center. + var x, y = 1, 1 + var finalWidth, finalHeight = width, height + cropHeight := width / 2 + if cropHeight > height { + // If cropped height is greater than actual height, we need to crop horizontally instead. + cropWidth := height * 2 + x = (width - cropWidth) / 2 + finalWidth = cropWidth + } else { + // Else crop vertically. + y = (height - cropHeight) / 2 + finalHeight = cropHeight + } + req := reqBody{ UploadToken: uploadToken, CropMask: cropMask{ TopLeft: topLeft{ - X: 1, - Y: 1, + X: x, + Y: y, }, - Width: 1280, - Height: 640, + Width: finalWidth, + Height: finalHeight, }, } diff --git a/event_sync/facebook_api.go b/event_sync/facebook_api.go index a78b17b..f090604 100644 --- a/event_sync/facebook_api.go +++ b/event_sync/facebook_api.go @@ -1,14 +1,15 @@ package event_sync import ( + "bytes" "errors" + "github.com/dxe/adb/model" + "image" "io/ioutil" "net/http" "path" "strconv" "strings" - - "github.com/dxe/adb/model" ) const facebookAPIBaseURL = "https://graph.facebook.com/v4.0" @@ -55,6 +56,8 @@ type FacebookCover struct { type Image struct { Buffer []byte Name string + Width int + Height int } func getFacebookEvents(page model.ChapterWithToken) ([]FacebookEvent, error) { @@ -82,26 +85,33 @@ func getFacebookEvent(page model.ChapterWithToken, eventID string) (FacebookEven } func downloadImageFromFacebook(imageUrl string) (Image, error) { - var image Image + var outputImage Image resp, err := http.Get(imageUrl) if err != nil { - return image, err + return outputImage, err } defer resp.Body.Close() if resp.StatusCode != http.StatusOK { - return image, errors.New("failed to get image from Facebook. Status: " + strconv.Itoa(resp.StatusCode)) + return outputImage, errors.New("failed to get image from Facebook. Status: " + strconv.Itoa(resp.StatusCode)) + } + + outputImage.Buffer, err = ioutil.ReadAll(resp.Body) + if err != nil { + return outputImage, err } - image.Buffer, err = ioutil.ReadAll(resp.Body) + img, _, err := image.Decode(bytes.NewReader(outputImage.Buffer)) if err != nil { - return image, err + return outputImage, err } + outputImage.Width = img.Bounds().Dx() + outputImage.Height = img.Bounds().Dy() - image.Name = path.Base(imageUrl) - image.Name = image.Name[:strings.Index(image.Name, "?")] + pathBase := path.Base(imageUrl) + outputImage.Name = pathBase[:strings.Index(pathBase, "?")] - return image, nil + return outputImage, nil }