Skip to content

Commit 16d9780

Browse files
committed
Updating Locally Working Edge Functions
1 parent 4eb20e0 commit 16d9780

File tree

11 files changed

+180
-139
lines changed

11 files changed

+180
-139
lines changed

netlify.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
function = "resize-image"
2727
path = "/api/resize-image"
2828

29-
[[edge_functions]]
30-
function = "edge-detection"
31-
path = "/api/edge-detection"
32-
3329
[[edge_functions]]
3430
function = "compress-image"
3531
path = "/api/compress-image"
32+
33+
[[edge_functions]]
34+
function = "edge-enhance"
35+
path = "/api/edge-enhance"

netlify/edge-functions/analyze-image.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// file: netlify/edge-functions/analyze-image.ts
21
import { multiParser } from "https://deno.land/x/multiparser@0.114.0/mod.ts";
32
import {
43
ImageMagick,

netlify/edge-functions/convert-image.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// file: netlify/edge-functions/convert-image.ts
21
import { multiParser } from "https://deno.land/x/multiparser@0.114.0/mod.ts";
32
import {
43
ImageMagick,

netlify/edge-functions/crop-image.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
// file: netlify/edge-functions/crop-image.ts
21
import { multiParser } from "https://deno.land/x/multiparser@0.114.0/mod.ts";
32
import {
43
ImageMagick,
54
initialize,
65
MagickImage,
76
MagickGeometry,
8-
} from "https://deno.land/x/imagemagick_deno/mod.ts";
7+
MagickFormat,
8+
} from "https://deno.land/x/imagemagick_deno@0.0.26/mod.ts";
99

1010
await initialize();
1111

1212
export default async (request: Request) => {
1313
if (request.method === "POST") {
1414
try {
1515
const form = await multiParser(request);
16+
1617
const fileData = form.files.file.content;
1718
const x = parseInt(form.fields.x) || 0;
1819
const y = parseInt(form.fields.y) || 0;
@@ -32,17 +33,32 @@ export default async (request: Request) => {
3233
headers: { "Content-Type": "application/json" },
3334
});
3435
}
35-
console.log("about to crop");
36+
3637
const cropped = await ImageMagick.read(
3738
fileData,
3839
async (image: MagickImage) => {
39-
const geometry = new MagickGeometry(width, height, x, y);
40+
// Ensure the cropping area does not exceed the original image dimensions
41+
const cropWidth = Math.min(width, image.width - x);
42+
const cropHeight = Math.min(height, image.height - y);
43+
44+
if (cropWidth <= 0 || cropHeight <= 0) {
45+
throw new Error("Invalid cropping dimensions");
46+
}
47+
48+
const geometry = new MagickGeometry(x, y, cropWidth, cropHeight);
49+
4050
image.crop(geometry);
41-
return await image.write((data: Uint8Array) => data);
51+
52+
const data = await image.write(
53+
MagickFormat.Png,
54+
(data: Uint8Array) => data
55+
);
56+
57+
return data;
4258
}
4359
);
44-
console.log(cropped);
45-
if (!cropped) {
60+
61+
if (!cropped || cropped.length === 0) {
4662
return new Response(JSON.stringify({ error: "Failed cropping" }), {
4763
status: 400,
4864
headers: { "Content-Type": "application/json" },
@@ -52,7 +68,8 @@ export default async (request: Request) => {
5268
return new Response(cropped, {
5369
status: 200,
5470
headers: {
55-
"Content-Type": "image/jpeg",
71+
"Content-Type": "image/png",
72+
"Content-Length": cropped.length.toString(),
5673
},
5774
});
5875
} catch (error) {
@@ -64,6 +81,7 @@ export default async (request: Request) => {
6481
});
6582
}
6683
}
84+
6785
return new Response("Method Not Allowed", { status: 405 });
6886
};
6987

netlify/edge-functions/edge-detection.ts

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { multiParser } from "https://deno.land/x/multiparser@0.114.0/mod.ts";
2+
import {
3+
ImageMagick,
4+
initialize,
5+
MagickImage,
6+
MagickFormat,
7+
EvaluateOperator,
8+
Channels,
9+
} from "https://deno.land/x/imagemagick_deno@0.0.26/mod.ts";
10+
11+
await initialize();
12+
13+
export default async (request: Request) => {
14+
if (request.method === "POST") {
15+
try {
16+
const form = await multiParser(request);
17+
18+
const fileData = form.files.file.content;
19+
if (!fileData || !(fileData instanceof Uint8Array)) {
20+
return new Response(JSON.stringify({ error: "Invalid file data" }), {
21+
status: 400,
22+
headers: { "Content-Type": "application/json" },
23+
});
24+
}
25+
26+
const processedImage = await ImageMagick.read(
27+
fileData,
28+
async (image: MagickImage) => {
29+
// Apply evaluate method to enhance edges
30+
image.evaluate(Channels.All, EvaluateOperator.Pow, 1.5);
31+
32+
const data = await image.write(
33+
MagickFormat.Png,
34+
(data: Uint8Array) => data
35+
);
36+
37+
return data;
38+
}
39+
);
40+
41+
if (!processedImage || processedImage.length === 0) {
42+
return new Response(
43+
JSON.stringify({ error: "Failed to process image" }),
44+
{
45+
status: 400,
46+
headers: { "Content-Type": "application/json" },
47+
}
48+
);
49+
}
50+
51+
return new Response(processedImage, {
52+
status: 200,
53+
headers: {
54+
"Content-Type": "image/png",
55+
"Content-Length": processedImage.length.toString(),
56+
},
57+
});
58+
} catch (error) {
59+
return new Response(JSON.stringify({ error: error.message }), {
60+
status: 400,
61+
headers: {
62+
"Content-Type": "application/json",
63+
},
64+
});
65+
}
66+
}
67+
68+
return new Response("Method Not Allowed", { status: 405 });
69+
};
70+
71+
export const config = { path: "/api/edge-detection" };

netlify/edge-functions/generate-thumbnail.ts

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
// file: netlify/edge-functions/generate-thumbnail.ts
21
import { multiParser } from "https://deno.land/x/multiparser@0.114.0/mod.ts";
32
import {
43
ImageMagick,
54
initialize,
6-
} from "https://deno.land/x/imagemagick_deno/mod.ts";
5+
MagickImage,
6+
MagickFormat,
7+
MagickGeometry,
8+
} from "https://deno.land/x/imagemagick_deno@0.0.26/mod.ts";
79

810
await initialize();
911

1012
export default async (request: Request) => {
1113
if (request.method === "POST") {
1214
try {
1315
const form = await multiParser(request);
16+
1417
const fileData = form.files.file.content;
15-
const width = parseInt(form.fields.width);
16-
const height = parseInt(form.fields.height);
18+
const width = parseInt(form.fields.width) || 100; // Default width
19+
const height = parseInt(form.fields.height) || 100; // Default height
1720

1821
if (
1922
!fileData ||
@@ -27,25 +30,36 @@ export default async (request: Request) => {
2730
});
2831
}
2932

30-
const inputFile = await Deno.makeTempFile();
31-
await Deno.writeFile(inputFile, fileData);
33+
const thumbnail = await ImageMagick.read(
34+
fileData,
35+
async (image: MagickImage) => {
36+
const geometry = new MagickGeometry(width, height);
37+
image.resize(geometry);
38+
39+
const data = await image.write(
40+
MagickFormat.Png,
41+
(data: Uint8Array) => data
42+
);
3243

33-
const outputFile = `${inputFile}_thumbnail.jpg`;
34-
await ImageMagick.convert([
35-
inputFile,
36-
"-resize",
37-
`${width}x${height}`,
38-
outputFile,
39-
]);
44+
return data;
45+
}
46+
);
4047

41-
const thumbnail = await Deno.readFile(outputFile);
42-
await Deno.remove(inputFile);
43-
await Deno.remove(outputFile);
48+
if (!thumbnail || thumbnail.length === 0) {
49+
return new Response(
50+
JSON.stringify({ error: "Failed to generate thumbnail" }),
51+
{
52+
status: 400,
53+
headers: { "Content-Type": "application/json" },
54+
}
55+
);
56+
}
4457

4558
return new Response(thumbnail, {
4659
status: 200,
4760
headers: {
48-
"Content-Type": "image/jpeg",
61+
"Content-Type": "image/png",
62+
"Content-Length": thumbnail.length.toString(),
4963
},
5064
});
5165
} catch (error) {
@@ -57,6 +71,7 @@ export default async (request: Request) => {
5771
});
5872
}
5973
}
74+
6075
return new Response("Method Not Allowed", { status: 405 });
6176
};
6277

netlify/edge-functions/image-dimensions.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// file: netlify/edge-functions/image-dimensions.ts
21
import { multiParser } from "https://deno.land/x/multiparser@0.114.0/mod.ts";
32

43
export default async (request: Request) => {

0 commit comments

Comments
 (0)