Esempio n. 1
0
// Converts the current image into a thumbnail of the specified
// width and height preserving ratio. It uses Crop() to clip the
// image to the specified area.
//
// If width or height are bigger than the current image, a centered
// thumbnail will be produced.
//
// Is width and height are smaller than the current image, the image
// will be resized and cropped, if needed.
func (self *Canvas) Thumbnail(width uint, height uint) error {

	var ratio float64

	// Normalizing image.

	ratio = math.Min(float64(self.Width())/float64(width), float64(self.Height())/float64(height))

	if ratio < 1.0 {
		// Origin image is smaller than the thumbnail image.
		max := uint(math.Max(float64(width), float64(height)))

		// Empty replacement buffer with transparent background.
		replacement := New()

		replacement.SetBackgroundColor("none")

		replacement.Blank(max, max)

		// Putting original image in the center of the replacement canvas.
		replacement.AppendCanvas(self, int(int(width-self.Width())/2), int(int(height-self.Height())/2))

		// Replacing wand
		C.DestroyMagickWand(self.wand)

		self.wand = C.CloneMagickWand(replacement.wand)

	} else {
		// Is bigger, just resizing.
		err := self.Resize(uint(float64(self.Width())/ratio), uint(float64(self.Height())/ratio))
		if err != nil {
			return err
		}
	}

	// Now we have an image that we can use to crop the thumbnail from.
	err := self.Crop(int(int(self.Width()-width)/2), int(int(self.Height()-height)/2), width, height)

	if err != nil {
		return err
	}

	return nil
}
Esempio n. 2
0
// Makes an exact copy of the MagickWand object
func (mw *MagickWand) Clone() *MagickWand {
	clone := C.CloneMagickWand(mw.mw)
	return &MagickWand{clone}
}