Add teachers photos
This commit is contained in:
2
src/shared/data/teachers-photos/.gitignore
vendored
Normal file
2
src/shared/data/teachers-photos/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
sources/*
|
||||
sources/urls.txt
|
||||
47
src/shared/data/teachers-photos/scripts/crop.mjs
Normal file
47
src/shared/data/teachers-photos/scripts/crop.mjs
Normal file
@@ -0,0 +1,47 @@
|
||||
import fs from 'fs/promises'
|
||||
import path from 'path'
|
||||
import sharp from 'sharp'
|
||||
|
||||
import { dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url)) + '/'
|
||||
|
||||
async function cropImages(imagePaths) {
|
||||
try {
|
||||
await fs.mkdir(__dirname + '../cropped')
|
||||
} catch (err) {
|
||||
if (err.code !== 'EEXIST') {
|
||||
console.error('Failed to create directory:', err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for (const imagePath of imagePaths) {
|
||||
const imageFileName = path.basename(imagePath)
|
||||
const outputFileName = `${__dirname}../cropped/${imageFileName}`
|
||||
|
||||
try {
|
||||
const image = sharp(imagePath)
|
||||
const metadata = await image.metadata()
|
||||
|
||||
const minDimension = Math.min(metadata.width, metadata.height)
|
||||
|
||||
await image
|
||||
.extract({ left: 0, top: 0, width: minDimension, height: minDimension })
|
||||
.resize(96, 96, { fit: 'contain' })
|
||||
.toFile(outputFileName)
|
||||
|
||||
console.log(`Successfully cropped ${imageFileName}`)
|
||||
} catch (err) {
|
||||
console.error(`Failed to crop ${imageFileName}:`, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const files = await fs.readdir(__dirname + '../sources')
|
||||
const imagePaths = files
|
||||
.filter(f => f.toLowerCase().endsWith('.jpg') || f.toLowerCase().endsWith('.png'))
|
||||
.map(f => __dirname + '../sources/' + f)
|
||||
|
||||
await cropImages(imagePaths)
|
||||
28
src/shared/data/teachers-photos/scripts/download.zsh
Executable file
28
src/shared/data/teachers-photos/scripts/download.zsh
Executable file
@@ -0,0 +1,28 @@
|
||||
#!/usr/bin/env zsh
|
||||
|
||||
# Read each line from urls.txt
|
||||
while IFS= read -r url; do
|
||||
# Extract file name from URL
|
||||
filename=$(basename $url)
|
||||
|
||||
# Download the content to a temporary file
|
||||
temp_file=$(mktemp)
|
||||
curl -s -o $temp_file $url
|
||||
|
||||
# Check if download was successful
|
||||
if [[ $? -eq 0 ]]; then
|
||||
# Use 'file --mime-type' to determine the mime type of the file
|
||||
content_type=$(file --mime-type -b $temp_file)
|
||||
|
||||
# Check if the content type starts with 'image'
|
||||
if [[ $content_type == image/* ]]; then
|
||||
echo "$filename downloaded successfully."
|
||||
mv $temp_file $filename
|
||||
else
|
||||
echo "Skipping $filename (Content type is not image)."
|
||||
rm -f $temp_file
|
||||
fi
|
||||
else
|
||||
echo "Failed to download $filename."
|
||||
fi
|
||||
done < urls.txt
|
||||
16
src/shared/data/teachers-photos/scripts/rename-decode.mjs
Normal file
16
src/shared/data/teachers-photos/scripts/rename-decode.mjs
Normal file
@@ -0,0 +1,16 @@
|
||||
import fs from 'fs/promises'
|
||||
|
||||
import { dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url)) + '/'
|
||||
|
||||
const files = await fs.readdir(__dirname + '../sources')
|
||||
const images = files
|
||||
.filter(f => f.toLowerCase().endsWith('.jpg') || f.toLowerCase().endsWith('.png'))
|
||||
.filter(f => f.startsWith('%'))
|
||||
|
||||
for(const image of images) {
|
||||
console.log(image, '->', decodeURIComponent(image))
|
||||
await fs.rename(__dirname + '../sources/' + image, __dirname + '../sources/' + decodeURIComponent(image))
|
||||
}
|
||||
Reference in New Issue
Block a user