-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Ajat/Albert] move s3 and cloudfront package into aws package and ref…
…actor storage.baseUrl to storage.baseURL
- Loading branch information
1 parent
404b35f
commit 7567fa9
Showing
40 changed files
with
1,298 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Image Processor for Darkroom | ||
|
||
#### About | ||
This module holds the logic to process images. It is used by the Darkroom [Application Server](https://***REMOVED***/darkroom/core). | ||
You may implement the `Processor` interface to gain custom functionality while still keeping other Darkroom functionality. | ||
|
||
#### Interface | ||
```go | ||
type Processor interface { | ||
Crop(input []byte, width, height int, point CropPoint) ([]byte, error) | ||
Resize(input []byte, width, height int) ([]byte, error) | ||
Watermark(base []byte, overlay []byte, opacity uint8) ([]byte, error) | ||
GrayScale(input []byte) ([]byte, error) | ||
} | ||
``` | ||
Any `struct` implementing the above interface can be used with Darkroom. | ||
|
||
#### Example | ||
|
||
```go | ||
bp := NewBildProcessor() | ||
img, _ := ioutil.ReadFile("test.png") | ||
output, err := bp.Resize(img, 500, 500) | ||
_, _ := ioutil.WriteFile("output.png", output, 0644) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package processor | ||
|
||
type CropPoint int | ||
|
||
const ( | ||
CropTopLeft CropPoint = 1 | ||
CropTop CropPoint = 2 | ||
CropTopRight CropPoint = 3 | ||
CropLeft CropPoint = 4 | ||
CropCenter CropPoint = 5 | ||
CropRight CropPoint = 6 | ||
CropBottomLeft CropPoint = 7 | ||
CropBottom CropPoint = 8 | ||
CropBottomRight CropPoint = 9 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package processor | ||
|
||
// Processor interface for performing operations on image | ||
type Processor interface { | ||
Crop(input []byte, width, height int, point CropPoint) ([]byte, error) | ||
Resize(input []byte, width, height int) ([]byte, error) | ||
Watermark(base []byte, overlay []byte, opacity uint8) ([]byte, error) | ||
GrayScale(input []byte) ([]byte, error) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package native | ||
|
||
import ( | ||
"bytes" | ||
"github.com/anthonynsimon/bild/clone" | ||
"github.com/anthonynsimon/bild/effect" | ||
"github.com/anthonynsimon/bild/transform" | ||
"image" | ||
"image/color" | ||
"image/draw" | ||
"image/jpeg" | ||
"image/png" | ||
"***REMOVED***/darkroom/core/pkg/processor" | ||
) | ||
|
||
const ( | ||
pngType = "png" | ||
jpgType = "jpeg" | ||
) | ||
|
||
// BildProcessor uses bild library to process images using native Golang image.Image interface | ||
type BildProcessor struct { | ||
} | ||
|
||
func (bp *BildProcessor) Crop(input []byte, width, height int, point processor.CropPoint) ([]byte, error) { | ||
img, f, err := image.Decode(bytes.NewReader(input)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
w, h := getResizeWidthAndHeightForCrop(width, height, img.Bounds().Dx(), img.Bounds().Dy()) | ||
|
||
img = transform.Resize(img, w, h, transform.Linear) | ||
x0, y0 := getStartingPointForCrop(w, h, width, height, point) | ||
rect := image.Rect(x0, y0, width+x0, height+y0) | ||
img = (clone.AsRGBA(img)).SubImage(rect) | ||
|
||
return bp.encode(img, f) | ||
} | ||
|
||
func (bp *BildProcessor) Resize(input []byte, width, height int) ([]byte, error) { | ||
img, f, err := image.Decode(bytes.NewReader(input)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
initW := img.Bounds().Dx() | ||
initH := img.Bounds().Dy() | ||
|
||
w, h := getResizeWidthAndHeight(width, height, initW, initH) | ||
if w != initW || h != initH { | ||
img = transform.Resize(img, w, h, transform.Linear) | ||
} | ||
|
||
return bp.encode(img, f) | ||
} | ||
|
||
func (bp *BildProcessor) Watermark(base []byte, overlay []byte, opacity uint8) ([]byte, error) { | ||
baseImg, f, err := image.Decode(bytes.NewReader(base)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
overlayImg, _, err := image.Decode(bytes.NewReader(overlay)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ratio := float64(overlayImg.Bounds().Dy()) / float64(overlayImg.Bounds().Dx()) | ||
dWidth := float64(baseImg.Bounds().Dx()) / 2.0 | ||
|
||
// Resizing overlay image according to base image | ||
overlayImg = transform.Resize(overlayImg, int(dWidth), int(dWidth*ratio), transform.Linear) | ||
|
||
// Anchor point for overlaying | ||
x := (baseImg.Bounds().Dx() - overlayImg.Bounds().Dx()) / 2 | ||
y := (baseImg.Bounds().Dy() - overlayImg.Bounds().Dy()) / 2 | ||
offset := image.Pt(int(x), int(y)) | ||
|
||
// Mask image (that is just a solid light gray image) | ||
mask := image.NewUniform(color.Alpha{A: opacity}) | ||
|
||
// Performing overlay | ||
draw.DrawMask(baseImg.(draw.Image), overlayImg.Bounds().Add(offset), overlayImg, image.ZP, mask, image.ZP, draw.Over) | ||
|
||
return bp.encode(baseImg, f) | ||
} | ||
|
||
func (bp *BildProcessor) GrayScale(input []byte) ([]byte, error) { | ||
img, f, err := image.Decode(bytes.NewReader(input)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
img = effect.Grayscale(img) | ||
return bp.encode(img, f) | ||
} | ||
|
||
func (bp *BildProcessor) encode(img image.Image, format string) ([]byte, error) { | ||
if format == pngType && isOpaque(img) { | ||
format = jpgType | ||
} | ||
buff := &bytes.Buffer{} | ||
var err error | ||
if format == pngType { | ||
enc := png.Encoder{CompressionLevel: png.BestCompression} | ||
err = enc.Encode(buff, img) | ||
} else { | ||
err = jpeg.Encode(buff, img, nil) | ||
} | ||
return buff.Bytes(), err | ||
} | ||
|
||
func NewBildProcessor() *BildProcessor { | ||
return &BildProcessor{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package native | ||
|
||
import ( | ||
"bytes" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/suite" | ||
"image" | ||
"io/ioutil" | ||
"***REMOVED***/darkroom/core/pkg/processor" | ||
"testing" | ||
) | ||
|
||
type BildProcessorSuite struct { | ||
suite.Suite | ||
srcData []byte | ||
watermarkData []byte | ||
badData []byte | ||
processor processor.Processor | ||
} | ||
|
||
func (s *BildProcessorSuite) SetupSuite() { | ||
s.processor = NewBildProcessor() | ||
s.srcData, _ = ioutil.ReadFile("test.png") | ||
s.watermarkData, _ = ioutil.ReadFile("overlay.png") | ||
s.badData = []byte("badImage.ext") | ||
} | ||
|
||
func TestNewBildProcessor(t *testing.T) { | ||
suite.Run(t, new(BildProcessorSuite)) | ||
} | ||
|
||
func (s *BildProcessorSuite) TestBildProcessor_Resize() { | ||
output, err := s.processor.Resize(s.srcData, 500, 500) | ||
|
||
assert.NotNil(s.T(), output) | ||
assert.Nil(s.T(), err) | ||
|
||
img, _, _ := image.Decode(bytes.NewReader(output)) | ||
assert.Equal(s.T(), 500, img.Bounds().Dx()) | ||
assert.Equal(s.T(), 375, img.Bounds().Dy()) | ||
} | ||
|
||
func (s *BildProcessorSuite) TestBildProcessor_Crop() { | ||
output, err := s.processor.Crop(s.srcData, 500, 500, processor.CropCenter) | ||
|
||
assert.NotNil(s.T(), output) | ||
assert.Nil(s.T(), err) | ||
|
||
img, _, _ := image.Decode(bytes.NewReader(output)) | ||
assert.Equal(s.T(), 500, img.Bounds().Dx()) | ||
assert.Equal(s.T(), 500, img.Bounds().Dy()) | ||
} | ||
|
||
func (s *BildProcessorSuite) TestBildProcessor_Grayscale() { | ||
output, err := s.processor.GrayScale(s.srcData) | ||
|
||
assert.NotNil(s.T(), output) | ||
assert.Nil(s.T(), err) | ||
assert.NotEqual(s.T(), s.srcData, output) | ||
} | ||
|
||
func (s *BildProcessorSuite) TestBildProcessor_Watermark() { | ||
output, err := s.processor.Watermark(s.srcData, s.watermarkData, 200) | ||
|
||
assert.NotNil(s.T(), output) | ||
assert.Nil(s.T(), err) | ||
|
||
assert.NotEqual(s.T(), s.srcData, output) | ||
} | ||
|
||
func (s *BildProcessorSuite) TestBildProcessorWithBadInput() { | ||
output, err := s.processor.Crop(s.badData, 0, 0, processor.CropCenter) | ||
assert.NotNil(s.T(), err) | ||
assert.Nil(s.T(), output) | ||
|
||
output, err = s.processor.Resize(s.badData, 0, 0) | ||
assert.NotNil(s.T(), err) | ||
assert.Nil(s.T(), output) | ||
|
||
output, err = s.processor.GrayScale(s.badData) | ||
assert.NotNil(s.T(), err) | ||
assert.Nil(s.T(), output) | ||
|
||
output, err = s.processor.Watermark(s.badData, s.watermarkData, 255) | ||
assert.NotNil(s.T(), err) | ||
assert.Nil(s.T(), output) | ||
|
||
output, err = s.processor.Watermark(s.srcData, s.badData, 255) | ||
assert.NotNil(s.T(), err) | ||
assert.Nil(s.T(), output) | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.