Imgbot icon indicating copy to clipboard operation
Imgbot copied to clipboard

Supported media file types / extensions

Open PatMyron opened this issue 6 years ago • 18 comments

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

PatMyron avatar Feb 22 '19 16:02 PatMyron

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?

dabutvin avatar Feb 22 '19 17:02 dabutvin

FFmpeg?

PatMyron avatar Feb 22 '19 18:02 PatMyron

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 avatar Mar 03 '19 15:03 geeknoid

@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

dabutvin avatar Mar 03 '19 22:03 dabutvin

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 " as an external command. Any file it modifies, you include in the PR you post.

Now to run svgo, you would need to install it (npm install svgo -g) and then use the npx tool with it (npx svgo ). If this isn't working for some reason, an alternate solution is to create a docker image that contains svgo and use 'docker run' to execute it.

geeknoid avatar Mar 04 '19 19:03 geeknoid

makes sense @geeknoid 👍

I just opened #341 which would make this a lot more feasible - along with some potential performance improvements

dabutvin avatar Mar 05 '19 00:03 dabutvin

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

dabutvin avatar Apr 05 '19 19:04 dabutvin

just opened #386 to start adding svg support with svgo. There's some more work to do, but it's a start

dabutvin avatar May 13 '19 17:05 dabutvin

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.

stale[bot] avatar Jul 21 '19 14:07 stale[bot]

@dabutvin re-open?

PatMyron avatar Jul 28 '19 17:07 PatMyron

SVG support is in!!!!

dabutvin avatar Sep 27 '19 19:09 dabutvin

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.

dabutvin avatar Sep 28 '19 19:09 dabutvin

Any update of pdf compression? It would be really nice for people that use LaTex.

gleich avatar Jan 17 '20 00:01 gleich

Hi @Matt-Gleich !

I'm totally cool with adding pdf compression. You recommend any libraries or tools that we should look at?

dabutvin avatar Jan 17 '20 03:01 dabutvin

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#.

gleich avatar Jan 17 '20 03:01 gleich

@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... 🤔

karlhorky avatar Mar 24 '23 10:03 karlhorky

Alternative 1: I've also created an issue in the GitHub issues for Calibre Image Actions:

  • https://github.com/calibreapp/image-actions/issues/179

karlhorky avatar Mar 24 '23 11:03 karlhorky

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 }}

karlhorky avatar Mar 24 '23 13:03 karlhorky