Created
March 6, 2016 08:27
-
-
Save justinfx/153e611c4df38e97494d to your computer and use it in GitHub Desktop.
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
// Testing reported mem leak at: https://github.com/gographics/imagick/issues/72 | |
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"os" | |
"runtime" | |
"gopkg.in/gographics/imagick.v2/imagick" | |
) | |
const SrcImage = `/path/to/source/image.jpg` | |
func main() { | |
imagick.Initialize() | |
defer imagick.Terminate() | |
i := 0 | |
var stats runtime.MemStats | |
for { | |
Process() | |
runtime.ReadMemStats(&stats) | |
i++ | |
fmt.Printf("After run #%04d: Alloc: %v, Idle: %v, InUse: %v, Objects: %v\n", i, | |
stats.HeapAlloc, stats.HeapIdle, stats.HeapInuse, stats.HeapObjects) | |
} | |
} | |
func Process() { | |
f, err := os.Open(SrcImage) | |
if err != nil { | |
log.Fatalf("Failed to open source image: %v", err) | |
} | |
defer f.Close() | |
img, err := ioutil.ReadAll(f) | |
if err != nil { | |
log.Fatalf("Failed to read image file: %v", err) | |
} | |
mw := imagick.NewMagickWand() | |
defer mw.Destroy() | |
err = mw.ReadImageBlob(img) | |
if err != nil { | |
log.Fatalf("Failed to read image blob: %v", err) | |
} | |
originalWidth := float64(mw.GetImageWidth()) | |
originalHeight := float64(mw.GetImageHeight()) | |
imageAspectRatio := originalWidth / originalHeight | |
masterWidth := 320.0 | |
masterHeight := 180.0 | |
masterAspectRatio := masterWidth / masterHeight | |
pwm := imagick.NewPixelWand() | |
defer pwm.Destroy() | |
tx := imagick.NewMagickWand() | |
defer tx.Destroy() | |
var w, h uint = 0, 0 | |
size := fmt.Sprintf("%dx%d^+0+0", w, h) | |
if imageAspectRatio <= masterAspectRatio { | |
// trim the height | |
w = uint(originalWidth) | |
h = (uint(originalWidth / masterAspectRatio)) | |
size = fmt.Sprintf("%dx%d^+0+0", w, h) | |
} else { | |
// trim the width | |
w = uint(originalHeight * masterAspectRatio) | |
h = uint(originalHeight) | |
size = fmt.Sprintf("%dx%d^+0+0", w, h) | |
} | |
tx = mw.TransformImage("", size) | |
tx.SetImageGravity(imagick.GRAVITY_CENTER) | |
offsetX := -(int(w) - int(tx.GetImageWidth())) / 2 | |
offsetY := -(int(h) - int(tx.GetImageHeight())) / 2 | |
err = tx.ExtentImage(w, h, offsetX, offsetY) | |
if err != nil { | |
log.Fatalf("Failed calling ExtendImage(%v, %v, %v, %v): %v", | |
w, h, offsetX, offsetY, err) | |
} | |
if float64(tx.GetImageWidth()) > masterWidth && float64(tx.GetImageHeight()) > masterHeight { | |
err = tx.ResizeImage(uint(masterWidth), uint(masterHeight), imagick.FILTER_BOX, 1) | |
if err != nil { | |
log.Fatalf("Inside CreateMaster function Couldn't "+ | |
"resize the image %s: %s", SrcImage, err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment