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

Add quality to individual formats #35

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ The plugin uses [sharp](https://github.com/lovell/sharp) to resize the image.
| Output format | The output format of your images: Same as source, JPEG, PNG, WebP or AVIF |
| Width | **Required** Width of the image |
| Height | Height of the image |
| Quality | Imagequality (overrides global quality setting for this image format) |
| Fit | How the image should be resized to fit both provided dimensions. [More info](https://sharp.pixelplumbing.com/api-resize#parameters) |
| Position | To use when fit is cover or contain. [More info](https://sharp.pixelplumbing.com/api-resize#parameters) |
| Without enlargement | Do not enlarge if the width or height are already less than the specified dimensions. [More info](https://sharp.pixelplumbing.com/api-resize#parameters) |
Expand Down
24 changes: 22 additions & 2 deletions admin/src/pages/Settings/ImageFormat.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const ImageFormat = (props) => {
))}
</Select>
</GridItem>
<GridItem col={6} s={12}>
<GridItem col={4} s={7}>
<NumberInput
label={formatMessage({
id: getTrad("settings.form.formats.width.label"),
Expand All @@ -147,7 +147,7 @@ const ImageFormat = (props) => {
value={input.width}
/>
</GridItem>
<GridItem col={6} s={12}>
<GridItem col={4} s={7}>
<NumberInput
label={formatMessage({
id: getTrad("settings.form.formats.height.label"),
Expand All @@ -167,6 +167,26 @@ const ImageFormat = (props) => {
value={input.height}
/>
</GridItem>
<GridItem col={4} s={7}>
<NumberInput
label={formatMessage({
id: getTrad("settings.form.formats.quality.label"),
})}
name="quality"
onValueChange={(value) =>
handleFormatsChange(
{
target: {
name: "quality",
value,
},
},
index
)
}
value={input.quality}
/>
</GridItem>
<GridItem col={4} s={7}>
<Select
label={formatMessage({
Expand Down
3 changes: 3 additions & 0 deletions admin/src/pages/Settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ export const SettingsPage = () => {
label={formatMessage({
id: getTrad("settings.form.quality.label"),
})}
hint={formatMessage({
id: getTrad("settings.form.quality.description"),
})}
name="quality"
validations={{
min: 1,
Expand Down
2 changes: 2 additions & 0 deletions admin/src/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
"settings.form.formats.convertToFormat.description": "Choose the output format of your images",
"settings.form.formats.width.label": "Width",
"settings.form.formats.height.label": "Height",
"settings.form.formats.quality.label": "Quality",
"settings.form.formats.fit.label": "Fit",
"settings.form.formats.fit.description": "How the image should be resized to fit both provided dimensions",
"settings.form.formats.position.label": "Position",
"settings.form.formats.position.description": "To use when fit is cover or contain",
"settings.form.formats.withoutEnlargement.label": "Without enlargement",
"settings.form.formats.withoutEnlargement.description": "Do not enlarge if the width or height are already less than the specified dimensions",
"settings.form.quality.label": "Quality",
"settings.form.quality.description": "Can be overridden by each format",
"settings.form.progressive.label": "Progressive",
"settings.header.label": "Responsive image",
"settings.section.global.label": "Global settings",
Expand Down
1 change: 1 addition & 0 deletions server/controllers/validation/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const settingsSchema = yup.object({
convertToFormat: yup.string().transform(emptyStringToNull).nullable(),
width: yup.number().required().min(1),
height: yup.number().min(1).transform(emptyStringToNull).nullable(),
quality: yup.number().min(1).max(100).transform(emptyStringToNull).nullable(),
fit: yup.string(),
position: yup.string(),
withoutEnlargement: yup.boolean(),
Expand Down
3 changes: 3 additions & 0 deletions server/services/image-manipulation.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ const resizeFileTo = async (

let sharpInstance = autoOrientation ? sharp().rotate() : sharp();

// merge global quality with format specific quality
quality = options?.quality ? options.quality : quality;

if (options.convertToFormat) {
sharpInstance = sharpInstance.toFormat(options.convertToFormat);
}
Expand Down
Loading