or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

cloud-storage.mdconfiguration.mderror-handling.mdimage-sources.mdindex.mdresults.mdtransformations.md

results.mddocs/

0

# Results and Output

1

2

Methods for retrieving processed images as files, buffers, or metadata, with both Promise and callback support.

3

4

## Capabilities

5

6

### Getting Results

7

8

Retrieve the processing result for accessing image data and metadata.

9

10

```typescript { .api }

11

/**

12

* Get the processing result for the source

13

* @returns Result instance containing image data and metadata

14

*/

15

result(): Result;

16

```

17

18

**Usage Example:**

19

20

```typescript

21

const tinify = require("tinify");

22

23

tinify.key = "your-api-key";

24

25

const source = tinify.fromFile("image.jpg").resize({ method: "fit", width: 800 });

26

const result = source.result();

27

28

// Access metadata

29

const width = await result.width();

30

const height = await result.height();

31

const size = await result.size();

32

33

console.log(`Processed image: ${width}×${height}, ${size} bytes`);

34

```

35

36

### File Output

37

38

Save processed images directly to the filesystem with both Promise and callback support.

39

40

```typescript { .api }

41

/**

42

* Save processed image to file (Promise version)

43

* @param path - Output file path

44

* @returns Promise that resolves when file is written

45

*/

46

toFile(path: string): Promise<void>;

47

48

/**

49

* Save processed image to file (callback version)

50

* @param path - Output file path

51

* @param callback - Callback function receiving error or null

52

*/

53

toFile(path: string, callback: (err: Error | null) => void): void;

54

```

55

56

**Usage Examples:**

57

58

```typescript

59

const tinify = require("tinify");

60

61

tinify.key = "your-api-key";

62

63

// Promise-based file output

64

try {

65

await tinify.fromFile("input.jpg")

66

.resize({ method: "cover", width: 400, height: 300 })

67

.toFile("output.jpg");

68

console.log("Image saved successfully");

69

} catch (error) {

70

console.error("Failed to save image:", error.message);

71

}

72

73

// Callback-based file output

74

tinify.fromFile("input.png")

75

.convert({ type: "image/webp" })

76

.toFile("output.webp", (err) => {

77

if (err) {

78

console.error("Failed to save image:", err.message);

79

} else {

80

console.log("Image saved successfully");

81

}

82

});

83

84

// From Source instance

85

const source = tinify.fromFile("large-image.png")

86

.resize({ method: "fit", width: 1200 });

87

88

await source.toFile("resized-image.png");

89

90

// From Result instance

91

const result = source.result();

92

await result.toFile("result-copy.png");

93

```

94

95

### Buffer Output

96

97

Get processed image data as a Uint8Array buffer for in-memory operations.

98

99

```typescript { .api }

100

/**

101

* Get processed image as buffer (Promise version)

102

* @returns Promise resolving to image data as Uint8Array

103

*/

104

toBuffer(): Promise<Uint8Array>;

105

106

/**

107

* Get processed image as buffer (callback version)

108

* @param callback - Callback function receiving error or image data

109

*/

110

toBuffer(callback: (err: Error | null, data?: Uint8Array) => void): void;

111

```

112

113

**Usage Examples:**

114

115

```typescript

116

const tinify = require("tinify");

117

import * as fs from "fs";

118

119

tinify.key = "your-api-key";

120

121

// Promise-based buffer output

122

const imageBuffer = await tinify.fromFile("input.jpg")

123

.resize({ method: "fit", width: 800 })

124

.toBuffer();

125

126

// Use buffer data

127

fs.writeFileSync("manual-output.jpg", imageBuffer);

128

129

// Callback-based buffer output

130

tinify.fromFile("input.png")

131

.convert({ type: "image/webp" })

132

.toBuffer((err, data) => {

133

if (err) {

134

console.error("Failed to get buffer:", err.message);

135

} else {

136

console.log(`Buffer size: ${data!.length} bytes`);

137

// Process buffer data...

138

}

139

});

140

141

// From Result instance

142

const source = tinify.fromFile("image.jpg");

143

const result = source.result();

144

const buffer = await result.toBuffer();

145

```

146

147

### Image Metadata

148

149

Access processed image metadata including dimensions, file size, and format information.

150

151

```typescript { .api }

152

/**

153

* Get image width in pixels

154

*/

155

width(): Promise<number>;

156

width(callback: (err: Error | null, width?: number) => void): void;

157

158

/**

159

* Get image height in pixels

160

*/

161

height(): Promise<number>;

162

height(callback: (err: Error | null, height?: number) => void): void;

163

164

/**

165

* Get image file size in bytes

166

*/

167

size(): Promise<number>;

168

size(callback: (err: Error | null, size?: number) => void): void;

169

170

/**

171

* Get image media type (MIME type)

172

*/

173

mediaType(): Promise<string | void>;

174

mediaType(callback: (err: Error | null, type?: string | void) => void): void;

175

176

/**

177

* Get image content type (alias for mediaType)

178

*/

179

contentType(): Promise<string | void>;

180

contentType(callback: (err: Error | null, type?: string | void) => void): void;

181

182

/**

183

* Get recommended file extension for the image format

184

*/

185

extension(): Promise<string | void>;

186

extension(callback: (err: Error | null, ext?: string | void) => void): void;

187

```

188

189

**Usage Examples:**

190

191

```typescript

192

const tinify = require("tinify");

193

194

tinify.key = "your-api-key";

195

196

const source = tinify.fromFile("image.jpg")

197

.resize({ method: "fit", width: 800 })

198

.convert({ type: ["image/webp", "image/jpg"] });

199

200

const result = source.result();

201

202

// Promise-based metadata access

203

const width = await result.width();

204

const height = await result.height();

205

const size = await result.size();

206

const mediaType = await result.mediaType();

207

const extension = await result.extension();

208

209

console.log({

210

dimensions: `${width}×${height}`,

211

fileSize: `${size} bytes`,

212

format: mediaType,

213

extension: extension

214

});

215

216

// Callback-based metadata access

217

result.width((err, width) => {

218

if (err) {

219

console.error("Failed to get width:", err.message);

220

} else {

221

console.log(`Image width: ${width} pixels`);

222

}

223

});

224

225

// Use extension for dynamic filename

226

const ext = await result.extension();

227

await source.toFile(`processed-image.${ext}`);

228

```

229

230

### Storage Location

231

232

For cloud storage operations, get the storage location URL.

233

234

```typescript { .api }

235

/**

236

* Get storage location URL (for cloud storage results)

237

*/

238

location(): Promise<string>;

239

location(callback: (err: Error | null, location?: string) => void): void;

240

```

241

242

**Usage Example:**

243

244

```typescript

245

const tinify = require("tinify");

246

247

tinify.key = "your-api-key";

248

249

// Store to cloud and get location

250

const resultMeta = tinify.fromFile("image.jpg")

251

.resize({ method: "fit", width: 800 })

252

.store({

253

service: "s3",

254

aws_access_key_id: "ACCESS_KEY",

255

aws_secret_access_key: "SECRET_KEY",

256

region: "us-west-1",

257

path: "my-bucket/processed-images/image.jpg"

258

});

259

260

const location = await resultMeta.location();

261

console.log(`Image stored at: ${location}`);

262

```

263

264

## Result Class Hierarchy

265

266

```typescript { .api }

267

/**

268

* Base class for result metadata

269

*/

270

class ResultMeta {

271

width(): Promise<number>;

272

height(): Promise<number>;

273

location(): Promise<string>;

274

}

275

276

/**

277

* Full result class with image data

278

*/

279

class Result extends ResultMeta {

280

toFile(path: string): Promise<void>;

281

toBuffer(): Promise<Uint8Array>;

282

size(): Promise<number>;

283

mediaType(): Promise<string | void>;

284

contentType(): Promise<string | void>;

285

extension(): Promise<string | void>;

286

}

287

```

288

289

## Error Handling

290

291

Result and output methods can throw the following errors:

292

293

- **ClientError**: Invalid file path or processing errors

294

- **ConnectionError**: Network issues during file operations

295

- **ServerError**: Temporary API service issues

296

- **AccountError**: API key or billing issues

297

298

**Error Handling Example:**

299

300

```typescript

301

const tinify = require("tinify");

302

303

tinify.key = "your-api-key";

304

305

try {

306

const source = tinify.fromFile("image.jpg");

307

const result = source.result();

308

309

const buffer = await result.toBuffer();

310

const metadata = {

311

width: await result.width(),

312

height: await result.height(),

313

size: await result.size(),

314

format: await result.mediaType()

315

};

316

317

console.log("Processing successful:", metadata);

318

} catch (error) {

319

if (error instanceof tinify.ClientError) {

320

console.error("Client error:", error.message);

321

} else if (error instanceof tinify.ConnectionError) {

322

console.error("Network error:", error.message);

323

}

324

}

325

```