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


Golang imaging.Thumbnail函數代碼示例

本文整理匯總了Golang中github.com/disintegration/imaging.Thumbnail函數的典型用法代碼示例。如果您正苦於以下問題:Golang Thumbnail函數的具體用法?Golang Thumbnail怎麽用?Golang Thumbnail使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


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

示例1: MakeFromImage

func MakeFromImage(srcImage image.Image, t string, w, h int) (image *image.NRGBA, err error) {
	switch t {
	case "thumbnail":
		image = imaging.Thumbnail(srcImage, w, h, imaging.Lanczos)
	case "resize":
		image = imaging.Resize(srcImage, w, h, imaging.Lanczos)
	case "fit":
		image = imaging.Fit(srcImage, w, h, imaging.Lanczos)
	default:
		image = imaging.Thumbnail(srcImage, w, h, imaging.Lanczos)
	}

	return image, nil
}
開發者ID:netw0rm,項目名稱:reweb,代碼行數:14,代碼來源:utils.go

示例2: transformImage

// transformImage modifies the image m based on the transformations specified
// in opt.
func transformImage(m image.Image, opt Options) image.Image {
	// resize if needed
	if w, h, resize := resizeParams(m, opt); resize {
		if opt.Fit {
			m = imaging.Fit(m, w, h, resampleFilter)
		} else {
			if w == 0 || h == 0 {
				m = imaging.Resize(m, w, h, resampleFilter)
			} else {
				m = imaging.Thumbnail(m, w, h, resampleFilter)
			}
		}
	}

	// flip
	if opt.FlipVertical {
		m = imaging.FlipV(m)
	}
	if opt.FlipHorizontal {
		m = imaging.FlipH(m)
	}

	// rotate
	switch opt.Rotate {
	case 90:
		m = imaging.Rotate90(m)
	case 180:
		m = imaging.Rotate180(m)
	case 270:
		m = imaging.Rotate270(m)
	}

	return m
}
開發者ID:LimiQS,項目名稱:imageproxy,代碼行數:36,代碼來源:transform.go

示例3: Resized

func Resized(ext string, data []byte, width, height int) (resized []byte, w int, h int) {
	if width == 0 && height == 0 {
		return data, 0, 0
	}
	srcImage, _, err := image.Decode(bytes.NewReader(data))
	if err == nil {
		bounds := srcImage.Bounds()
		var dstImage *image.NRGBA
		if bounds.Dx() > width && width != 0 || bounds.Dy() > height && height != 0 {
			if width == height && bounds.Dx() != bounds.Dy() {
				dstImage = imaging.Thumbnail(srcImage, width, height, imaging.Lanczos)
				w, h = width, height
			} else {
				dstImage = imaging.Resize(srcImage, width, height, imaging.Lanczos)
			}
		} else {
			return data, bounds.Dx(), bounds.Dy()
		}
		var buf bytes.Buffer
		switch ext {
		case ".png":
			png.Encode(&buf, dstImage)
		case ".jpg", ".jpeg":
			jpeg.Encode(&buf, dstImage, nil)
		case ".gif":
			gif.Encode(&buf, dstImage, nil)
		}
		return buf.Bytes(), dstImage.Bounds().Dx(), dstImage.Bounds().Dy()
	} else {
		glog.Error(err)
	}
	return data, 0, 0
}
開發者ID:netroby,項目名稱:seaweedfs,代碼行數:33,代碼來源:resizing.go

示例4: createThumb

func (m *Manikyr) createThumb(root, parentFile string) {
	img, err := openImageWhenReady(parentFile)
	if err != nil {
		m.EmitEvent(root, Error, parentFile, err)
		return
	}

	localThumbs := m.ThumbDirGetter(parentFile)
	_, err = os.Stat(localThumbs)
	// If thumbDir does not exist...
	if os.IsNotExist(err) {
		// ..create it
		err := os.Mkdir(localThumbs, m.thumbDirPerms)
		if err != nil {
			m.EmitEvent(root, Error, localThumbs, err)
			return
		}
	} else if err != nil {
		m.EmitEvent(root, Error, localThumbs, err)
		return
	}

	// Save the thumbnail
	thumb := imaging.Thumbnail(img, m.thumbWidth, m.thumbHeight, m.thumbAlgo)
	thumbPath := path.Join(localThumbs, m.ThumbNameGetter(parentFile))
	if err = imaging.Save(thumb, thumbPath); err != nil {
		m.EmitEvent(root, Error, thumbPath, err)
		return
	}

	m.EmitEvent(root, ThumbCreate, thumbPath, nil)
}
開發者ID:ComSecNinja,項目名稱:manikyr,代碼行數:32,代碼來源:manikyr.go

示例5: thumbFile

func thumbFile(filePath string) {
	src, _ := imaging.Open(filePath)
	var dst *image.NRGBA

	dst = imaging.Thumbnail(src, 48, 48, imaging.CatmullRom)
	imaging.Save(dst, filePath)
}
開發者ID:zeuson,項目名稱:gorevel,代碼行數:7,代碼來源:application.go

示例6: ProcessAvatar

func ProcessAvatar(user *User) error {
	origImg, err := imaging.Open(GetAvatarPath(user, "o"))
	if err != nil {
		return err
	}

	src := imaging.Clone(origImg)

	for _, sz := range AvatarSizes {
		var dst image.Image

		switch sz.Type {
		case "thumbnail":
			dst = imaging.Thumbnail(src, sz.Width, sz.Height, imaging.Lanczos)
		case "fit":
			dst = imaging.Fit(src, sz.Width, sz.Height, imaging.Lanczos)
		}

		err := imaging.Save(dst, GetAvatarPath(user, sz.Suffix))
		if err != nil {
			return err
		}
	}

	return nil
}
開發者ID:postfix,項目名稱:quiet-1,代碼行數:26,代碼來源:processing.go

示例7: ProcessPhoto

func ProcessPhoto(photo *Photo) error {
	origImg, err := imaging.Open(GetPhotoPath(photo, "o"))
	if err != nil {
		return err
	}

	src := imaging.Clone(origImg)

	for _, sz := range PhotoSizes {
		var dst image.Image

		switch sz.Type {
		case "thumbnail":
			dst = imaging.Thumbnail(src, sz.Width, sz.Height, imaging.Lanczos)
		case "fit":
			dst = imaging.Fit(src, sz.Width, sz.Height, imaging.Lanczos)
		}

		err := imaging.Save(dst, GetPhotoPath(photo, sz.Suffix))
		if err != nil {
			return err
		}
	}

	return nil
}
開發者ID:postfix,項目名稱:quiet-1,代碼行數:26,代碼來源:processing.go

示例8: Resize

func (api *Api) Resize(imgloc string, xsize, ysize int, resizeType string) bool {
	dest := setSize(imgloc, xsize, ysize)
	if _, err := os.Stat(dest); err == nil {
		return true
	}
	bts, err := ioutil.ReadFile(imgloc)
	if err != nil {
		fmt.Println(err)
		return false
	}
	rdr := bytes.NewReader(bts)
	i, _, err := image.Decode(rdr)
	if err != nil {
		fmt.Println(err)
		return false
	}
	var fsimg *image.NRGBA
	switch resizeType {
	case "fit":
		fsimg = imaging.Fit(i, xsize, ysize, imaging.Lanczos)
	case "thumb":
		fsimg = imaging.Thumbnail(i, xsize, ysize, imaging.Lanczos)
	default:
		fsimg = imaging.Resize(i, xsize, ysize, imaging.Lanczos)
	}
	out, err := os.Create(dest)
	if err != nil {
		return false
	}
	defer out.Close()
	jpeg.Encode(out, fsimg, nil)
	return true
}
開發者ID:sisteamnik,項目名稱:guseful,代碼行數:33,代碼來源:api.go

示例9: MakeThumbnailFromReader

func MakeThumbnailFromReader(reader io.Reader, w, h int) (image *image.NRGBA, err error) {
	srcImage, err := Open(reader)
	if err != nil {
		return nil, err
	}

	image = imaging.Thumbnail(srcImage, w, h, imaging.Lanczos)
	return
}
開發者ID:netw0rm,項目名稱:reweb,代碼行數:9,代碼來源:utils.go

示例10: MakeThumbnail

// 生成縮略圖
func MakeThumbnail(fromFile string, w, h int) (image *image.NRGBA, err error) {
	srcImage, err := imaging.Open(fromFile)
	if err != nil {
		return nil, err
	}

	image = imaging.Thumbnail(srcImage, w, h, imaging.Lanczos)
	return
}
開發者ID:netw0rm,項目名稱:reweb,代碼行數:10,代碼來源:utils.go

示例11: Thumbnail

//Creating a thumbnail from a mp4 is complex so I cheat and use FFMPEG to create a JPEG...
//JPEG is straightforwards
// but ffmpeg probably can't make a thumbnail from a piped reader, so this only works if our
//ReadSeeker is actually an *os.File
func (m *Mp4Video) Thumbnail(in io.ReadSeeker, longSide int) (io.ReadSeeker, string, error) {
	var cmd *exec.Cmd
	if file, ok := in.(*os.File); ok {
		//this is the best way as ffmpeg can seek.
		cmd = exec.Command("ffmpeg", "-i", "/dev/fd/3", "-vframes", "1", "-f", "image2", "-")
		cmd.ExtraFiles = []*os.File{file}
	} else {
		log.Println("mp4thumb: using stdin (will probably fail...)")
		cmd = exec.Command("ffmpeg", "-i", "-", "-vframes", "1", "-f", "image2", "-")
		cmd.Stdin = in
	}
	stdout, err := cmd.StdoutPipe()
	//cmd.Stderr = os.Stderr
	if err != nil {
		return nil, "", err
	}
	if err := cmd.Start(); err != nil {
		return nil, "", err
	}
	img, err := jpeg.Decode(stdout)
	if err != nil {
		return nil, "", err
	}
	if err := cmd.Wait(); err != nil {
		return nil, "", err
	}
	//now we should have a jpeg to resize!
	var w, h int
	aspect := float64(m.Width) / float64(m.Height)
	if m.Width > m.Height {
		w, h = longSide, int(float64(longSide)/aspect)
	} else {
		w, h = int(float64(longSide)*aspect), longSide
	}
	switch m.Orientation {
	case photo.OrientedNormal90, photo.OrientedNormal270:
		//flip then rotate 270
		w, h = h, w
	}
	//now create thumbnail.
	img = imaging.Thumbnail(img, w, h, imaging.Box)
	//rotate if needed.
	switch m.Orientation {
	case photo.OrientedNormal90:
		//rotate 90 (270 anticlockwise)
		img = imaging.Rotate270(img)
	case photo.OrientedNormal180:
		//rotate 180
		img = imaging.Rotate180(img)
	case photo.OrientedNormal270:
		//rotate 270 (90 anti-clockwise)
		img = imaging.Rotate90(img)
	}
	var wr bytes.Buffer
	err = jpeg.Encode(&wr, img, nil)
	return bytes.NewReader(wr.Bytes()), "image/jpeg", err
}
開發者ID:thechriswalker,項目名稱:opfs,代碼行數:61,代碼來源:mp4.go

示例12: MakeFromReader

func MakeFromReader(reader io.Reader, t string, w, h int) (image *image.NRGBA, err error) {
	srcImage, err := Open(reader)
	if err != nil {
		return nil, err
	}

	switch t {
	case "thumbnail":
		image = imaging.Thumbnail(srcImage, w, h, imaging.Lanczos)
	case "resize":
		image = imaging.Resize(srcImage, w, h, imaging.Lanczos)
	case "fit":
		image = imaging.Fit(srcImage, w, h, imaging.Lanczos)
	default:
		image = imaging.Thumbnail(srcImage, w, h, imaging.Lanczos)
	}

	return
}
開發者ID:netw0rm,項目名稱:reweb,代碼行數:19,代碼來源:utils.go

示例13: Thumbnail

//Creating a thumbnail from a JPEG is straightforwards
func (p *JpegPhoto) Thumbnail(in io.ReadSeeker, longSide int) (io.ReadSeeker, string, error) {

	//first we need to read the image.
	img, _, err := image.Decode(in)
	if err != nil {
		return nil, "", err
	}
	var w, h int
	aspect := float64(p.Width) / float64(p.Height)
	if p.Width > p.Height {
		w, h = longSide, int(float64(longSide)/aspect)
	} else {
		w, h = int(float64(longSide)*aspect), longSide
	}
	//we need to do this switch twice. first to check if we need to swap width/height
	//as we resize before rotation/flip
	//then after to do the resize/flip.
	switch p.Orientation {
	case OrientedNormal90, OrientedMirror90, OrientedNormal270, OrientedMirror270:
		//flip then rotate 270
		w, h = h, w
	}
	//now create thumbnail.
	img = imaging.Thumbnail(img, w, h, imaging.Box)
	//now we need to rotate/flip it to match the ExifOrientation flag
	switch p.Orientation {
	case OrientedNormal:
		//nothing
	case OrientedMirror:
		//flip only
		img = imaging.FlipH(img)
	case OrientedNormal90:
		//rotate 90
		img = imaging.Rotate90(img)
	case OrientedMirror90:
		//flip and rotate 90
		img = imaging.FlipH(imaging.Rotate90(img))
	case OrientedNormal180:
		//rotate 180
		img = imaging.Rotate180(img)
	case OrientedMirror180:
		//flip then rotate 180
		img = imaging.FlipH(imaging.Rotate180(img))
	case OrientedNormal270:
		//rotate 270 (90 anti-clockwise)
		img = imaging.Rotate270(img)
	case OrientedMirror270:
		//flip then rotate 270
		img = imaging.FlipH(imaging.Rotate270(img))
	}
	//now re-encode
	var wr bytes.Buffer
	err = jpeg.Encode(&wr, img, nil)
	return bytes.NewReader(wr.Bytes()), "image/jpeg", err
}
開發者ID:thechriswalker,項目名稱:opfs,代碼行數:56,代碼來源:jpeg.go

示例14: maskImage

func maskImage(dst draw.Image, filename string) {
	infile, err := os.Open(filename)
	defer infile.Close()

	if err != nil {
		term.OutputError(fmt.Sprintf("Unable to open overlay image - %s", err.Error()))
	} else {
		overlay, _, err := image.Decode(infile)
		if err != nil {
			term.OutputError(fmt.Sprintf("Unable to decode image file - %s", err.Error()))
		} else {
			mask := image.NewUniform(color.Alpha{128})
			draw.DrawMask(dst, dst.Bounds(), imaging.Thumbnail(overlay, 480, 480, imaging.CatmullRom), image.Pt(0, 0), mask, image.Pt(0, 0), draw.Over)
		}
	}
}
開發者ID:Ruxton,項目名稱:mix_cover_builder,代碼行數:16,代碼來源:buildcover.go

示例15: Resize

//Resize with mode crop. Does the acutal resizing and returns the image
//errors only if dstWidth or dstHeight is invalid
func (c CropResizer) Resize(input image.Image, dstWidth, dstHeight int) (image.Image, error) {
	if dstWidth < 0 && dstHeight < 0 {
		return nil, fmt.Errorf("Either width or height must be greater zero to keep the existing ratio")
	}

	originalBounds := input.Bounds()
	originalRatio := float64(originalBounds.Dx()) / float64(originalBounds.Dy())

	if dstWidth < 0 {
		dstWidth = int(float64(dstHeight) * originalRatio)
	}

	if dstHeight < 0 {
		dstHeight = int(float64(dstWidth) / originalRatio)
	}

	return imaging.Thumbnail(input, dstWidth, dstHeight, imaging.Lanczos), nil
}
開發者ID:marvin0815,項目名稱:gridfs-image-server,代碼行數:20,代碼來源:resize.go


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