google-apps-scriptgoogle-slides-apigoogle-slides

flip a image in google slides using google apps script


I want to flip image using apps script. But its not flipping in place. I want to flip in place and not to move the image to a different location. I tried with

function myFunction() {
  var image = SlidesApp.getActivePresentation().getSlides()[0].getImages()[0]
  image.scaleWidth(-1)
}

The image

enter image description here

The expected

enter image description here

Obtained result

enter image description here


Solution

  • You need to move the position a size:

      const t = img.getTransform()
      img.setTransform(
        t.toBuilder()
          .setScaleX(-t.getScaleX())
          .setTranslateX(t.getTranslateX() + t.getScaleX() * img.getInherentWidth())
          .build()
      )
    

    This flips the image by changing the matrix transform. Basically it's the scale and the move by the size of the image.

    References