Adding Images to a Slide

This page describes how to add images to an existing Google Slides presentation. When adding an image, you provide the Slides API with a publicly accessible URL to the image.

About images

Images in the Slides API are a type of page element. As with any page element, you specify the visual size and position of the image using the size and transform properties of the PageElement. For more details on how to correctly size and position your image, see Size and Position Shapes.

Example

This example uses the Slides API to add an image to a presentation.

Apps Script

slides/api/Snippets.gs
// Create a new image, using the supplied object ID, with content downloaded from imageUrl.
var requests = [];
var imageId = 'MyImage_01';
var imageUrl = 'https://www.google.com/images/branding/googlelogo/2x/' +
    'googlelogo_color_272x92dp.png';
var emu4M = {
  magnitude: 4000000,
  unit: 'EMU'
};
requests.push({
  createImage: {
    objectId: imageId,
    url: imageUrl,
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: emu4M,
        width: emu4M
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 100000,
        translateY: 100000,
        unit: 'EMU'
      }
    }
  }
});

// Execute the request.
var response = Slides.Presentations.batchUpdate({
  requests: requests
}, presentationId);

var createImageResponse = response.replies;
console.log('Created image with ID: %s', createImageResponse[0].createImage.objectId);

Go

slides/snippets/presentations.go
// Create a new image, using the supplied object ID, with content downloaded from imageURL.
imageId := "MyImageId_01"
emu4M := slides.Dimension{Magnitude: 4000000, Unit: "EMU"}
requests := []*slides.Request{{
	CreateImage: &slides.CreateImageRequest{
		ObjectId: imageId,
		Url:      imageURL,
		ElementProperties: &slides.PageElementProperties{
			PageObjectId: slideId,
			Size: &slides.Size{
				Height: &emu4M,
				Width:  &emu4M,
			},
			Transform: &slides.AffineTransform{
				ScaleX:     1.0,
				ScaleY:     1.0,
				TranslateX: 100000.0,
				TranslateY: 100000.0,
				Unit:       "EMU",
			},
		},
	},
}}

// Execute the request.
body := &slides.BatchUpdatePresentationRequest{
	Requests: requests,
}
response, err := slidesService.Presentations.BatchUpdate(presentationId, body).Do()
if err != nil {
	log.Fatalf("Unable to create image. %v", err)
} else {
	fmt.Printf("Created image with ID: %s", response.Replies[0].CreateImage.ObjectId)
}

Java

slides/snippets/src/main/java/Snippets.java
String imageUrl = "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png";

// Create a new image, using the supplied object ID, with content downloaded from imageUrl.
List<Request> requests = new ArrayList<>();
String imageId = "MyImageId_01";
Dimension emu4M = new Dimension().setMagnitude(4000000.0).setUnit("EMU");
requests.add(new Request()
        .setCreateImage(new CreateImageRequest()
                .setObjectId(imageId)
                .setUrl(imageUrl)
                .setElementProperties(new PageElementProperties()
                        .setPageObjectId(slideId)
                        .setSize(new Size()
                                .setHeight(emu4M)
                                .setWidth(emu4M))
                        .setTransform(new AffineTransform()
                                .setScaleX(1.0)
                                .setScaleY(1.0)
                                .setTranslateX(100000.0)
                                .setTranslateY(100000.0)
                                .setUnit("EMU")))));

// Execute the request.
BatchUpdatePresentationRequest body =
        new BatchUpdatePresentationRequest().setRequests(requests);
BatchUpdatePresentationResponse response =
        slidesService.presentations().batchUpdate(presentationId, body).execute();
CreateImageResponse createImageResponse = response.getReplies().get(0).getCreateImage();
System.out.println("Created image with ID: " + createImageResponse.getObjectId());

JavaScript

slides/snippets/snippets.js
// Create a new image, using the supplied object ID, with content downloaded from imageUrl.
var requests = [];
var imageId = 'MyImage_01';
var emu4M = {
  magnitude: 4000000,
  unit: 'EMU'
};
requests.push({
  createImage: {
    objectId: imageId,
    url: imageUrl,
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: emu4M,
        width: emu4M
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 100000,
        translateY: 100000,
        unit: 'EMU'
      }
    }
  }
});

// Execute the request.
gapi.client.slides.presentations.batchUpdate({
  presentationId: presentationId,
  requests: requests
}).then((response) => {
  var createImageResponse = response.result.replies;
  console.log(`Created image with ID: ${createImageResponse[0].createImage.objectId}`);
});

Node.js

slides/snippets/snippets.js
// Create a new image, using the supplied object ID, with content downloaded from imageUrl.
let imageId = 'MyImage_01';
let emu4M = {
  magnitude: 4000000,
  unit: 'EMU',
};
let requests = [{
  createImage: {
    objectId: imageId,
    url: imageUrl,
    elementProperties: {
      pageObjectId: pageId,
      size: {
        height: emu4M,
        width: emu4M,
      },
      transform: {
        scaleX: 1,
        scaleY: 1,
        translateX: 100000,
        translateY: 100000,
        unit: 'EMU',
      },
    },
  },
}];

// Execute the request.
this.slidesService.presentations.batchUpdate({
  presentationId,
  resource: {requests},
}, (err, response) => {
  let createImageResponse = response.replies;
  console.log(`Created image with ID: ${createImageResponse[0].createImage.objectId}`);
});

PHP

slides/snippets/src/SlidesSnippets.php
<?php
// Create a new image, using the supplied object ID, with content downloaded from imageUrl.
$imageId = 'MyImage_01';
$emu4M = array('magnitude' => 4000000, 'unit' => 'EMU');
$requests = array();
$requests[] = new Google_Service_Slides_Request(array(
    'createImage' => array(
        'objectId' => $imageId,
        'url' => $imageUrl,
        'elementProperties' => array(
            'pageObjectId' => $pageId,
            'size' => array(
                'height' => $emu4M,
                'width' => $emu4M
            ),
            'transform' => array(
                'scaleX' => 1,
                'scaleY' => 1,
                'translateX' => 100000,
                'translateY' => 100000,
                'unit' => 'EMU'
            )
        )
    )
));

// Execute the request.
$batchUpdateRequest = new Google_Service_Slides_BatchUpdatePresentationRequest(array(
    'requests' => $requests
));
$response = $slidesService->presentations->batchUpdate($presentationId, $batchUpdateRequest);
$createImageResponse = $response->getReplies()[0]->getCreateImage();
printf("Created image with ID: %s\n", $createImageResponse->getObjectId());

Python

slides/snippets/slides_snippets.py
# Create a new image, using the supplied object ID,
# with content downloaded from IMAGE_URL.
IMAGE_URL = ('https://www.google.com/images/branding/'
             'googlelogo/2x/googlelogo_color_272x92dp.png')
requests = []
image_id = 'MyImage_01'
emu4M = {
    'magnitude': 4000000,
    'unit': 'EMU'
}
requests.append({
    'createImage': {
        'objectId': image_id,
        'url': IMAGE_URL,
        'elementProperties': {
            'pageObjectId': page_id,
            'size': {
                'height': emu4M,
                'width': emu4M
            },
            'transform': {
                'scaleX': 1,
                'scaleY': 1,
                'translateX': 100000,
                'translateY': 100000,
                'unit': 'EMU'
            }
        }
    }
})

# Execute the request.
body = {
    'requests': requests
}
response = slides_service.presentations() \
    .batchUpdate(presentationId=presentation_id, body=body).execute()
create_image_response = response.get('replies')[0].get('createImage')
print('Created image with ID: {0}'.format(
    create_image_response.get('objectId')))

Ruby

slides/snippets/lib/file_snippets.rb
# Create a new image, using the supplied object ID, with content downloaded from image_url.
requests = []
image_id = 'MyImage_01'
emu4M = {
  magnitude: '4000000',
  unit:      'EMU'
}
requests << {
  create_image: {
    object_id_prop:     image_id,
    url:                IMAGE_URL,
    element_properties: {
      page_object_id: page_id,
      size:           {
        height: emu4M,
        width:  emu4M
      },
      transform:      {
        scale_x:     '1',
        scale_y:     '1',
        translate_x: '100000',
        translate_y: '100000',
        unit:        'EMU'
      }
    }
  }
}

# Execute the request.
req = Google::Apis::SlidesV1::BatchUpdatePresentationRequest.new(requests: requests)
response = slides_service.batch_update_presentation(
  presentation_id,
  req
)
create_image_response = response.replies[0].create_image
puts "Created image with ID: #{create_image_response.object_id}"

Оставить отзыв о...

Текущей странице
Нужна помощь? Обратитесь в службу поддержки.