Skip to content

Commit 407d68e

Browse files
authored
Merge pull request #485 from michaelchadwick/add-some-qual-qual
Add support for `options.quality`
2 parents 449befc + a574908 commit 407d68e

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

README.md

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,22 @@ I hate having to create a bunch of identical images for use as icons and favicon
99

1010
It is built using EmberJS and takes advantage of the awesome [Sharp](https://github.com/lovell/sharp) library to do the heavy lifting.
1111

12-
1312
## Compatibility
1413

1514
* Ember.js v4.8 or above
1615
* Ember CLI v4.8 or above
1716
* Node.js v18 or above
1817

19-
2018
## Installation
2119

2220
```bash
2321
ember install ember-cli-image-transformer
2422
```
2523

26-
2724
## Usage
2825

29-
Create an `ember-cli-image-transformer` section in your `ember-cli-build.js` file with
30-
an `images` array. Each element in the array represents a different set of images to
26+
Create an `ember-cli-image-transformer` section in your `ember-cli-build.js` file with
27+
an `images` array. Each element in the array represents a different set of images to
3128
be generated.
3229

3330
```js
@@ -55,6 +52,21 @@ module.exports = function(defaults) {
5552
5653
All generated images will be placed into the `public/assets` path of your application.
5754
55+
### Usage with custom quality
56+
57+
The Sharp library uses default quality values depending on the output format. If the file type [supports it](https://sharp.pixelplumbing.com/api-output/#toformat), you may pass an optional `quality: [number]` in your image object array:
58+
59+
```js
60+
images: [
61+
{
62+
inputFilename: 'public/trapezoid.svg',
63+
outputFileName: 'icon-trapezoid',
64+
convertTo: 'png',
65+
sizes: [32, 48, 192],
66+
quality: 50,
67+
}
68+
]
69+
```
5870
5971
### Usage in a template
6072
@@ -73,12 +85,12 @@ All generated images will be placed into the `public/assets` path of your applic
7385
|`sizes`| :heavy_check_mark: | none | `[92, 150]` | An array of image sizes to produce |
7486
|`destination`| | `assets/icons` | `images/wherever/you/want` | The destination directory for the output images relative to `/public` |
7587
|`background`| | none | `{r: 255, g: 255, b: 255, alpha: 0}` | Add a background color to the image. |
88+
|`quality`| | varies by format | `25` | The quality (out of 100) of the image processing. |
7689
7790
## Contributing
7891
7992
See the [Contributing](CONTRIBUTING.md) guide for details.
8093
81-
8294
## License
8395
8496
This project is licensed under the [MIT License](LICENSE.md).

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports = {
3939
include: [pathData.base],
4040
});
4141
let options = {
42+
quality: obj.quality,
4243
sizes: obj.sizes,
4344
inputFilename: pathData.base,
4445
outputFileName: obj.outputFileName,

lib/generate-icons.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ class GenerateIcons extends CachingWriter {
3737
image.flatten({ background: this.options.background });
3838
}
3939

40-
return image.toFormat(this.options.convertTo).toFile(outputPath);
40+
if (this.options.quality) {
41+
return image
42+
.toFormat(this.options.convertTo, { quality: this.options.quality })
43+
.toFile(outputPath);
44+
} else {
45+
return image.toFormat(this.options.convertTo).toFile(outputPath);
46+
}
4147
}
4248
/**
4349
* There is an issue in the way sharp processes SVGs on OSX

0 commit comments

Comments
 (0)