Beispiel #1
0
func glitchImage(wand *imagick.MagickWand, q url.Values) ([]byte, error) {
	data := wand.GetImage().GetImageBlob()
	jpgHeaderLength := getJpegHeaderSize(data)
	maxIndex := len(data) - jpgHeaderLength - 4
	params := getParams(q)
	length := int(params["iterations"])
	for i := 0; i < length; i++ {
		pxMin := math.Floor(float64(maxIndex) / params["iterations"] * float64(i))
		pxMax := math.Floor(float64(maxIndex) / params["iterations"] * float64((i + 1)))
		delta := pxMax - pxMin
		pxI := math.Floor(pxMin + delta*params["seed"])
		if int(pxI) > maxIndex {
			pxI = float64(maxIndex)
		}
		index := math.Floor(float64(jpgHeaderLength) + pxI)
		data[int(index)] = byte(math.Floor(params["amount"] * float64(256)))
	}
	wand2 := imagick.NewMagickWand()
	if err := wand2.ReadImageBlob(data); err != nil {
		return nil, err
	}
	if err := wand2.SetImageFormat("PNG"); err != nil {
		return nil, err
	}
	return wand2.GetImage().GetImageBlob(), nil
}
Beispiel #2
0
func resizeImage(wand *imagick.MagickWand, size int, mozaic bool) *imagick.MagickWand {
	width := float32(wand.GetImageWidth())
	height := float32(wand.GetImageHeight())
	var rate float32
	if width > height {
		rate = float32(size) / width
	} else {
		rate = float32(size) / height
	}
	if mozaic {
		wand.ResizeImage(uint(width*rate/20), uint(height*rate/20), imagick.FILTER_LANCZOS, 1)
		wand.ResizeImage(uint(width*rate), uint(height*rate), imagick.FILTER_POINT, 1)
	} else {
		wand.ResizeImage(uint(width*rate), uint(height*rate), imagick.FILTER_LANCZOS, 1)
	}
	return wand.GetImage()
}