Skip to content

Commit fc49f81

Browse files
author
Amir Tocker
committed
Update code samples in the README file. Fixes #135
1 parent 1a3c2bf commit fc49f81

File tree

1 file changed

+71
-34
lines changed

1 file changed

+71
-34
lines changed

README.md

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,20 @@ Each request to our secure APIs (e.g., image uploads, eager sprite generation) m
6363

6464
Setting the `cloud_name`, `api_key` and `api_secret` parameters can be done either directly in each call to a Cloudinary method, by calling the cloudinary.config(), or by using the CLOUDINARY_URL environment variable.
6565

66+
### Require the Cloudinary library
67+
68+
```js
69+
var cloudinary = require('cloudinary').v2
70+
```
71+
6672
### Overriding the request agent
6773
To override the request agent pass the agent into any method that makes a
6874
request and it will be used instead of the normal https agent. e.g
69-
```js
7075

76+
```js
7177
cloudinary.uploader.upload_stream(
72-
function(result) { console.log(result); },
73-
{ agent: myAgent }
78+
{ agent: myAgent },
79+
function(error, result) { console.log(result); }
7480
);
7581

7682
```
@@ -81,21 +87,29 @@ Any image uploaded to Cloudinary can be transformed and embedded using powerful
8187

8288
The following example generates the url for accessing an uploaded `sample` image while transforming it to fill a 100x150 rectangle:
8389

84-
cloudinary.url("sample.jpg", {width: 100, height: 150, crop: "fill"})
90+
```js
91+
cloudinary.url("sample.jpg", {width: 100, height: 150, crop: "fill"})
92+
```
8593

8694
Another example, emedding a smaller version of an uploaded image while generating a 90x90 face detection based thumbnail:
8795

88-
cloudinary.url("woman.jpg", {width: 90, height: 90, crop: "thumb", gravity: "face"})
96+
```js
97+
cloudinary.url("woman.jpg", {width: 90, height: 90, crop: "thumb", gravity: "face"});
98+
```
8999

90100
You can provide either a Facebook name or a numeric ID of a Facebook profile or a fan page.
91101

92102
Embedding a Facebook profile to match your graphic design is very simple:
93103

94-
cloudinary.url("billclinton.jpg", {width: 90, height: 130, type: "facebook", crop: "fill", gravity: "north_west"})
104+
```js
105+
cloudinary.url("billclinton.jpg", {width: 90, height: 130, type: "facebook", crop: "fill", gravity: "north_west"});
106+
```
95107

96108
Same goes for Twitter:
97109

98-
cloudinary.url("billclinton.jpg", {type: "twitter_name"})
110+
```js
111+
cloudinary.url("billclinton.jpg", {type: "twitter_name"});
112+
```
99113

100114
![](https://res.cloudinary.com/cloudinary/image/upload/see_more_bullet.png) **See [our documentation](https://cloudinary.com/documentation/node_image_manipulation) for more information about displaying and transforming images in Node.js**.
101115

@@ -105,65 +119,88 @@ Assuming you have your Cloudinary configuration parameters defined (`cloud_name`
105119

106120
The following example uploads a local JPG to the cloud:
107121

108-
var cloudinary = require('cloudinary')
109-
110-
cloudinary.uploader.upload("my_picture.jpg", function(result) { console.log(result) })
122+
```js
123+
var cloudinary = require('cloudinary').v2;
124+
cloudinary.uploader.upload("my_picture.jpg", function(error, result) { console.log(result) });
125+
```
111126

112127
Below is an example of an upload's result:
113128

114-
{ public_id: '4srvcynxrf5j87niqcx6w',
115-
version: 1340625837,
116-
signature: '01234567890abcdef01234567890abcdef012345',
117-
width: 200,
118-
height: 200,
119-
format: 'jpg',
120-
resource_type: 'image',
121-
url: 'http://res.cloudinary.com/demo/image/upload/v1340625837/4srvcynxrf5j87niqcx6w.jpg',
122-
secure_url: 'https://res.cloudinary.com/demo/image/upload/v1340625837/4srvcynxrf5j87niqcx6w.jpg' }
129+
```json
130+
{
131+
"public_id": "4srvcynxrf5j87niqcx6w",
132+
"version": 1340625837,
133+
"signature": "01234567890abcdef01234567890abcdef012345",
134+
"width": 200,
135+
"height": 200,
136+
"format": "jpg",
137+
"resource_type": "image",
138+
"url": "http://res.cloudinary.com/demo/image/upload/v1340625837/4srvcynxrf5j87niqcx6w.jpg",
139+
"secure_url": "https://res.cloudinary.com/demo/image/upload/v1340625837/4srvcynxrf5j87niqcx6w.jpg"
140+
}
141+
```
123142

124143
The uploaded image is assigned a randomly generated public ID. The image is immediately available for download through a CDN:
125144

126-
cloudinary.url("abcfrmo8zul1mafopawefg.jpg")
127-
128-
http://res.cloudinary.com/demo/image/upload/abcfrmo8zul1mafopawefg.jpg
145+
```js
146+
cloudinary.url("abcfrmo8zul1mafopawefg.jpg");
129147

148+
// http://res.cloudinary.com/demo/image/upload/abcfrmo8zul1mafopawefg.jpg
149+
```
130150
You can also specify your own public ID:
131151

132-
cloudinary.uploader.upload("http://www.example.com/image.jpg", function(result) { console.log(result) }, {public_id: 'sample_remote'})
152+
```js
153+
cloudinary.uploader.upload(
154+
"http://www.example.com/image.jpg",
155+
{public_id: 'sample_remote'},
156+
function(error, result) {
157+
console.log(result)
158+
}
159+
);
160+
161+
cloudinary.url("sample_remote.jpg")
133162

134-
cloudinary.url("sample_remote.jpg")
163+
// http://res.cloudinary.com/demo/image/upload/sample_remote.jpg
135164

136-
http://res.cloudinary.com/demo/image/upload/sample_remote.jpg
165+
```
137166

138167
![](https://res.cloudinary.com/cloudinary/image/upload/see_more_bullet.png) **See [our documentation](https://cloudinary.com/documentation/node_image_upload) for plenty more options of uploading to the cloud from your Node.js code or directly from the browser**.
139168

140169
### cloudinary.upload_stream
141170

142171
You can use cloudinary.upload_stream to write to the uploader as a stream:
143172

144-
var fs = require('fs');
145-
var stream = cloudinary.uploader.upload_stream(function(result) { console.log(result); });
146-
var file_reader = fs.createReadStream('my_picture.jpg', {encoding: 'binary'}).on('data', stream.write).on('end', stream.end);
173+
```js
174+
var fs = require('fs');
175+
var stream = cloudinary.uploader.upload_stream(function(error, result) { console.log(result); });
176+
var file_reader = fs.createReadStream('my_picture.jpg', {encoding: 'binary'}).on('data', stream.write).on('end', stream.end);
177+
```
147178

148179
#### Version 1.1 upload_stream change notes
149180
The `upload_stream` method was modified to return a `Transform` stream object, we advise to change the `on('data')` and `on('end')` to pipe API:
150181

151-
var file_reader = fs.createReadStream('my_picture.jpg').pipe(stream);
182+
```js
183+
var file_reader = fs.createReadStream('my_picture.jpg').pipe(stream);
152184

185+
```
153186
if you still need to use event chanining, you can wrap `stream.write` and `stream.end` with wrapper functions
154187

155-
var file_reader = fs.createReadStream('my_picture.jpg', {encoding: 'binary'}).
156-
on('data', function(data){stream.write(data)}).on('end', function(){stream.end()});
157-
188+
```js
189+
var file_reader = fs.createReadStream('my_picture.jpg', {encoding: 'binary'})
190+
.on('data', function(data){stream.write(data)})
191+
.on('end', function(){stream.end()});
192+
```
158193
### cloudinary.image
159194

160195
Returns an html image tag pointing to Cloudinary.
161196

162197
Usage:
163198

164-
cloudinary.image("sample", {format: "png", width: 100, height: 100, crop: "fill"})
199+
```js
200+
cloudinary.image("sample", {format: "png", width: 100, height: 100, crop: "fill"})
165201

166-
// <img src='http://res.cloudinary.com/demo/image/upload/c_fill,h_100,w_100/sample.png' height='100' width='100'/>
202+
// <img src='http://res.cloudinary.com/demo/image/upload/c_fill,h_100,w_100/sample.png' height='100' width='100'/>
203+
```
167204

168205
### Samples
169206

0 commit comments

Comments
 (0)