Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Programmatic API example #19

Open
leolux opened this issue Jul 8, 2020 · 1 comment
Open

Programmatic API example #19

leolux opened this issue Jul 8, 2020 · 1 comment

Comments

@leolux
Copy link

leolux commented Jul 8, 2020

Is there a way to use the API programmatically within a standard Java main() method?

@gulhe
Copy link

gulhe commented Oct 28, 2021

##TLDR;

Yup you may, it takes some trial and errors and looking into the implementation with your favorite intelliJ to know what to do.
But it's possible.

##LONG VERSION;

I wondered the same and after some fiddling I obtained something like this :

public static void main(String[] args) {
    var sourceFolder = new File(/*filepath*/);

    var files = Objects.requireNonNull(sourceFolder.listFiles(File::isFile));
    Arrays.sort(files, Comparator.comparing(File::getName));
    optimizeAndStore(files);
    String[] fileNames = Arrays.stream(files)
            .map(File::getAbsolutePath)
            .toArray(String[]::new);
    new PngtasticOptimizer(/*target path*/, fileNames, "", false, 0, "zopfli", 1, "debug");
}

values passed to new PngtasticOptimizer(...) are the ones I used, check the source to know what to use.

I even ended up copying most of PngtasticOptimizer's constructor code to get finer access to where my files would end and to better suit my specific needs.

That's not ideal, specially considering that current version (1.6) implementation of zopfli leaves artifacts cf #7

But it does a pretty decent job at compressing pngs rather fast (when you run it with "" instead of zopfli)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants