question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Adding image sizes to image

See original GitHub issue

When I add an image attribute to the page I noticed that inside of the component the image has attributes to it.

When I call my images into the asset manager I do it like the following:

    type: 'image',
    src: 'http://placehold.it/350x250/459ba8/fff/image2.jpg',
    height: 350,
    width: 250

However, the height and width aren’t available when the image gets added to the canvas. I’ve tried to get the sizes by doing:

component.views[0].el.height

and adding it to the object, but every time I try I get 0 because the image isn’t available in the DOM yet, so it doesn’t know what size it is.

Is there a way to pass these heights and widths to the image, I need this for some lazy load functions I’m doing. I’m currently doing this on an update

editor.on('component:update', component => {
    const item = component.attributes.attributes;
			
    // item['width'] = component.view.el.width;
    // item['height'] = component.view.el.height;

    console.log(editor.AssetManager.getAll());
    console.log('Comp:', component.views[0].el.height);
    //console.log(image.naturalWidth);
			
});

Should i be adding to the attributes earlier than this?

Thanks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
artfcommented, Apr 20, 2020

@awaredigital probably the best solution would be to follow the suggestion to listen up to src changes, then find the right asset and update attributes accordingly

model: {
	init() {
		this.on('change:src', this.onSrcUpdate);
	},
	onSrcUpdate() {
		const src = this.get('src');
		const assets = editor.AssetManager.getAll();
		const asset = assets.filter(asset => asset.get('src') == src)[0];
		if (asset) {
			const width = asset.get('width');
			width && this.addAttributes({ width });
			// ...
		}
	}
}
0reactions
andy-awarecommented, Apr 9, 2020

Thank you for that, it makes sense. But, I can’t see how to get the attributes of the image from the asset manager. When I try to access it, it’s always data from the “image” are they available in the model?

And this:

editor.on('component:update', component => {
        const computed = getComputedStyle(component.view.el)
        console.log(computed.width)
        //....

Always returns 0 as a value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image sizes - Help Center - Typeform
Images uploaded to the Picture Choice question will be resized to fit depending on the size option you select when adding the question....
Read more >
How to Create Additional Image Sizes in WordPress
Step by step guide showing how to easily add additional image sizes in WordPress themes and post content by utilizing the WordPress built-in ......
Read more >
Image rating settings | QuestionPro Help Document
The image sizes include - auto-size to window, small, medium, large, and custom. You can also set an image size (width and height)...
Read more >
Change your picture size - Microsoft Support
Change the size of your pictures in Microsoft Forms by making them larger, smaller, or zooming in to see just one part of...
Read more >
Image Sizes in Typeform - YouTube
Typeform lets you add images, icons, and GIFs to questions. Whether as an answer option, or as part of the design, beautify your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found