@Test public void testUpdate() throws Exception { try { api.createStreamingProfile(UPDATE_PROFILE_NAME, null, Collections.singletonList(ObjectUtils.asMap( "transformation", new Transformation().crop("limit").width(1200).height(1200).bitRate("5m") )), ObjectUtils.emptyMap()); } catch (AlreadyExists ignored) { } Map result = api.updateStreamingProfile(UPDATE_PROFILE_NAME, null, Collections.singletonList( ObjectUtils.asMap("transformation", new Transformation().crop("limit").width(800).height(800).bitRate("5m") )), ObjectUtils.emptyMap()); assertTrue(result.containsKey("data")); assertThat(result, (Matcher) hasEntry("message", (Object) "updated")); Map profile = (Map) result.get("data"); assertThat(profile, (Matcher) hasEntry("name", (Object) UPDATE_PROFILE_NAME)); assertThat(profile, Matchers.hasEntry(equalTo("representations"), (Matcher) hasItem(hasKey("transformation")))); final Map representation = (Map) ((List) profile.get("representations")).get(0); Map transformation = (Map) ((List) representation.get("transformation")).get(0); assertThat(transformation, allOf( (Matcher) hasEntry("width", 800), (Matcher) hasEntry("height", 800), (Matcher) hasEntry("crop", "limit"), (Matcher) hasEntry("bit_rate", "5m") )); }
@Test public void testDelete() throws Exception { ApiResponse result; try { api.createStreamingProfile(DELETE_PROFILE_NAME, null, Collections.singletonList(ObjectUtils.asMap( "transformation", new Transformation().crop("limit").width(1200).height(1200).bitRate("5m") )), ObjectUtils.emptyMap()); } catch (AlreadyExists ignored) { } result = api.deleteStreamingProfile(DELETE_PROFILE_NAME); assertEquals("deleted", result.get("message")); }
/** * Construct the final url with the dimensions included as the last transformation in the url. * @param view The view to adapt the image size to. * @param baseUrl The base cloudinary Url to chain the transformation to. * @return The url with the responsive transformation. */ private Url buildUrl(View view, Url baseUrl) { // add a new transformation on top of anything already there: Url url = baseUrl.clone(); url.transformation().chain(); if (autoHeight) { int contentHeight = view.getHeight() - view.getPaddingTop() - view.getPaddingBottom(); url.transformation().height(trimAndRoundUp(contentHeight)); } if (autoWidth) { int contentWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight(); url.transformation().width(trimAndRoundUp(contentWidth)); } url.transformation().crop(cropMode).gravity(gravity); return url; }
@Test public void testCreate() throws Exception { ApiResponse result = api.createStreamingProfile(CREATE_PROFILE_NAME, null, Collections.singletonList(ObjectUtils.asMap( "transformation", new Transformation().crop("limit").width(1200).height(1200).bitRate("5m") )), ObjectUtils.emptyMap()); assertTrue(result.containsKey("data")); Map profile = (Map) result.get("data"); assertThat(profile, (Matcher) hasEntry("name", (Object) CREATE_PROFILE_NAME)); }
public static String getCroppedThumbnailUrl(int size, Resource resource) { return MediaManager.get().getCloudinary().url() .resourceType(resource.getResourceType()) .transformation(new Transformation().gravity("auto").width(size).height(size)) .format("webp") .generate(resource.getCloudinaryPublicId()); }
@Test public void testShouldSupportIhIw() { String trans = new Transformation().width("iw").height("ih").crop("crop").generate(); assertEquals("c_crop,h_ih,w_iw", trans); }
@Test public void withLiteral() throws Exception { Transformation transformation = new Transformation().ifCondition("w_lt_200").crop("fill").height(120).width(80); assertEquals("should include the if parameter as the first component in the transformation string", "if_w_lt_200,c_fill,h_120,w_80", transformation.toString()); transformation = new Transformation().crop("fill").height(120).ifCondition("w_lt_200").width(80); assertEquals("should include the if parameter as the first component in the transformation string", "if_w_lt_200,c_fill,h_120,w_80", transformation.toString()); String chained = "[{if: \"w_lt_200\",crop: \"fill\",height: 120, width: 80}, {if: \"w_gt_400\",crop: \"fit\",width: 150,height: 150},{effect: \"sepia\"}]"; List transformations = ObjectUtils.toList(new JSONArray(chained)); transformation = new Transformation(transformations); assertEquals("should allow multiple conditions when chaining transformations", "if_w_lt_200,c_fill,h_120,w_80/if_w_gt_400,c_fit,h_150,w_150/e_sepia", transformation.toString()); }
@Test public void testCrop() { Transformation transformation = new Transformation().width(100).height(101); String result = cloudinary.url().transformation(transformation).generate("test"); assertEquals(DEFAULT_UPLOAD_PATH + "h_101,w_100/test", result); assertEquals("101", transformation.getHtmlHeight()); assertEquals("100", transformation.getHtmlWidth()); transformation = new Transformation().width(100).height(101).crop("crop"); result = cloudinary.url().transformation(transformation).generate("test"); assertEquals(DEFAULT_UPLOAD_PATH + "c_crop,h_101,w_100/test", result); }
@Test public void testSignedUrl() { // should correctly sign a url String expected = DEFAULT_UPLOAD_PATH + "s--Ai4Znfl3--/c_crop,h_20,w_10/v1234/image.jpg"; String actual = cloudinary.url().version(1234).transformation(new Transformation().crop("crop").width(10).height(20)).signed(true) .generate("image.jpg"); assertEquals(expected, actual); expected = DEFAULT_UPLOAD_PATH + "s----SjmNDA--/v1234/image.jpg"; actual = cloudinary.url().version(1234).signed(true).generate("image.jpg"); assertEquals(expected, actual); expected = DEFAULT_UPLOAD_PATH + "s--Ai4Znfl3--/c_crop,h_20,w_10/image.jpg"; actual = cloudinary.url().transformation(new Transformation().crop("crop").width(10).height(20)).signed(true).generate("image.jpg"); assertEquals(expected, actual); }
@Test public void testResponsiveWidth() { // should support responsive width Transformation trans = new Transformation().width(100).height(100).crop("crop").responsiveWidth(true); String result = cloudinary.url().transformation(trans).generate("test"); assertTrue(trans.isResponsive()); assertEquals(DEFAULT_UPLOAD_PATH + "c_crop,h_100,w_100/c_limit,w_auto/test", result); Transformation.setResponsiveWidthTransformation(asMap("width", "auto", "crop", "pad")); trans = new Transformation().width(100).height(100).crop("crop").responsiveWidth(true); result = cloudinary.url().transformation(trans).generate("test"); assertTrue(trans.isResponsive()); assertEquals(DEFAULT_UPLOAD_PATH + "c_crop,h_100,w_100/c_pad,w_auto/test", result); Transformation.setResponsiveWidthTransformation(null); }
@Test public void testUnderlay() { Transformation transformation = new Transformation().underlay("text:hello"); String result = cloudinary.url().transformation(transformation).generate("test"); assertEquals(DEFAULT_UPLOAD_PATH + "u_text:hello/test", result); // should not pass width/height to html if underlay transformation = new Transformation().underlay("text:hello").width(100).height(100); result = cloudinary.url().transformation(transformation).generate("test"); assertNull(transformation.getHtmlHeight()); assertNull(transformation.getHtmlWidth()); assertEquals(DEFAULT_UPLOAD_PATH + "h_100,u_text:hello,w_100/test", result); }
@SuppressWarnings("unchecked") @Test public void testImageTag() { Transformation transformation = new Transformation().width(100).height(101).crop("crop"); String result = cloudinary.url().transformation(transformation).imageTag("test", asMap("alt", "my image")); assertEquals("<img src='http://res.cloudinary.com/test123/image/upload/c_crop,h_101,w_100/test' alt='my image' height='101' width='100'/>", result); transformation = new Transformation().width(0.9).height(0.9).crop("crop").responsiveWidth(true); result = cloudinary.url().transformation(transformation).imageTag("test", asMap("alt", "my image")); assertEquals( "<img alt='my image' class='cld-responsive' data-src='http://res.cloudinary.com/test123/image/upload/c_crop,h_0.9,w_0.9/c_limit,w_auto/test'/>", result); result = cloudinary.url().transformation(transformation).imageTag("test", asMap("alt", "my image", "class", "extra")); assertEquals( "<img alt='my image' class='extra cld-responsive' data-src='http://res.cloudinary.com/test123/image/upload/c_crop,h_0.9,w_0.9/c_limit,w_auto/test'/>", result); transformation = new Transformation().width("auto").crop("crop"); result = cloudinary.url().transformation(transformation).imageTag("test", asMap("alt", "my image", "responsive_placeholder", "blank")); assertEquals( "<img src='data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7' alt='my image' class='cld-responsive' data-src='http://res.cloudinary.com/test123/image/upload/c_crop,w_auto/test'/>", result); result = cloudinary.url().transformation(transformation).imageTag("test", asMap("alt", "my image", "responsive_placeholder", "other.gif")); assertEquals( "<img src='other.gif' alt='my image' class='cld-responsive' data-src='http://res.cloudinary.com/test123/image/upload/c_crop,w_auto/test'/>", result); }
@Test public void testOverlay() { // should support overlay Transformation transformation = new Transformation().overlay("text:hello"); String result = cloudinary.url().transformation(transformation).generate("test"); assertEquals(DEFAULT_UPLOAD_PATH + "l_text:hello/test", result); // should not pass width/height to html if overlay transformation = new Transformation().overlay("text:hello").width(100).height(100); result = cloudinary.url().transformation(transformation).generate("test"); assertNull(transformation.getHtmlHeight()); assertNull(transformation.getHtmlWidth()); assertEquals(DEFAULT_UPLOAD_PATH + "h_100,l_text:hello,w_100/test", result); transformation = new Transformation().overlay(new TextLayer().text("goodbye")); result = cloudinary.url().transformation(transformation).generate("test"); assertEquals(DEFAULT_UPLOAD_PATH + "l_text:goodbye/test", result); }