Repository navigation
Expand file tree
/
Copy pathTransformAndDeliver.java
More file actions
98 lines (86 loc) · 4.26 KB
/
Copy pathTransformAndDeliver.java
File metadata and controls
98 lines (86 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Build delivery URLs. This example makes no API calls and needs no credentials beyond a
* cloud name, because URL building is entirely local.
*
* Prerequisites
* - CLOUDINARY_URL set in the environment, or just a cloud name. Only cloud_name is used.
* - cloudinary-http5 on the classpath. Nothing is sent, but the Cloudinary constructor
* requires an HTTP adapter and throws java.lang.UnknownError without one.
*
* Run
* javac -cp "<classpath>" -d out examples/TransformAndDeliver.java
* java -cp "<classpath>:out" TransformAndDeliver
*
* How this differs in a real project
* URLs would be generated per request from IDs held in your datastore, and rendered by a
* template engine rather than printed.
*
* Docs: docs/transform-and-deliver-image.md
* docs/transform-and-deliver-video.md
*/
import com.cloudinary.Cloudinary;
import com.cloudinary.Transformation;
public class TransformAndDeliver {
private static final String IMAGE_PUBLIC_ID = "sample.jpg";
private static final String VIDEO_PUBLIC_ID = "dog.mp4";
public static void main(String[] args) {
try {
Cloudinary cloudinary = new Cloudinary();
if (cloudinary.config.cloudName == null) {
System.err.println("No Cloudinary configuration found. Set CLOUDINARY_URL.");
System.exit(1);
}
System.out.println("plain:");
System.out.println(" " + cloudinary.url().generate(IMAGE_PUBLIC_ID));
System.out.println("optimized (f_auto,q_auto - apply this by default):");
System.out.println(" " + cloudinary.url()
.transformation(new Transformation().fetchFormat("auto").quality("auto"))
.generate(IMAGE_PUBLIC_ID));
System.out.println("square thumbnail, content-aware crop:");
System.out.println(" " + cloudinary.url()
.transformation(new Transformation()
.width(300).height(300).crop("fill").gravity("auto")
.fetchFormat("auto").quality("auto"))
.generate(IMAGE_PUBLIC_ID));
System.out.println("chained - crop, then sharpen, then optimize:");
System.out.println(" " + cloudinary.url()
.transformation(new Transformation()
.width(400).height(400).crop("fill").gravity("auto")
.chain()
.effect("sharpen")
.chain()
.fetchFormat("auto").quality("auto"))
.generate(IMAGE_PUBLIC_ID));
System.out.println("responsive srcset (assembled by hand - the SDK has no builder):");
StringBuilder srcset = new StringBuilder();
for (int width : new int[]{400, 800, 1200}) {
if (srcset.length() > 0) {
srcset.append(", ");
}
srcset.append(cloudinary.url()
.transformation(new Transformation()
.width(width).crop("scale")
.fetchFormat("auto").quality("auto"))
.generate(IMAGE_PUBLIC_ID))
.append(" ").append(width).append("w");
}
System.out.println(" " + srcset);
System.out.println("video, scaled:");
System.out.println(" " + cloudinary.url().resourceType("video")
.transformation(new Transformation()
.width(640).crop("scale").quality("auto"))
.generate(VIDEO_PUBLIC_ID));
System.out.println("video poster frame at 2s:");
System.out.println(" " + cloudinary.url().resourceType("video").format("jpg")
.transformation(new Transformation()
.width(640).crop("fill").startOffset("2"))
.generate("dog"));
} catch (IllegalArgumentException e) {
System.err.println("Configuration error: " + e.getMessage());
System.exit(1);
} catch (RuntimeException e) {
System.err.println("Failed: " + e.getMessage());
System.exit(1);
}
}
}