-
Notifications
You must be signed in to change notification settings - Fork 92
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
Resizing images #43
Comments
Hi @denis-sokolov |
In the last days I have put in quite a bit of effort to make responsive-loader work with my Next.js app, but now that it does it works mostly great, although I do miss out on some of the features of next-optimized-images. |
Thanks for the link, I didn't know this loader yet. I think it can be supported and combined with the other parts of next-optimized-images, I'll try it out in the next days! |
Hello @denis-sokolov // next.config.js
|
I got the responsive-loader to work with hacks I don’t understand. There is something with the interaction between // Webpack config:
config.module.rules.push({
test: /\.(jpg)$/,
use: [
{ loader: __dirname + "/lib/Images/image-fixing-loader" },
{
loader: "responsive-loader",
options: {
adapter: require("responsive-loader/sharp"),
// Warning, dark magic here and in image-fixing-loader
name: "static/[name]-[hash].[ext]",
sizes: [210, 420, 700, 1200, 2000],
placeholder: true,
placeholderSize: 50
}
}
]
});
// image-fixing-loader:
module.exports = function loader(content) {
return content.replace(/__webpack_public_path__/g, JSON.stringify("/_next/"));
}; |
Are you able to use responsive-loader with next-optimized-images? I'm looking for a solution that combines image resizing and compression. |
I’m not using them together, but the responsive-loader already does resizing for you! See the |
A short update from my side: I was able to add support for the responsive-loader to next-optimized-images yesterday. I'm now just waiting for responsive-loader to publish the newest version to npm (which doesn't require the |
That's great to hear! As I understand it, responsive-loader resizes images and provides a srcset value, but it does not compress/optimize images, so that's why it'd be ideal to combine it with next-optimized-images. Is that right? |
Version v2.2.0 has just been published which now supports image resizing. After adding the const oneSize = require('./images/my-image.jpg?resize&size=300');
const multipleSizes = require('./images/my-image.jpg?resize&sizes[]=300&sizes[]=600&sizes[]=1000');
export default () => (
<div>
{/* Single image: */}
<img src={oneSize.src} />
{/* Source set with multiple sizes: */}
<img srcSet={multipleSizes.srcSet} src={multipleSizes.src} />
</div>
); I hope that it works as you have expected, otherwise please tell me :) |
So as of now, you can resize some of your images (with the Just a small note about compressing of resized images: responsive-loader provides a quality option but it looks like it only gets applied to jpeg images and not png. There is maybe a way to also optimize png images (and also other formats which responsive-loader doesn't support, like webp) with a custom adapter. But I currently don't have much time (end of the year, also end of the semester so exams start soon), but I'll explore this option in the semester break. Until then, you have the same features as you would have when just using responsive-loader out of the box. |
I’ve tried it out and it works exactly as it should. Small not, but while I have to manually add |
Hi @denis-sokolov, thank you for trying it out! To understand your use-case better, do you use mostly the same sizes (so you define them in the loader options and then only add |
Sorry for misleading. Yeah, I meant a default query for images only. In my use-case I use the same sizes, but lots of pictures. I configure the sizes once and require pictures everywhere:
I would like to be bold and say this is the most popular use-case. At the end of the day, the |
For our use case, we don't use the same sizes for all images. We'd size them based off the resource query. |
So, I think that I found an improvement for both use cases. Specifying the size with resource queriesWhen you want to resize single images with the query param (e.g. Let responsive-loader handle all JPEG and PNG images per defaultIf you want all images to be resized with a global config (as of your use case @denis-sokolov), you can now overwrite the default loader with the I hope that this improves the experience for both use cases. Both changes are available in version |
This does work great and I have now switched to next-optimized-images and deleted the hacks from my code! \o/ Thank you. |
Thanks for putting in all the work. My use-case is that I have a JPEG image and I want to provide the following to a
|
Hi @jonasIv I just saw that there is an issue in responsive-loader for exactly your request: dazuaz/responsive-loader#42 and even a PR which had it implemented: dazuaz/responsive-loader#58 |
I'll close this for now as the main functionality has been implemented. I'll keep an eye open for the webp implementation in responsive-loader and update this package as soon as they added support for webp images. |
I had an issue of getting "responsive-loader/sharp" working in "next-optimized-images" (v2.6.2) in year 2021. Here is what I did in order to make it working.
As result you have HTML generated as below: <img
srcset="
/_next/static/images/1-OpenProject-300-8bd205d8fa6712a27d68d10d2fa96041.png 300w,
/_next/static/images/1-OpenProject-320-149721ce42ee5cc54bb9969e9527f858.png 320w,
/_next/static/images/1-OpenProject-640-43595f0f9dc6d2e17b557d7def96c796.png 640w,
/_next/static/images/1-OpenProject-960-1c9adea9078cf40f468c7a2a5d4041e0.png 960w,
/_next/static/images/1-OpenProject-1200-0f021a08f50c63c7b0f47c0f6eb921a6.png 1200w,
/_next/static/images/1-OpenProject-1800-70b7d4f7b788d3418cf7729e764e9951.png 1800w,
/_next/static/images/1-OpenProject-2400-5bbb6e6fcc73108fff95a32dc9cf346b.png 2400w
"
src="/_next/static/images/1-OpenProject-300-8bd205d8fa6712a27d68d10d2fa96041.png"
/> |
Would you consider resizing images to be a part of the design goals of this module? It would be very convenient to keep original images in the project directory and in the importing code specify
require('./images/my-photo.jpg?resize=1000px')}
. From my point of view, resizing images down is a similar semantically operation as minifying with mozjpeg. What do you think?The text was updated successfully, but these errors were encountered: