示例#1
0
文件: main.go 项目: nozaq/vips
func main() {
	filename := ""
	options := vips.Options{Extend: vips.EXTEND_WHITE}
	flag.StringVar(&filename, "file", "", "input file")
	flag.IntVar(&options.Width, "width", 0, "")
	flag.IntVar(&options.Height, "height", 0, "")
	flag.BoolVar(&options.Crop, "crop", false, "")
	flag.BoolVar(&options.Enlarge, "enlarge", false, "")
	flag.IntVar(&options.Quality, "quality", 90, "")
	flag.Parse()

	fmt.Fprintf(os.Stderr, "options: %+v\n", options)

	file, err := os.Open(filename)
	if err != nil {
		flag.PrintDefaults()
		fmt.Fprintln(os.Stderr, err)
		return
	}

	buf, err := ioutil.ReadAll(file)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}

	img, err := vips.Resize(buf, options)
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		return
	}
	os.Stdout.Write(img)
}
示例#2
0
func (image *Images) CreateThumbnail(thumb string) error {
	options := vips.Options{
		Width:        400,
		Height:       300,
		Crop:         false,
		Extend:       vips.EXTEND_WHITE,
		Interpolator: vips.BILINEAR,
		Gravity:      vips.CENTRE,
		Quality:      95,
	}

	f, _ := os.Open(image.Path)
	inBuf, _ := ioutil.ReadAll(f)
	defer f.Close()
	buf, err := vips.Resize(inBuf, options)
	if err != nil {
		fmt.Println("Error while opening original", image.Path, "-", err)
		return err
	}

	file, err := os.Create(thumb)
	if err != nil {
		fmt.Println("Error while creating thumbnail", thumb, "-", err)
		return err
	}
	defer file.Close()
	err = ioutil.WriteFile(thumb, buf, 777)
	if err != nil {
		fmt.Println("Error while saving thumbnail", thumb, "-", err)
		return err
	}
	return nil
}
示例#3
0
文件: speed.go 项目: nozaq/vips
func main() {

	options := vips.Options{Extend: vips.EXTEND_WHITE}
	flag.IntVar(&options.Width, "width", 800, "")
	flag.IntVar(&options.Height, "height", 600, "")
	flag.BoolVar(&options.Crop, "crop", false, "")
	flag.BoolVar(&options.Enlarge, "enlarge", false, "")
	flag.IntVar(&options.Quality, "quality", 90, "")
	flag.Parse()

	const N = 5
	images := make([][]byte, N)

	for i := 0; i < N; i++ {
		filename := fmt.Sprintf("%d.jpg", i+1)
		fmt.Println("reading:", filename)
		img, err := os.Open(filename)
		if err != nil {
			log.Fatal(err)
		}
		buf, err := ioutil.ReadAll(img)
		if err != nil {
			log.Fatal(err)
		}
		if err := img.Close(); err != nil {
			log.Fatal(err)
		}
		images[i] = buf
	}

	go printStats()

	workers := runtime.GOMAXPROCS(0)
	ch := make(chan []byte, workers)

	for i := 0; i < workers; i++ {
		go func() {
			for buf := range ch {
				timer.Time(func() {
					_, err := vips.Resize(buf, options)
					if err != nil {
						log.Fatal(err)
					}
				})
			}
		}()
	}

	for {
		ch <- images[rand.Intn(N)]
	}
}
示例#4
0
文件: manip.go 项目: danrjohnson/vip
func Resize(src io.Reader, c *CacheContext) (io.Reader, error) {
	raw, err := ioutil.ReadAll(src)
	if err != nil {
		return nil, err
	}

	options := vips.Options{
		Width:        c.Width,
		Crop:         true,
		Extend:       vips.EXTEND_WHITE,
		Interpolator: vips.BILINEAR,
		Gravity:      vips.CENTRE,
		Quality:      80,
	}

	if c.Crop {
		data := bytes.NewReader(raw)

		image, _, err := image.Decode(data)
		if err != nil {
			return nil, err
		}

		minDimension := int(math.Min(float64(image.Bounds().Size().X), float64(image.Bounds().Size().Y)))

		if minDimension < options.Width || options.Width == 0 {
			options.Width = minDimension
		}

		options.Height = options.Width
	}

	res, err := vips.Resize(raw, options)
	if err != nil {
		return nil, err
	}

	return bytes.NewBuffer(res), err
}
示例#5
0
func ResizeImage(srcImagePath, dstImagePath string, width, height int) bool {
	//check if file already exist, if so just return true
	if _, err := os.Stat(dstImagePath); os.IsExist(err) {
		return true
	}
	options := vips.Options{
		Width:        width,
		Height:       height,
		Crop:         false,
		Enlarge:      false,
		Extend:       vips.EXTEND_WHITE,
		Interpolator: vips.NOHALO,
		Gravity:      vips.CENTRE,
		Quality:      90,
	}
	f, _ := os.Open(srcImagePath)
	inBuf, _ := ioutil.ReadAll(f)
	buf, err := vips.Resize(inBuf, options)
	if err != nil {
		log.Println("VIPS Error:", err)
		return false
	}
	of, err := os.Create(dstImagePath)
	if err != nil {
		log.Println("Failed to open file to write to:", dstImagePath, " because: ", err)
		return false
	}
	defer func() {
		if err := of.Close(); err != nil {
			log.Println("Failed to close file: ", dstImagePath, " because: ", err)
		}
	}()
	if _, err := of.Write(buf); err != nil {
		log.Println("Failed to write to file: ", dstImagePath, "because: ", err)
		return false
	}
	return true
}
示例#6
0
func ResizeThumbnailVips(inBuf []byte) ([]byte, error) {
	return vips.Resize(inBuf, ThumbnailOption())
}
示例#7
0
func ResizeMediumVips(inBuf []byte) ([]byte, error) {
	return vips.Resize(inBuf, MediumOption())
}
示例#8
0
func ResizeLargeVips(inBuf []byte) ([]byte, error) {
	return vips.Resize(inBuf, LargeOption())
}