mirror of
https://github.com/qwertyforce/scenery.git
synced 2025-05-30 19:22:29 +00:00
thumbnail_ops
This commit is contained in:
parent
6c5b585b56
commit
4b05c24097
@ -1,7 +1,7 @@
|
||||
import sharp from 'sharp';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import config from "../../config/config"
|
||||
import thumbnail_ops from "../helpers/thumbnail_ops"
|
||||
const PATH_TO_IMAGES = path.join(config.root_path, 'public', 'images')
|
||||
const PATH_TO_WEBP_IMAGES = path.join(config.root_path,'public','webp_images')
|
||||
const WEBP_IMAGES = fs.readdirSync(PATH_TO_WEBP_IMAGES)
|
||||
@ -11,24 +11,7 @@ async function convert() {
|
||||
for (const image_file_name of IMAGES) {
|
||||
if(!WEBP_IMAGES.includes(`${path.parse(image_file_name).name}.webp`)){
|
||||
console.log(`converting ${image_file_name}`)
|
||||
const metadata = await sharp(`${PATH_TO_IMAGES}/${image_file_name}`).metadata()
|
||||
if(metadata && metadata.height && metadata.width){
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const x:any ={}
|
||||
if(metadata.width>metadata.height){
|
||||
|
||||
x.width=Math.min(metadata.width,750)
|
||||
}else if(metadata.width<metadata.height){
|
||||
x.height=Math.min(metadata.height,750)
|
||||
}else{
|
||||
x.width=Math.min(metadata.width,750)
|
||||
}
|
||||
const data = await sharp(`${PATH_TO_IMAGES}/${image_file_name}`).resize(x)
|
||||
.webp({ quality: 80, reductionEffort: 6 })
|
||||
.toFile(`${PATH_TO_WEBP_IMAGES}/${path.parse(image_file_name).name}.webp`);
|
||||
console.log(data)
|
||||
}
|
||||
|
||||
await thumbnail_ops.generate_thumbnail(`${PATH_TO_IMAGES}/${image_file_name}`,parseInt(path.parse(image_file_name).name))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
24
server/helpers/thumbnail_ops.ts
Normal file
24
server/helpers/thumbnail_ops.ts
Normal file
@ -0,0 +1,24 @@
|
||||
import config from '../../config/config'
|
||||
import path from 'path'
|
||||
import sharp from 'sharp'
|
||||
const PATH_TO_WEBP_IMAGES = path.join(config.root_path,'public','webp_images')
|
||||
|
||||
async function generate_thumbnail(image_src: Buffer | string,image_id:number){ //buffer or path to the image
|
||||
const metadata = await sharp(image_src).metadata()
|
||||
if(metadata && metadata.height && metadata.width){
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const x:any ={}
|
||||
if(metadata.width>metadata.height){
|
||||
x.width=Math.min(metadata.width,750)
|
||||
}else if(metadata.width<metadata.height){
|
||||
x.height=Math.min(metadata.height,750)
|
||||
}else{
|
||||
x.width=Math.min(metadata.width,750)
|
||||
}
|
||||
const data = await sharp(image_src).resize(x)
|
||||
.webp({ quality: 80, reductionEffort: 6 })
|
||||
.toFile(`${PATH_TO_WEBP_IMAGES}/${image_id}.webp`);
|
||||
console.log(data)
|
||||
}
|
||||
}
|
||||
export default {generate_thumbnail}
|
@ -5,11 +5,10 @@ import { Request, Response } from 'express';
|
||||
import axios from 'axios'
|
||||
import fs from 'fs'
|
||||
import path from 'path';
|
||||
import sharp from 'sharp'
|
||||
import image_ops from './../helpers/image_ops'
|
||||
import thumbnail_ops from '../helpers/thumbnail_ops'
|
||||
const imghash: any = require('imghash');
|
||||
const PATH_TO_IMAGES = path.join(process.cwd(), 'public', 'images')
|
||||
const PATH_TO_WEBP = path.join(process.cwd(),'public','webp_images')
|
||||
async function parse_author(tags: any) {
|
||||
for (const tag of tags) {
|
||||
const idx = tag.indexOf("artist:")
|
||||
@ -40,9 +39,7 @@ async function import_from_derpi(req: Request, res: Response) {
|
||||
}
|
||||
const image =await axios.get(derpi_data.representations.full, {responseType: 'arraybuffer'})
|
||||
const new_image_id = (await db_ops.image_ops.get_max_image_id())+1
|
||||
await sharp(image.data)
|
||||
.webp({ quality: 80, reductionEffort: 6 })
|
||||
.toFile(`${PATH_TO_WEBP}/${new_image_id}.webp`);
|
||||
await thumbnail_ops.generate_thumbnail(image.data,new_image_id)
|
||||
fs.writeFile(`${PATH_TO_IMAGES}/${new_image_id}.${derpi_data.format.toLowerCase()}`, image.data, 'binary', function (err) {
|
||||
if (err) {
|
||||
console.log(`There was an error writing the image: derpi_id: ${derpy_import_id} id: ${new_image_id}`)
|
||||
|
Loading…
x
Reference in New Issue
Block a user