How to set the height of Image component in next.js - image

I am trying to decrease the height of Image component in Next.js. But, When I increase the width, the height has no affect. The image height stays the same as width. If width is 100, the image becomes 100x100 but, I need 100x50. How can I achieve that?
Below is my code
<Image
alt="Mountains"
width={80}
height={50}
src={Dog}
style={{ objectFit: 'contain' }}
/>

Related

Automatically size image components in React Native

I want to set the width of an Image component to be 100% of my container's width and the height to be set according to the result width value and image's aspect ratio.
My container has some padding, which means that Dimensions.get('window').width is not useful to me since it does not gives me the value that results from image's width being set to 100%. Other scenarios also can't afford the Dimensions API as a acceptable solution.
Here's an code example:
<View
style={{
flex: 1,
paddingHorizontal: 20,
}}>
<Image
style={{
flex: 1, // Not what I want => just so I can see the image
width: '100%',
// height: 'auto', // The behavior that I want
backgroundColor: 'red',
}}
resizeMode={'contain'}
source={{
uri: 'https://via.placeholder.com/200x400/000000',
}}
/>
</View>
Result:
The red background serves just to visualize that the Image component.
Using onLayout prop I can get the runtime value of the resulting width, and with the aid of the Image API i could calculate the desired height. The problem with that is that React Native will first render a 0 height component and just after that onLayout seems to be resolved, and that makes sense.
So?
Is there a way to get before hand the width value of a image component such as this (declared using relative width)?
Is automatically setting image height even possible in React Native? If so, how?
Notes:
- In the image docs of React Native there is an explanation of why they chose not to size image components automatically by default.
- At least in this case, Parent onLayout is resolved after its children.
In your componentDidMount you could calculate the image metrics, and then use that height and width to calculate the aspectRatio.
componentDidMount = () => {
Image.getSize(YourUri, (width, height) => {
this.setState({width, height});
});
}
In your image you will add the styles like this:
<Image
style={{
width: '100%',
backgroundColor: 'red',
aspectRatio: this.state.height !== null ? this.state.width / this.state.height : 1
}}
resizeMode={'contain'}
source={{
uri: 'https://via.placeholder.com/200x400/000000',
}}
/>
As stated in React native docs:
Aspect ratio controls the size of the undefined dimension of a node.
Aspect ratio is a non-standard property only available in React Native
and not CSS.
-On a node with a set width/height aspect ratio controls the size of the unset dimension
-On a node with a set flex basis aspect ratio controls the size of the node in the cross axis if unset
-On a node with a measure function aspect ratio works as though the measure function measures the flex basis
-On a node with flex grow/shrink aspect ratio controls the size of the node in the cross axis if unset
Aspect ratio takes min/max dimensions into account

React Native Image resizeMode='contain' not working

I can't get resizeMode='contain' to work for images loaded remotely, wondering if there's something I've done wrong.
Render Method:
return (
<View style={styles.internal_button_avatar_container}>
<Image
style={styles.internal_button_avatar_image}
source={{ uri: this.state.imageURL }}
loadingIndicatorSource={require('../assets/icons/PageLink-Loading.png')}
resizeMode={'contain'}
/>
</View>
);
Styles (relevant section):
internal_button_avatar_container: {
marginRight: 10,
width: 40,
height: 40,
},
internal_button_avatar_image: {
width: 40,
height: 40,
},
I've tried resizeMode='contain' and resizeMode='center' but the image always is cropped around the outside of it's frame. Screenshot showing cropped icon.. I've also tried with undefined height and width on the image.
The cropping only appears to happen if the source image is not the same aspect ratio as the frame, a square source will result in perfectly scaled image.
I tried to reproduce your situation, but I haven't problems with that in this snack:
https://snack.expo.io/#gersonmontenegro/resizemode
The original size of the image is bigger than the container, even it has a different aspect radio, and it has loaded according to the internal_button_avatar_image.
Is ok for you?
Turns out I was getting the image from an endpoint in an API that auto-cropped the original image - should've checked the source image before everything else!

Flex mobile resized image layout

I'm trying to display a resized image in a flex mobile application and I couldn't get it displayed correctly.
When set to width="100%" it looks OK. (Image 1)
When I scale it down to let's say 30% by Flex (scaleMode="letterbox"), it is scaled down, but the components above and below the image still keep the distance from the Image control as it were 100% height. (Image 2)
I tried dozens of layout tricks from autolayout=true to dynamically scaled elements, but couldn't find a solution. What am I doing wrong?
(I use Flash Builder 4.7, Flex 4.6 and Air SDK 13.0 on a 64-bit Win 7 Machine.)
Thank you for your answer, really appreciate it! However, this is unfortunately not a solution as it just sets the height the same as width. In this case, the two labels gets closer to the image, but not in the place where they supposed to go.
Meanwhile I could find a way to correctly resize the image along with its bounding box.
The main idea is to put the image in a container, and adjust the image size according to the ratio of the container size divided by the original image size.
Then the image will fully fill the container, and I can then set the desired scaling ratio by adjusting the container's width.
Here is my code that works perfectly, and I really hope that it will help someone else in the future:
<?xml version="1.0" encoding="utf-8"?>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
import spark.components.Image;
private var img:Image = new Image();
private function init():void {
drawImg("assets/image.png");
}
private function drawImg(src:String):void {
img.source = src;
img.scaleMode = "letterbox";
img.smooth = true;
imgHolder.addElement(img);
img.addEventListener(FlexEvent.READY, imgStatus);
}
private function imgStatus(e:FlexEvent):void {
var imgw:Number = e.currentTarget.bitmapData.width;
var imgh:Number = e.currentTarget.bitmapData.height;
var ratio:Number = Number(imgHolder.width/imgw);
img.width = imgw*ratio
img.height = imgh*ratio
}
]]>
</fx:Script>
<s:VGroup width="100%" horizontalAlign="center">
<s:Label text="Foo" />
<s:HGroup id="imgHolder" width="30%" />
<s:Label text="Bar" />
</s:VGroup>
First I always got a null object, so I found that I need to listen for the image to be fully loaded, and now it works perfectly.
Again, the desired width property can be set on the container and not the image.
Here's a screenshot with the results.
The height of the image indeed remains untouched by only setting the image width.
If you draw a rectangle around the image you will see the bounding box with the same size all the time.
You can modify your code like the following to have a dynamic height and have the labels positioned as you wish:
<s:VGroup width="100%"
horizontalAlign="center">
<s:Label verticalAlign="bottom"
text="Foo" />
<s:Image id="img"
source="test.gif"
width="30%"
height="{img.width}"
scaleMode="letterbox" />
<s:Label verticalAlign="top"
text="Bar" />
</s:VGroup>

XUL set toolbarbutton width

how can i set the width for a toolbar button?
I've set it in the XUL and trying with the js but none of theses methods works using the
document.....setAttribute("style","width:50px")
How can i do that?
Thanks.
Having something like this:
<toolbarpalette id="BrowserToolbarPalette">
<toolbarbutton id="toolbarbuttonID"
image='chrome://yourExt/content/images/logo.png'
class="toolbarbutton-1 chromeclass-toolbar-additional"
</toolbarbutton>
</toolbarpalette>
You can set the image size inside your css file:
#toolbarbuttonID .toolbarbutton-icon {
width: 50px
height:16px
}
If you want, you can also add an image of the height and width desired and add it as the button's image. It will stretch the size of the button:

Image disappears on setting width [Titanium]

I created an imageView in Titanium. Whenever I set the width and height of the image to "auto"; the image is not visible. But if I manually set height and width of the image, the image appears.
Why does this happen ?
var image = Titanium.UI.createImageView({
backgroundImage:'test.png',
width: 'auto',
height: 'auto'
})
If you go here on the DOCs you will see that `auto is deprecated:
'auto' represents the default sizing behavior for a given type of view. The use of 'auto' is deprecated, and should be replaced with the SIZE or FILL constants if it is necessary to set the view's behavior explicitly.
Instead try setting the width and height to Titanium.UI.SIZE, Titanium.UI.FILL, a pixel value, or a percentage of the parent view.

Resources