Skip to content

Commit

Permalink
Merge pull request #191 from dxe/fix-eventbrite-image-crop
Browse files Browse the repository at this point in the history
Fix EB image crop
  • Loading branch information
jakehobbs authored Jan 28, 2023
2 parents 18f0139 + 53adced commit 7ec7789
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
31 changes: 25 additions & 6 deletions event_sync/eventbrite_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
},
}

Expand Down
30 changes: 20 additions & 10 deletions event_sync/facebook_api.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

0 comments on commit 7ec7789

Please sign in to comment.