-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Rework 'make generate-images' #12316
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9e1edbf
Rework 'make generate-images'
silverwind f3558c4
use proper centering value for preserveAspectRatio
silverwind e1143c0
fix lint
silverwind 6cf5394
use fabric
silverwind 25d5689
better linting fix
silverwind 920c0a3
fix typo
silverwind d4e82ee
mention detail-remove class in docs
silverwind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #!/usr/bin/env node | ||
| 'use strict'; | ||
|
|
||
| const imageminZopfli = require('imagemin-zopfli'); | ||
| const {fabric} = require('fabric'); | ||
| const {DOMParser, XMLSerializer} = require('xmldom'); | ||
| const {readFile, writeFile} = require('fs').promises; | ||
| const {resolve} = require('path'); | ||
|
|
||
| function exit(err) { | ||
| if (err) console.error(err); | ||
| process.exit(err ? 1 : 0); | ||
| } | ||
|
|
||
| function loadSvg(svg) { | ||
| return new Promise((resolve) => { | ||
| fabric.loadSVGFromString(svg, (objects, options) => { | ||
| resolve({objects, options}); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function generate(svg, outputFile, {size, bg, removeDetail} = {}) { | ||
| const parser = new DOMParser(); | ||
| const serializer = new XMLSerializer(); | ||
| const document = parser.parseFromString(svg); | ||
|
|
||
| if (removeDetail) { | ||
| for (const el of Array.from(document.getElementsByTagName('g') || [])) { | ||
| for (const attribute of Array.from(el.attributes || [])) { | ||
| if (attribute.name === 'class' && attribute.value === 'detail-remove') { | ||
| el.parentNode.removeChild(el); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| svg = serializer.serializeToString(document); | ||
|
|
||
| const {objects, options} = await loadSvg(svg); | ||
| const canvas = new fabric.Canvas(); | ||
| canvas.setDimensions({width: size, height: size}); | ||
| const ctx = canvas.getContext('2d'); | ||
| ctx.scale(options.width ? (size / options.width) : 1, options.height ? (size / options.height) : 1); | ||
|
|
||
| if (bg) { | ||
| canvas.add(new fabric.Rect({ | ||
| left: 0, | ||
| top: 0, | ||
| height: size * (1 / (size / options.height)), | ||
| width: size * (1 / (size / options.width)), | ||
| fill: 'white', | ||
| })); | ||
| } | ||
|
|
||
| canvas.add(fabric.util.groupSVGElements(objects, options)); | ||
| canvas.renderAll(); | ||
|
|
||
| let png = Buffer.from([]); | ||
| for await (const chunk of canvas.createPNGStream()) { | ||
| png = Buffer.concat([png, chunk]); | ||
| } | ||
|
|
||
| png = await imageminZopfli({more: true})(png); | ||
| await writeFile(outputFile, png); | ||
| } | ||
|
|
||
| async function main() { | ||
| const svg = await readFile(resolve(__dirname, '../assets/logo.svg'), 'utf8'); | ||
| await generate(svg, resolve(__dirname, '../public/img/gitea-lg.png'), {size: 880}); | ||
| await generate(svg, resolve(__dirname, '../public/img/gitea-512.png'), {size: 512}); | ||
| await generate(svg, resolve(__dirname, '../public/img/gitea-192.png'), {size: 192}); | ||
| await generate(svg, resolve(__dirname, '../public/img/gitea-sm.png'), {size: 120}); | ||
| await generate(svg, resolve(__dirname, '../public/img/avatar_default.png'), {size: 200}); | ||
| await generate(svg, resolve(__dirname, '../public/img/favicon.png'), {size: 180, removeDetail: true}); | ||
| await generate(svg, resolve(__dirname, '../public/img/apple-touch-icon.png'), {size: 180, bg: true}); | ||
| } | ||
|
|
||
| main().then(exit).catch(exit); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.