当前位置: 首页>>代码示例>>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;未经允许,请勿转载。