Skip to content
This repository was archived by the owner on Oct 8, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions halfshell/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ import (
"io"
"io/ioutil"
"os"
"strconv"
"strings"

"github.com/rafikk/imagick/imagick"
)

var EmptyImageDimensions = ImageDimensions{}
var EmptyResizeDimensions = ResizeDimensions{}
var DefaultFocalPoint = Focalpoint{0.5, 0.5}

type Image struct {
Wand *imagick.MagickWand
Expand Down Expand Up @@ -109,3 +111,31 @@ type ResizeDimensions struct {
Scale ImageDimensions
Crop ImageDimensions
}

// Focalpoint is a float pair representing the location of the image subject.
// (0.5, 0.5) is the middle. (1, 1) is the bottom right. (0, 0) is the top left.
type Focalpoint struct {
X float64
Y float64
}

// NewFocalpointFromString splits the given string into a Focalpoint struct. The
// string format should be: "X,Y". For example: "0.1,0.1".
func NewFocalpointFromString(s string) (fp Focalpoint) {
pair := strings.Split(s, ",")
if len(pair) != 2 {
return DefaultFocalPoint
}

x, err := strconv.ParseFloat(pair[0], 64)
if err != nil {
return DefaultFocalPoint
}

y, err := strconv.ParseFloat(pair[1], 64)
if err != nil {
return DefaultFocalPoint
}

return Focalpoint{x, y}
}
19 changes: 8 additions & 11 deletions halfshell/image_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type ImageProcessorOptions struct {
Dimensions ImageDimensions
BlurRadius float64
ScaleMode uint
Focalpoint Focalpoint
}

type imageProcessor struct {
Expand Down Expand Up @@ -153,7 +154,7 @@ func (ip *imageProcessor) resize(img *Image, req *ImageProcessorOptions) error {
}

if resize.Crop != EmptyImageDimensions {
err = ip.cropApply(img, resize.Crop)
err = ip.cropApply(img, resize.Crop, req.Focalpoint)
if err != nil {
return err
}
Expand Down Expand Up @@ -292,17 +293,13 @@ func (ip *imageProcessor) resizeApply(img *Image, dimensions ImageDimensions) er
return nil
}

func (ip *imageProcessor) cropApply(img *Image, reqDimensions ImageDimensions) error {
if reqDimensions == EmptyImageDimensions {
return nil
}
func (ip *imageProcessor) cropApply(img *Image, reqDimensions ImageDimensions, focalpoint Focalpoint) error {
oldDimensions := img.GetDimensions()
return img.Wand.CropImage(
reqDimensions.Width,
reqDimensions.Height,
int((oldDimensions.Width-reqDimensions.Width)/2),
int((oldDimensions.Height-reqDimensions.Height)/2),
)
x := int(focalpoint.X * (float64(oldDimensions.Width) - float64(reqDimensions.Width)))
y := int(focalpoint.Y * (float64(oldDimensions.Height) - float64(reqDimensions.Height)))
w := reqDimensions.Width
h := reqDimensions.Height
return img.Wand.CropImage(w, h, x, y)
}

func (ip *imageProcessor) blur(image *Image, request *ImageProcessorOptions) error {
Expand Down
2 changes: 2 additions & 0 deletions halfshell/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (p *Route) SourceAndProcessorOptionsForRequest(r *http.Request) (
width, _ := strconv.ParseUint(r.FormValue("w"), 10, 32)
height, _ := strconv.ParseUint(r.FormValue("h"), 10, 32)
blurRadius, _ := strconv.ParseFloat(r.FormValue("blur"), 64)
focalpoint := r.FormValue("focalpoint")

scaleModeName := r.FormValue("scale_mode")
scaleMode, _ := ScaleModes[scaleModeName]
Expand All @@ -77,5 +78,6 @@ func (p *Route) SourceAndProcessorOptionsForRequest(r *http.Request) (
Dimensions: ImageDimensions{uint(width), uint(height)},
BlurRadius: blurRadius,
ScaleMode: uint(scaleMode),
Focalpoint: NewFocalpointFromString(focalpoint),
}
}