com.google.appengine.api.images.ServingUrlOptions Java Examples

The following examples show how to use com.google.appengine.api.images.ServingUrlOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestServingViaBlobstoreApi.java    From appengine-gcs-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testFoundUrl() throws IOException {
  GcsFilename gcsFilename =
      new GcsFilename(TestServingViaBlobstoreApi.class.getName(), "testFoundUrl");
  @SuppressWarnings("resource")
  GcsOutputChannel channel = GCS_SERVICE.createOrReplace(
      gcsFilename, new GcsFileOptions.Builder().mimeType("image/png").build());
  byte[] bytes = BaseEncoding.base64().decode(PNG);
  channel.write(ByteBuffer.wrap(bytes));
  channel.close();

  BlobKey blobKey = BLOB_STORE.createGsBlobKey(
      "/gs/" + gcsFilename.getBucketName() + "/" + gcsFilename.getObjectName());

  byte[] imageData = BLOB_STORE.fetchData(blobKey, 0, bytes.length);
  assertArrayEquals(bytes, imageData);

  ServingUrlOptions opts = ServingUrlOptions.Builder.withBlobKey(blobKey);
  opts.imageSize(bytes.length);
  String url = IMAGES_SERVICE.getServingUrl(opts);
  assertTrue(url.length() > 0);
}
 
Example #2
Source File: ImageServingUrlTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void servingUrlWithImageSize() throws Exception {
    ServingUrlOptions servingUrlOptions = ServingUrlOptions.Builder.withBlobKey(blobKey);
    String baseUrl = imagesService.getServingUrl(servingUrlOptions);
    String actualUrl = imagesService.getServingUrl(servingUrlOptions.imageSize(32).crop(false));
    String expectedUrl = baseUrl + "=s32";
    assertEquals(expectedUrl, actualUrl);
}
 
Example #3
Source File: ImageServingUrlTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void servingUrlWithImageSizeAndCrop() throws Exception {
    ServingUrlOptions servingUrlOptions = ServingUrlOptions.Builder.withBlobKey(blobKey);
    String baseUrl = imagesService.getServingUrl(servingUrlOptions);
    String actualUrl = imagesService.getServingUrl(servingUrlOptions.imageSize(32).crop(true));
    String expectedUrl = baseUrl + "=s32-c";
    assertEquals(expectedUrl, actualUrl);
}
 
Example #4
Source File: ImageServingUrlTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void servingUrlWithSecureFlagFalse() throws Exception {
    ServingUrlOptions servingUrlOptions = ServingUrlOptions.Builder.withBlobKey(blobKey);
    String url = imagesService.getServingUrl(servingUrlOptions.crop(false));
    assertStartsWith("http://", url);

    url = imagesService.getServingUrl(servingUrlOptions.imageSize(32).crop(false).secureUrl(false));
    assertStartsWith("http://", url);
}
 
Example #5
Source File: ImageServingUrlTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void servingUrlWithSecureFlagTrue() throws Exception {
    assumeEnvironment(Environment.APPSPOT, Environment.CAPEDWARF);

    ServingUrlOptions servingUrlOptions = ServingUrlOptions.Builder.withBlobKey(blobKey);
    String url = imagesService.getServingUrl(servingUrlOptions.secureUrl(true));
    assertStartsWith("https://", url);

    url = imagesService.getServingUrl(servingUrlOptions.imageSize(32).crop(false).secureUrl(true));
    assertStartsWith("https://", url);
}
 
Example #6
Source File: ImageServingUrlTest.java    From appengine-tck with Apache License 2.0 5 votes vote down vote up
@Test
public void servingUrlWithOptionsWithSecureFlag() throws Exception {
    assumeEnvironment(Environment.APPSPOT, Environment.CAPEDWARF);

    ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey);

    String url = imagesService.getServingUrl(options);
    assertStartsWith("http://", url);

    url = imagesService.getServingUrl(options.secureUrl(true));
    assertStartsWith("https://", url);
}
 
Example #7
Source File: PhotoServiceManager.java    From solutions-photo-sharing-demo-java with Apache License 2.0 5 votes vote down vote up
public String getPhotoDisplayUrl(BlobKey blobKey) {
  ServingUrlOptions options = ServingUrlOptions.Builder.withBlobKey(blobKey);
  options.imageSize(configManager.getPhotoThumbnailSizeInPixels());
  options.crop(configManager.isPhotoThumbnailCrop());
  try {
    return ImagesServiceFactory.getImagesService().getServingUrl(options);
  } catch (ImagesServiceFailureException e) {
    logger.severe("Failed to get image serving url: " + e.getMessage());
    return "";
  }
}
 
Example #8
Source File: ImagesServlet.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

  // [START original_image]
  // Read the image.jpg resource into a ByteBuffer.
  FileInputStream fileInputStream = new FileInputStream(new File("WEB-INF/image.jpg"));
  FileChannel fileChannel = fileInputStream.getChannel();
  ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size());
  fileChannel.read(byteBuffer);

  byte[] imageBytes = byteBuffer.array();

  // Write the original image to Cloud Storage
  gcsService.createOrReplace(
      new GcsFilename(bucket, "image.jpeg"),
      new GcsFileOptions.Builder().mimeType("image/jpeg").build(),
      ByteBuffer.wrap(imageBytes));
  // [END original_image]

  // [START resize]
  // Get an instance of the imagesService we can use to transform images.
  ImagesService imagesService = ImagesServiceFactory.getImagesService();

  // Make an image directly from a byte array, and transform it.
  Image image = ImagesServiceFactory.makeImage(imageBytes);
  Transform resize = ImagesServiceFactory.makeResize(100, 50);
  Image resizedImage = imagesService.applyTransform(resize, image);

  // Write the transformed image back to a Cloud Storage object.
  gcsService.createOrReplace(
      new GcsFilename(bucket, "resizedImage.jpeg"),
      new GcsFileOptions.Builder().mimeType("image/jpeg").build(),
      ByteBuffer.wrap(resizedImage.getImageData()));
  // [END resize]

  // [START rotate]
  // Make an image from a Cloud Storage object, and transform it.
  BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
  BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + bucket + "/image.jpeg");
  Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
  Transform rotate = ImagesServiceFactory.makeRotate(90);
  Image rotatedImage = imagesService.applyTransform(rotate, blobImage);

  // Write the transformed image back to a Cloud Storage object.
  gcsService.createOrReplace(
      new GcsFilename(bucket, "rotatedImage.jpeg"),
      new GcsFileOptions.Builder().mimeType("image/jpeg").build(),
      ByteBuffer.wrap(rotatedImage.getImageData()));
  // [END rotate]

  // [START servingUrl]
  // Create a fixed dedicated URL that points to the GCS hosted file
  ServingUrlOptions options =
      ServingUrlOptions.Builder.withGoogleStorageFileName("/gs/" + bucket + "/image.jpeg")
          .imageSize(150)
          .crop(true)
          .secureUrl(true);
  String url = imagesService.getServingUrl(options);
  // [END servingUrl]

  // Output some simple HTML to display the images we wrote to Cloud Storage
  // in the browser.
  PrintWriter out = resp.getWriter();
  out.println("<html><body>\n");
  out.println(
      "<img src='//storage.cloud.google.com/" + bucket + "/image.jpeg' alt='AppEngine logo' />");
  out.println(
      "<img src='//storage.cloud.google.com/"
          + bucket
          + "/resizedImage.jpeg' alt='AppEngine logo resized' />");
  out.println(
      "<img src='//storage.cloud.google.com/"
          + bucket
          + "/rotatedImage.jpeg' alt='AppEngine logo rotated' />");
  out.println("<img src='" + url + "' alt='Hosted logo' />");
  out.println("</body></html>\n");
}
 
Example #9
Source File: ImageServingUrlTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void servingUrlWithNonexistentBlobKeyThrowsException() throws Exception {
    imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(new BlobKey("nonexistentBlob")));
}
 
Example #10
Source File: ImageServingUrlTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void servingUrlAlwaysReturnsSameUrlForSameBlobKey() throws Exception {
    String servingUrl1 = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey));
    String servingUrl2 = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey));
    assertEquals(servingUrl1, servingUrl2);
}
 
Example #11
Source File: ImageServingUrlTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void servingUrlWithOptionsWithImageSize() throws Exception {
    String baseUrl = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey));
    String actualUrl = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey).imageSize(32));
    assertEquals(baseUrl + "=s32", actualUrl);
}
 
Example #12
Source File: ImageServingUrlTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void servingUrlWithOptionsWithImageSizeAndCrop() throws Exception {
    String baseUrl = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey));
    String actualUrl = imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey).imageSize(32).crop(true));
    assertEquals(baseUrl + "=s32-c", actualUrl);
}
 
Example #13
Source File: ImageServingUrlTest.java    From appengine-tck with Apache License 2.0 4 votes vote down vote up
@Test
public void deleteServingUrl() throws Exception {
    imagesService.getServingUrl(ServingUrlOptions.Builder.withBlobKey(blobKey));
    imagesService.deleteServingUrl(blobKey);
}