Exemplo n.º 1
0
// Auto-orientates canvas based on its original image's EXIF metadata
func (self *Canvas) AutoOrientate() error {

	data := self.Metadata()

	orientation, err := strconv.Atoi(data["exif:Orientation"])

	if err != nil {
		return err
	}

	switch uint(orientation) {
	case TOP_LEFT_ORIENTATION:
		// normal

	case TOP_RIGHT_ORIENTATION:
		self.Flop()

	case BOTTOM_RIGHT_ORIENTATION:
		self.RotateCanvas(math.Pi)

	case BOTTOM_LEFT_ORIENTATION:
		self.Flip()

	case LEFT_TOP_ORIENTATION:
		self.Flip()
		self.RotateCanvas(-math.Pi / 2)

	case RIGHT_TOP_ORIENTATION:
		self.RotateCanvas(math.Pi / 2)

	case RIGHT_BOTTOM_ORIENTATION:
		self.Flop()
		self.RotateCanvas(-math.Pi / 2)

	case LEFT_BOTTOM_ORIENTATION:
		self.RotateCanvas(-math.Pi / 2)

	default:
		return errors.New("No orientation data found in file.")
	}

	success := C.MagickSetImageOrientation(self.wand, (C.OrientationType)(TOP_LEFT_ORIENTATION))

	if success == C.MagickFalse {
		return fmt.Errorf("Could not orientate photo: %s", self.Error())
	}

	self.SetMetadata("exif:Orientation", (string)(TOP_LEFT_ORIENTATION))

	return nil
}
Exemplo n.º 2
0
// Auto-orientates canvas based on its original image's EXIF metadata
func (cv Canvas) AutoOrientate() bool {

	data := cv.Metadata()

	orientation, err := strconv.Atoi(data["exif:Orientation"])

	if err != nil {
		return false
	}

	switch uint(orientation) {
	case TOP_LEFT_ORIENTATION:
		// normal

	case TOP_RIGHT_ORIENTATION:
		cv.Flop()

	case BOTTOM_RIGHT_ORIENTATION:
		cv.RotateCanvas(math.Pi)

	case BOTTOM_LEFT_ORIENTATION:
		cv.Flip()

	case LEFT_TOP_ORIENTATION:
		cv.Flip()
		cv.RotateCanvas(-math.Pi / 2)

	case RIGHT_TOP_ORIENTATION:
		cv.RotateCanvas(-math.Pi / 2)

	case RIGHT_BOTTOM_ORIENTATION:
		cv.Flop()
		cv.RotateCanvas(-math.Pi / 2)

	case LEFT_BOTTOM_ORIENTATION:
		cv.RotateCanvas(math.Pi / 2)

	default:
		return false
	}

	C.MagickSetImageOrientation(cv.wand, (C.OrientationType)(TOP_LEFT_ORIENTATION))
	cv.SetMetadata("exif:Orientation", (string)(TOP_LEFT_ORIENTATION))

	return true
}