Would it be possible to crop image using google app script without using any third party api

人盡茶涼 提交于 2021-01-28 06:06:11

问题


I want to crop an image with Google App Script if an image outside the page frame, but as far as I checked in Google App Script documentation and I could not find a way to crop the image. pageElements.asImage().replace (imgBlob, true); it is not allowed to pass cropping dimensions as parameters in .replace() to crop a image.

i know this can be achieved using a custom API , passing the image blob and crop area that will call cropping method on another server.

But how it will be possible to work with Google App Script, looking for expert advice.


回答1:


How about this answer?

Issue:

I think that in the current stage, replace(blobSource, crop) has the limitation. The official document says as follows.

crop Boolean: If true, crops the image to fit the existing image's size. Otherwise, the image is scaled and centered.

I confirmed that when the image is cropped using replace(blobSource, crop), the center of image is left. It seems that this is the current specification. And although there is the "cropProperties" of "UpdateImagePropertiesRequest" in Slides API, unfortunately, in the current stage, this cannot be still used. This has already been reported. Ref

Sample script:

If you use replace(blobSource, crop) under the current specification, how about the following sample script? As the sample situation, 2 images of "image1" and "image2" are prepared in the 1st slide, and "image1" is cropped using "image2".

The flow of this script is as follows.

Flow:

  1. Retrieve 2 images from a slide on Google Slides.
  2. Crop "image1" using "image2". By this, "image2" is replaced with "image1".
  3. Move the cropped image to "image1".
  4. Remove the original "image1".

Script:

function myFunction() {
  // 1. Retrieve 2 images from a slide on Google Slides.
  var slide = SlidesApp.getActivePresentation().getSlides()[0];
  var images = slide.getImages();
  var image1 = images[0];  // Red image.
  var image2 = images[1];  // Blue image.
  
  // 2. Crop "image1" using "image2". By this, "image2" is replaced with "image1".
  var replacedImage = image2.replace(image1.getBlob(), true);
  
  // 3. Move the cropped image to "image1".
  replacedImage.setTop(image1.getTop()).setLeft(image1.getLeft());
  
  // 4. Remove the original "image1".
  image1.remove();
}

Result:

When the script is run, "image1" is cropped. But it is found that in the current stage, the center of "image1" is left by the crop.

Note:

  • Slides API and Slides Service are growing now. So I think that this situation might be changed by the future update. But if you want this soon, how about requesting this to the issue tracker as the future request? Ref

References:

  • replace(blobSource, crop)
  • CropProperties

Added:

At an additional sample script for using replace(blobSource, crop), I would like to propose the method for using the self image. In this sample script, when the image is sticked out, the image of out of page is removed by cropping. The basic method is the same with above sample script.

Sample script:

function myFunction() {
  var s = SlidesApp.getActivePresentation();
  var slide = s.getSlides()[0];
  var images = slide.getImages();
  var image = images[0];
  var pageWidth = s.getPageWidth();
  var imagePosition = image.getLeft();
  var imageWidth = image.getWidth();
  var check = imagePosition + imageWidth - pageWidth;
  if (check > 0 && check < imageWidth) {
    image
      .duplicate()
      .setWidth(pageWidth - imagePosition)
      .asImage()
      .replace(image.getBlob(), true);
    image.remove();
  }
}

Result:

Note:

  • In this sample script, as a simple sample, I prepared only the right side of the horizontal direction. So when you want to remove the vertical direction, please modify the script for your actual situation.


来源:https://stackoverflow.com/questions/63251248/would-it-be-possible-to-crop-image-using-google-app-script-without-using-any-thi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!