示例#1
0
文件: image.go 项目: rosylilly/gofu
func (i *Image) Process(wand *imagick.MagickWand, q *Query) {
	defer wand.Clear()

	// wand.SetOption("jpeg:size", fmt.Sprintf("%dx%d", q.ResizedWidth, q.ResizedHeight))

	bench("wand read", func() { wand.ReadImage(i.Path) })
	bench("wand set image format", func() { wand.SetImageFormat("jpeg") })
	bench("wand set compress", func() { wand.SetCompression(imagick.COMPRESSION_JPEG2000) })
	bench("wand set image quality", func() { wand.SetImageCompressionQuality(95) })

	bench("wand resize", func() {
		i.resize(wand, q.ResizedWidth, q.ResizedHeight)
	})

	bench("wand strip", func() {
		wand.StripImage()
	})

	i.Blob = wand.GetImageBlob()
	i.Processed = true
}
示例#2
0
func convert(mw *imagick.MagickWand, request *ZRequest) error {

	fmt.Println("【zscale】:调用convert()方法......处理图片参数")

	var result error
	result = nil
	mw.ResetIterator()
	mw.SetImageOrientation(imagick.ORIENTATION_TOP_LEFT)

	x := request.X
	y := request.Y
	cols := uint(request.Width)
	rows := uint(request.Height)

	fmt.Sprintf("image cols %d, rows %d \n", cols, rows)

	if !(cols == 0 && rows == 0) {
		//fmt.Println("call crop&scal function......")

		/* crop and scale */
		if x == -1 && y == -1 {
			//	fmt.Println("call crop&scal function......")

			//	fmt.Print(fmt.Sprintf("proportion(im, %d, %d, %d) \n", request.Proportion, cols, rows))
			result = proportion(mw, request.Proportion, cols, rows)
			if result != nil {
				return result
			}
		} else {

			//	fmt.Print(fmt.Sprintf("crop(im, %d, %d, %d, %d) \n", x, y, cols, rows))

			result = crop(mw, x, y, cols, rows)
			if result != nil {
				return result
			}
		}
	}

	/* rotate image */
	if request.Rotate != 0 {
		//	fmt.Print(fmt.Sprintf("wi_rotate(im, %d) \n", request.Rotate))

		background := imagick.NewPixelWand()
		if background == nil {
			result = fmt.Errorf("init new pixelwand faile.")
			return result
		}
		defer background.Destroy()
		isOk := background.SetColor("#ffffff")
		if !isOk {
			result = fmt.Errorf("set background color faile.")
			return result
		}

		result = mw.RotateImage(background, float64(request.Rotate))
		if result != nil {
			return result
		}
	}

	/* set gray */
	if request.Gary == 1 {
		//	fmt.Print(fmt.Sprintf("wi_gray(im) \n"))
		result = mw.SetImageType(imagick.IMAGE_TYPE_GRAYSCALE)
		if result != nil {
			return result
		}
	}

	/* set quality */
	//fmt.Print(fmt.Sprintf("wi_set_quality(im, %d) \n", request.Quality))
	result = mw.SetImageCompressionQuality(uint(request.Quality))
	if result != nil {
		return result
	}

	/* set format */
	if "none" != request.Format {
		//		fmt.Print(fmt.Sprintf("wi_set_format(im, %s) \n", request.Format))
		result = mw.SetImageFormat(request.Format)
		if result != nil {
			return result
		}
	}

	//fmt.Print(fmt.Sprintf("convert(im, req) %s \n", result))

	return result
}
示例#3
0
func (i *Image) sizeFrames(sz *imgry.Sizing) error {
	var canvas *imagick.MagickWand
	var bg *imagick.PixelWand

	// Shortcut if there is nothing to size
	if sz.Size.Equal(imgry.ZeroRect) && sz.CropBox.Equal(imgry.ZeroFloatingRect) {
		return nil
	}

	coalesceAndDeconstruct := !sz.Flatten && i.mw.GetNumberImages() > 1
	if coalesceAndDeconstruct {
		i.mw = i.mw.CoalesceImages()
	}

	if sz.Canvas != nil {
		// If the user requested a canvas.
		canvas = imagick.NewMagickWand()
		bg = imagick.NewPixelWand()
		bg.SetColor("transparent")

		defer func() {
			bg.Destroy()
			if canvas != nil && canvas != i.mw {
				canvas.Destroy()
			}
		}()
	}

	i.mw.SetFirstIterator()
	for n := true; n; n = i.mw.NextImage() {
		pw, ph := int(i.mw.GetImageWidth()), int(i.mw.GetImageHeight())
		srcSize := imgry.NewRect(pw, ph)

		// Initial crop of the source image
		cropBox, cropOrigin, err := sz.CalcCropBox(srcSize)
		if err != nil {
			return err
		}

		if cropBox != nil && cropOrigin != nil && !cropBox.Equal(imgry.ZeroRect) {
			err := i.mw.CropImage(uint(cropBox.Width), uint(cropBox.Height), cropOrigin.X, cropOrigin.Y)
			if err != nil {
				return err
			}
			srcSize = cropBox
			i.mw.ResetImagePage("")
		}

		// Resize the image
		resizeRect, cropBox, cropOrigin := sz.CalcResizeRect(srcSize)
		if resizeRect != nil && !resizeRect.Equal(imgry.ZeroRect) {
			err := i.mw.ResizeImage(uint(resizeRect.Width), uint(resizeRect.Height), imagick.FILTER_LANCZOS, 1.0)
			if err != nil {
				return err
			}
			i.mw.ResetImagePage("")
		}

		// Perform any final crops from an operation
		if cropBox != nil && cropOrigin != nil && !cropBox.Equal(imgry.ZeroRect) {
			err := i.mw.CropImage(uint(cropBox.Width), uint(cropBox.Height), cropOrigin.X, cropOrigin.Y)
			if err != nil {
				return err
			}
			i.mw.ResetImagePage("")
		}

		// If we have a canvas we put the image at its center.
		if canvas != nil {
			canvas.NewImage(uint(sz.Canvas.Width), uint(sz.Canvas.Height), bg)
			canvas.SetImageBackgroundColor(bg)
			canvas.SetImageFormat(i.mw.GetImageFormat())

			x := (sz.Canvas.Width - int(i.mw.GetImageWidth())) / 2
			y := (sz.Canvas.Height - int(i.mw.GetImageHeight())) / 2
			canvas.CompositeImage(i.mw, imagick.COMPOSITE_OP_OVER, x, y)
			canvas.ResetImagePage("")
		}

		if sz.Flatten {
			break
		}
	}

	if canvas != nil {
		i.mw.Destroy()
		i.mw = canvas
	}

	if coalesceAndDeconstruct {
		// Compares each frame of the image, removes pixels that are already on the
		// background and updates offsets accordingly.
		i.mw = i.mw.DeconstructImages()
	}

	return nil
}