Imgbot
Imgbot copied to clipboard
Supported media file types / extensions
These seem like the current supported file extensions:
https://github.com/dabutvin/ImgBot/blob/392f7b20e4117e363b78e2537e4d915cabefcc26/Common/KnownImgPatterns.cs#L5
Would love additional formats like .svg
, .pdf
, and audio+video formats like .mp4
hey @PatMyron Would love to see imgbot expanding out like this.
I had this thought the other day about svgs, but couldn't find a good dotnet optimizer for them and I naively started my own (https://github.com/dabutvin/SvgClean)
I bet we can find existing optimizers for videos + pdfs though! I hadn't considered them before. Do you have any suggestions for optimizers for any of these file types?
FFmpeg?
I recommend integrating with https://github.com/svg/svgo which does an excellent job in crunching down SVGs. It'd be great to have ImgBot take care of those.
@geeknoid that would be really cool. I wonder what the best way to fit that into the rest of the compression workflow would be, given that svgo is all JavaScript
svgo can run on a specific file, on a specific directory, or on a specific directory recursively.
I don't know how ImgBot is put together, but if I were to hack this together, I'd just kick off "svgo -r -f
Now to run svgo, you would need to install it (npm install svgo -g) and then use the npx tool with it (npx svgo
makes sense @geeknoid 👍
I just opened #341 which would make this a lot more feasible - along with some potential performance improvements
It would be great to get svgo runnable as an executable, alternatively we would have to install node on the container - might not be so bad
just opened #386 to start adding svg support with svgo. There's some more work to do, but it's a start
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
@dabutvin re-open?
SVG support is in!!!!
see #488 for an example of an issue for adding a new compression library. We can use the same template for adding whole new file extensions.
Any update of pdf compression? It would be really nice for people that use LaTex.
Hi @Matt-Gleich !
I'm totally cool with adding pdf compression. You recommend any libraries or tools that we should look at?
A quick DuckDuckGo search brought up some useful tutorials on how to compress PDFs in C# https://duckduckgo.com/?q=how+to+compress+pdfs+with+c%23&atb=v181-1&ia=web
As far as anything that I have first-hand experience with, no I have done no work with C#.
@dabutvin is this project still being actively developed in 2023? I would be interested in the PDF compression/optimization support - is this already an accepted feature and just needs someone to take time to implement PDF compression in C#? Would guess that there would be libraries for this too... 🤔
Alternative 1: I've also created an issue in the GitHub issues for Calibre Image Actions:
- https://github.com/calibreapp/image-actions/issues/179
Alternative 2
A separate GitHub Actions workflow to do this using ghostscript
(generate an access token for this and add it as a GitHub repo secret called PDF_COMPRESSION_GITHUB_TOKEN
):
name: Compress PDFs
on:
pull_request:
# Run only when PDF files are added or changed
paths:
- '**.pdf'
jobs:
build:
# Only run on Pull Requests within the same repository, and not from forks.
if: github.event.pull_request.head.repo.full_name == github.repository
name: Compress PDFs
runs-on: ubuntu-latest
steps:
- name: Run apt update
run: sudo apt update
- name: Install Ghostscript for compression
run: sudo apt install -y ghostscript
- name: Checkout Repo
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: Compress PDFs
run: |
find . -type f -iname '*.pdf' -not -path "./node_modules/*" | while read file; do
echo "Compressing $file"
# Compress the file for screen using Ghostscript
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile="./temp.pdf" "$file"
mv ./temp.pdf "$file"
done
- name: Commit files
run: |
git config user.email [email protected]
git config user.name github-actions
# Discard modified compressed files that are not more
# than 10KB smaller than the previous version
git ls-files -z --modified '*.pdf' | while IFS= read -r -d '' file; do
old_size=$(git cat-file -s "HEAD:$file")
new_size=$(wc -c < "$file" | tr -d ' ')
if [[ $(($old_size - $new_size)) -lt 10000 ]]; then
echo "Discarding changes to $file, compressed file size $new_size is not >10KB smaller than old size $old_size"
git checkout -- "$file"
continue
fi
echo "Compressed $file from $old_size to $new_size bytes"
done
git add **/*.pdf
if [ -z "$(git status --porcelain)" ]; then
exit 0
fi
git commit -m "Compress PDFs"
git push origin HEAD:${{ github.head_ref }}
env:
GITHUB_TOKEN: ${{ secrets.PDF_COMPRESSION_GITHUB_TOKEN }}