1
0
mirror of https://git.sr.ht/~cadence/bibliogram synced 2025-12-14 18:45:06 +00:00

Replace GraphicsMagick with sharp

That was easier than I expected.

Closes #18.
This commit is contained in:
Cadence Fish
2020-01-30 01:14:00 +13:00
parent 06b54e48b2
commit 95cc416e08
4 changed files with 332 additions and 67 deletions

View File

@@ -1,7 +1,7 @@
const constants = require("../../lib/constants")
const {request} = require("../../lib/utils/request")
const {proxy} = require("pinski/plugins")
const gm = require("gm")
const sharp = require("sharp")
module.exports = [
{route: "/imageproxy", methods: ["GET"], code: async (input) => {
@@ -23,36 +23,24 @@ module.exports = [
const width = +params.get("width")
if (typeof width === "number" && !isNaN(width) && width > 0) {
/*
This uses graphicsmagick to force crop the image to a square.
Some thumbnails aren't square and will be stretched on the page without this.
This uses sharp to force crop the image to a square.
"entropy" seems to currently work better than "attention" on the thumbnail of this shortcode: B55yH20gSl0
Some thumbnails aren't square and would otherwise be stretched on the page without this.
If I cropped the images client side, it would have to be done with CSS background-image, which means no <img srcset>.
*/
return request(url).then(res => {
const image = gm(res.body).gravity("Center").crop(width, width, 0, 0).repage("+")
const converter = sharp().resize(width, width, {position: "entropy"})
return {
statusCode: 200,
contentType: "image/jpeg",
headers: {
"Cache-Control": constants.caching.image_cache_control
},
stream: image.stream("jpg")
stream: res.body.pipe(converter)
}
/*
Alternative buffer-based method for sending file:
return new Promise(resolve => {
image.toBuffer((err, buffer) => {
if (err) console.error(err)
resolve({
statusCode: 200,
contentType: "image/jpeg",
content: buffer
})
})
})
*/
})
} else {
// No specific size was requested, so just stream proxy the file directly.
return proxy(url, {
"Cache-Control": constants.caching.image_cache_control
})