-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
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
[icons] Support converting icons to PNG #44427
Comments
For anyone else trying to send an email, or for anything else that requires an image for that matter, but comes from Material-UI Icons... well... I have this horrible RegExp mess for you: lovell/sharp#4271 (comment) --- It "works", the colours are wrong, the result may be fuzzy (use |
Hey @AlbinoGeek! Do you need to convert the SVG in code (programmatically)? Or would downloading the PNGs work? If downloading the PNGs works, have you checked https://fonts.google.com/icons?icon.size=24&icon.color=%235f6368? |
For my particular use-case, a content management system with a WYSIWYG email template system, I would have the user select MUI icons to place in their email; which I reference as the React components. Therein, I would need a way to programattically exchange the React component for a PNG. Currently we are using import Facebook from '@mui/icons-material/Facebook'
import { renderToString } from 'react-dom/server'
import sharp from 'sharp'
const convertSvgToImage = async (
MuiSvgIcon: typeof Facebook
): Promise<Buffer> => {
const html = renderToString(<MuiSvgIcon />)
const css = RegExp(/MuiSvgIcon-root{(.*)}/).exec(html)?.[1] ?? ''
return await sharp(Buffer.from(html
.replace(/<style data-emotion[^>]*>.*<\/style>/, '')
.replace(/class="[^"]*"/, `style="${css}"`)
)).resize(48, 48).png().toBuffer()
}
// ... later when constructing the `inline` parameter for mailgun ...
const facebookIcon = await convertSvgToImage(Faceboook); |
Thanks for the response, @AlbinoGeek. This is not a use case we support, so I'll add the |
👋 Thanks for using this project! We use GitHub issues exclusively as a bug and feature requests tracker, however, this issue appears to be a support request. For support with Material UI please check out https://mui.com/material-ui/getting-started/support/. Thanks! If you have a question on Stack Overflow, you are welcome to link to it here, it might help others. |
Steps to reproduce
[ Question doesn't need reproduction. ]
Current behavior
I have also tried
ReactDOMServer.renderToString
,ReactDOMServer.renderToStaticMarkup
,ReactDOM.render
and friends to retrieve the SVG string forsharp
to convert, which returns an invalid XM Ldocument, crashingsharp
. Various issues opened over there.Expected behavior
We need PNGs for things like email which do not render SVGs.
Context
For context, we have the $600 paid X license.
Not relevant to this ticket, but 🤷🏻
Your environment
npx @mui/envinfo
Search keywords: icon, png, email, inline, embed, sharp, convert
The text was updated successfully, but these errors were encountered: