Example #1
0
func ApplyWaterMark(orig Image, mark image.Image) (Image, error) {
	oimg, err := orig.Image()
	if err != nil {
		return nil, e.Forward(err)
	}

	oimgW, oimgH := size(oimg)

	mark = resize.Resize(uint(round.RoundDec32(float32(oimgW)*0.9, 0)), 0, mark, resize.MitchellNetravali)

	markW, markH := size(mark)

	left := oimgW/2 - markW/2
	top := oimgH/2 - markH/2
	offset := image.Pt(left, top)

	b := oimg.Bounds()
	m := image.NewRGBA(b)
	draw.Draw(m, b, oimg, image.ZP, draw.Src)
	draw.Draw(m, mark.Bounds().Add(offset), mark, image.ZP, draw.Over)

	out, err := newMarkedImage(m)
	if err != nil {
		return nil, e.Forward(err)
	}
	return out, nil
}
Example #2
0
func New(name, disk string, sleep int, minfree float32, em *mail.Email, alarm time.Duration) monitors.Monitor {
	if alarm == 0 {
		alarm = Alarm
	}
	dm := &Disk{
		MonitorBase:  base.New(),
		name:         name,
		disk:         disk,
		close:        make(chan bool),
		alertPeriode: alarm,
		email:        em,
		monitoring:   true,
	}

	dm.status = status.NewStatuses(name, em, definitions.LengthStatusHistory, status.NewStatus, dm.SendEvent, nil)

	go func() {
		for {
			select {
			case <-time.After(time.Duration(sleep) * time.Second):
				d, err := event.ProbeDisk(disk)
				if err != nil {
					dm.status.Log(status.Verbose, "DiskSpace failed with error: %v", e.Trace(e.Forward(err)))
					continue
				}
				percent := (float32(d.Free) / float32(d.Total)) * 100.0
				if percent < minfree {
					if dm.alert.Before(time.Now()) {
						dm.alert = time.Now().Add(dm.alertPeriode)
						dm.status.LogAndEmail(status.Normal, "Disk limit ["+disk+"]", "Disk %v is %.4f%% free.", disk, round.RoundDec32(percent, 4))
					}
				} else {
					if dm.alert.After(time.Now()) {
						dm.status.LogAndEmail(status.Normal, "Disk limit ["+disk+"]", "Disk %v is %.4f%% free.", disk, round.RoundDec32(percent, 4))
					}
					dm.alert = time.Time{}
				}
				dm.SendEvent(d)
			case <-dm.close:
				break
			}
		}
		dm.status.Log(status.Normal, "Disk monitor stopped for %v.", name)
	}()
	return dm
}
Example #3
0
func (m *markedImage) Transform(crop image.Rectangle, width, height uint, dontenlarge bool, format string, resample resize.InterpolationFunction) (Image, error) {
	novaFoto, err := m.Copy()
	if err != nil {
		return nil, e.Forward(err)
	}

	img, err := novaFoto.Image()
	if err != nil {
		return nil, e.Forward(err)
	}

	if width > math.MaxInt32 {
		return nil, e.New("width is big")
	}
	if height > math.MaxInt32 {
		return nil, e.New("height is big")
	}

	bounds, err := m.Bounds()
	if err != nil {
		return nil, e.Forward(err)
	}
	w := bounds.Dx()
	h := bounds.Dy()

	f, err := m.Format()
	if err != nil {
		return nil, e.Forward(err)
	}

	if crop.Empty() && ((width == 0 && height == 0) || (int(width) == w && int(height) == h)) && (format == "" || format == f) {
		return novaFoto, nil
	}

	if !crop.Empty() {
		sub, ok := img.(SubImager)
		if !ok {
			return nil, e.New("this image type don't support cropping")
		}
		img = sub.SubImage(crop)
	}

	imgResized := img
	if !(width == 0 && height == 0) {
		rect := img.Bounds()
		w := uint(rect.Max.X - rect.Min.X)
		h := uint(rect.Max.Y - rect.Min.Y)

		imgRatio := img
		if width > 0 && height > 0 {
			ratio := round.RoundDec32(float32(w)/float32(h), 1)
			newRatio := round.RoundDec32(float32(width)/float32(height), 1)

			if ratio != newRatio {
				//TODO: se imagem tem largura menor que 10px não vai funcionar
				imgRatio, err = cutter.Crop(img, cutter.Config{
					Width:   int(newRatio * 10.0),
					Height:  10,
					Mode:    cutter.Centered,
					Options: cutter.Ratio,
				})
				if err != nil {
					return nil, e.Push(err, "can't crop the image")
				}
				rect = imgRatio.Bounds()
				w = uint(rect.Max.X - rect.Min.X)
				h = uint(rect.Max.Y - rect.Min.Y)
			}
		}

		imgResized = imgRatio
		if (width >= w || height >= h) && !dontenlarge {
			imgResized = resize.Resize(width, height, imgRatio, resample)
		} else if width < w && height < h {
			imgResized = resize.Resize(width, height, imgRatio, resample)
		}
	}

	// rect := imgResized.Bounds()
	// novaFoto.Width_ = rect.Max.X - rect.Min.X
	// novaFoto.Height_ = rect.Max.Y - rect.Min.Y

	b := make([]byte, 0, 1024*1024*5)
	buf := bytes.NewBuffer(b)

	// if format == "" {
	// 	format = novaFoto.Mime_
	// }

	switch format {
	// case "image/bmp", "image/x-windows-bmp":
	// 	err := bmp.Encode(buf, imgResized)
	// 	if err != nil {
	// 		return nil, e.Forward(err)
	// 	}
	// 	novaFoto.Format_ = "bmp"
	// case "image/gif":
	// 	opt := &gif.Options{
	// 		NumColors: 256,
	// 	}
	// 	err := gif.Encode(buf, imgResized, opt)
	// 	if err != nil {
	// 		return nil, e.Forward(err)
	// 	}
	// 	novaFoto.Format_ = "gif"
	case "image/jpeg":
		opt := &jpeg.Options{
			Quality: Quality,
		}
		err := jpeg.Encode(buf, imgResized, opt)
		if err != nil {
			return nil, e.Forward(err)
		}
		//novaFoto.Format_ = "jpeg"
	// case "image/png":
	// 	err := png.Encode(buf, imgResized)
	// 	if err != nil {
	// 		return nil, e.Forward(err)
	// 	}
	// 	novaFoto.Format_ = "png"
	// case "image/tiff", "image/x-tiff":
	// 	opt := &tiff.Options{
	// 		Compression: tiff.Deflate,
	// 		Predictor:   true,
	// 	}
	// 	err := tiff.Encode(buf, imgResized, opt)
	// 	if err != nil {
	// 		return nil, e.Forward(err)
	// 	}
	// 	novaFoto.Format_ = "tiff"
	default:
		return nil, e.New("image format isn't supported")
	}

	// novaFoto.Mime_ = format
	// novaFoto.FileSize_ = int64(buf.Len())
	// novaFoto.Data = buf.Bytes()

	return novaFoto, nil
}