Skip to content

Commit a4cc4d2

Browse files
committed
Set up structure for adding more transformations
1 parent ccb10d8 commit a4cc4d2

File tree

6 files changed

+124
-25
lines changed

6 files changed

+124
-25
lines changed

package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"webpack-cli": "^4.9.2"
2828
},
2929
"dependencies": {
30-
"@cloudinary/url-gen": "^1.5.1",
31-
"lodash": "^4.17.21"
30+
"@cloudinary/url-gen": "^1.5.1"
3231
}
3332
}

src/fullExample.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {Cloudinary} from "@cloudinary/url-gen";
2+
import {Transformation} from "@cloudinary/url-gen";
3+
4+
// Import required actions.
5+
import {thumbnail, scale} from "@cloudinary/url-gen/actions/resize";
6+
import {byRadius} from "@cloudinary/url-gen/actions/roundCorners";
7+
import {sepia} from "@cloudinary/url-gen/actions/effect";
8+
import {source} from "@cloudinary/url-gen/actions/overlay";
9+
import {opacity,brightness} from "@cloudinary/url-gen/actions/adjust";
10+
import {byAngle} from "@cloudinary/url-gen/actions/rotate"
11+
12+
// Import required qualifiers.
13+
import {image} from "@cloudinary/url-gen/qualifiers/source";
14+
import {Position} from "@cloudinary/url-gen/qualifiers/position";
15+
import {compass} from "@cloudinary/url-gen/qualifiers/gravity";
16+
import {focusOn} from "@cloudinary/url-gen/qualifiers/gravity";
17+
import {FocusOn} from "@cloudinary/url-gen/qualifiers/focusOn";
18+
19+
export default function getFullExampleImage() {
20+
21+
// Create a Cloudinary instance and set your cloud name.
22+
const cld = new Cloudinary({
23+
cloud: {
24+
cloudName: 'demo'
25+
}
26+
});
27+
28+
// Use the image with public ID, 'front_face'.
29+
const myImage = cld.image('front_face');
30+
31+
// Apply the transformation.
32+
myImage
33+
.resize(thumbnail().width(150).height(150).gravity(focusOn(FocusOn.face()))) // Crop the image.
34+
.roundCorners(byRadius(20)) // Round the corners.
35+
.effect(sepia()) // Apply a sepia effect.
36+
.overlay( // Overlay the Cloudinary logo.
37+
source(
38+
image('cloudinary_icon_blue')
39+
.transformation(new Transformation()
40+
.resize(scale(50)) // Resize the logo.
41+
.adjust(opacity(60)) // Adjust the opacity of the logo.
42+
.adjust(brightness(200))) // Adjust the brightness of the logo.
43+
)
44+
.position(new Position().gravity(compass('south_east')).offsetX(5).offsetY(5)) // Position the logo.
45+
)
46+
.rotate(byAngle(10)) // Rotate the result.
47+
.format('png'); // Deliver as PNG. */
48+
49+
return myImage;
50+
}

src/index.js

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,76 @@
11
import './style.css';
2-
import quickstart from './quickstart.js';
2+
import getQuickstartImage from './quickstart.js';
3+
import getFullExampleImage from './fullExample.js';
4+
5+
async function getComponent(index) {
6+
7+
let description = "";
8+
let link = "";
9+
let linkText = "";
10+
let myImageUrl = "";
11+
12+
// Set the variables for the different images
13+
switch (index)
14+
{
15+
case 1:
16+
{
17+
description = "Crop an image to a square, as shown in the";
18+
link = "https://cloudinary.com/documentation/javascript_quick_start#2_add_cloudinary_to_your_code";
19+
linkText = "Quickstart guide";
20+
myImageUrl = getQuickstartImage().toURL();
21+
break;
22+
}
23+
case 2:
24+
{
25+
description = "Apply a range of transformations, as shown in the";
26+
link = "https://cloudinary.com/documentation/javascript_integration#full_example";
27+
linkText = "Full example";
28+
myImageUrl = getFullExampleImage().toURL();
29+
break;
30+
}
31+
default:
32+
{
33+
console.log("Oops! Outside of range.");
34+
}
35+
}
336

4-
async function getComponent() {
537
const element = document.createElement('div');
6-
const { default: _ } = await import('lodash');
38+
const spacing = document.createElement('div');
39+
const spacing2 = document.createElement('div');
40+
const anchor = document.createElement('a');
41+
const imgElement = document.createElement('img');
742

843
element.classList.add('text','App');
9-
element.innerHTML = "Crop an image to a square, as shown in the <br><a class=\"App-link\" href=\"https://cloudinary.com/documentation/react2_quick_start#2_add_cloudinary_to_your_code\" target=\"_blank\" rel=\"noopener noreferrer\">Quickstart guide</a></br>"
44+
spacing.classList.add('space');
45+
spacing2.classList.add('space');
1046

11-
const imgElement = document.createElement('img');
12-
element.appendChild(imgElement);
13-
const myImage = quickstart();
14-
imgElement.src = myImage.toURL();
15-
16-
return element;
47+
// Add the description
48+
element.innerHTML = description;
49+
50+
// Define the link
51+
anchor.setAttribute("href", link);
52+
anchor.classList.add('App-link');
53+
anchor.setAttribute("target", "_blank");
54+
anchor.setAttribute("rel", "noopener noreferrer");
55+
anchor.innerHTML = linkText;
1756

57+
// Add the link and some space to the div
58+
element.appendChild(anchor);
59+
element.appendChild(spacing);
1860

61+
// Set the src attribute of the img element
62+
imgElement.src = myImageUrl;
63+
64+
// Add the image and some space to the div
65+
element.appendChild(imgElement);
66+
element.appendChild(spacing2);
67+
68+
return element;
1969
}
2070

21-
getComponent().then((component) => {
22-
document.body.appendChild(component);
23-
});
71+
for (let i=1; i<3; i++)
72+
{
73+
getComponent(i).then((component) => {
74+
document.body.appendChild(component);
75+
});
76+
}

src/quickstart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Cloudinary} from "@cloudinary/url-gen";
22
import {fill} from "@cloudinary/url-gen/actions/resize";
33

4-
export default function quickstart() {
4+
export default function getQuickstartImage() {
55

66
const cld = new Cloudinary({
77
cloud: {

webpack.config.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ const HtmlWebpackPlugin = require('html-webpack-plugin');
44
module.exports = {
55
mode: 'production',
66
entry: {
7-
index: {
8-
import: './src/index.js',
9-
dependOn: 'shared',
10-
},
11-
shared: 'lodash',
7+
index: './src/index.js',
128
},
139
plugins: [
1410
new HtmlWebpackPlugin({

0 commit comments

Comments
 (0)