當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Image.Crop方法代碼示例

本文整理匯總了Golang中github.com/ctripcorp/nephele/imgsvr/img4g.Image.Crop方法的典型用法代碼示例。如果您正苦於以下問題:Golang Image.Crop方法的具體用法?Golang Image.Crop怎麽用?Golang Image.Crop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/ctripcorp/nephele/imgsvr/img4g.Image的用法示例。


在下文中一共展示了Image.Crop方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Process

func (this *ResizeRProcessor) Process(img *img4g.Image) error {
	log.Debug("process resize r")
	var err error
	tran := this.Cat.NewTransaction("Command", "ResizeR")
	defer func() {
		tran.SetStatus(err)
		tran.Complete()
	}()
	var width, height int64
	width, height, err = img.Size()
	if err != nil {
		return err
	}
	if width <= this.Width && height <= this.Height {
		return nil
	}
	var (
		x int64 = 0
		y int64 = 0
		w int64 = width
		h int64 = height
	)
	if width > this.Width && height > this.Height {
		p1 := float64(this.Width) / float64(this.Height)
		p2 := float64(width) / float64(height)
		w = 0
		h = 0
		if math.Abs(p1-p2) > 0.0001 {
			if p2 > p1 { //以高縮小
				h = height
				w = int64(math.Floor(float64(h) * p1))
				x = (width - w) / 2
			}
			if p2 < p1 { //以寬縮小
				w = width
				h = int64(math.Floor(float64(w) / p1))
				y = (height - h) / 2
			}
			err = img.Crop(w, h, x, y)
			if err != nil {
				return err
			}
		}
		err = img.Resize(this.Width, this.Height)
		return err
	} else {
		if width > this.Width {
			x = (w - this.Width) / 2
			w = this.Width
		}
		if height > this.Height {
			y = (h - this.Height) / 2
			h = this.Height
		}
		err = img.Crop(w, h, x, y)
		return err
	}

}
開發者ID:chenbk85,項目名稱:nephele,代碼行數:59,代碼來源:resize_r.go

示例2: Process

func (this *ResizeCProcessor) Process(img *img4g.Image) error {
	l4g.Debug("process resize c")
	var err error
	tran := this.Cat.NewTransaction(Image, "ResizeC")
	defer func() {
		tran.SetStatus(err)
		tran.Complete()
	}()

	var width, height = this.imgWidth, this.imgHeight
	var wd, ht int64
	if width == 0 || height == 0 {
		wd, ht, err = img.Size()
		if err != nil {
			return err
		}
		width = wd
		height = ht
	}

	p1 := float64(this.Width) / float64(this.Height)
	p2 := float64(width) / float64(height)
	var (
		x int64 = 0
		y int64 = 0
		w int64 = 0
		h int64 = 0
	)
	if math.Abs(p1-p2) > 0.0001 {
		if p2 > p1 { //以高縮小
			h = height
			w = int64(math.Floor(float64(h) * p1))
			x = (width - w) / 2
		}
		if p2 < p1 { //以寬縮小
			w = width
			h = int64(math.Floor(float64(w) / p1))
			y = (height - h) / 2
		}
		err = img.Crop(w, h, x, y)
		if err != nil {
			return err
		}
	}
	err = img.Resize(this.Width, this.Height)
	return err
}
開發者ID:zesus19,項目名稱:nephele,代碼行數:47,代碼來源:resizecprocessor.go

示例3: Process

func (this *ResizeRProcessor) Process(img *img4g.Image) error {
	l4g.Debug("process resize r")
	var err error
	tran := this.Cat.NewTransaction(Image, "ResizeR")
	defer func() {
		tran.SetStatus(err)
		tran.Complete()
	}()
	var width, height int64
	width, height, err = img.Size()
	if err != nil {
		return err
	}
	if width <= this.Width && height <= this.Height {
		return nil
	}
	if width > this.Width && height > this.Height {
		c := ResizeCProcessor{this.Width, this.Height, this.Cat, width, height}
		err = c.Process(img)
		return err
	}
	var (
		x int64 = 0
		y int64 = 0
		w int64 = width
		h int64 = height
	)
	if width > this.Width {
		x = (w - this.Width) / 2
		w = this.Width
	}
	if height > this.Height {
		y = (h - this.Height) / 2
		h = this.Height
	}
	err = img.Crop(w, h, x, y)
	return err
}
開發者ID:zesus19,項目名稱:nephele,代碼行數:38,代碼來源:resizerprocessor.go


注:本文中的github.com/ctripcorp/nephele/imgsvr/img4g.Image.Crop方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。