简体   繁体   English

react - 获取要处理的图像的宽度/高度

[英]react - get width/height of image to process

I was wondering how to get the height and width of an image if I'm rendering it react style so I can process it to flip it to portrait mode if the width is larger than the height.我想知道如何获得图像的高度和宽度,如果我正在渲染它的反应样式,那么如果宽度大于高度,我可以处理它以将其翻转为纵向模式。

Example: https://codesandbox.io/s/k9kwv0kp93示例: https : //codesandbox.io/s/k9kwv0kp93

Example code:示例代码:

index.js索引.js

import React from 'react';
import { render } from 'react-dom';
import Hello from './Hello';

const styles = {
  fontFamily: 'sans-serif',
  textAlign: 'center',
};

const App = () => (
  <div style={styles}>
    <Hello name="CodeSandbox" />
    <h2>Start editing to see some magic happen {'\u2728'}</h2>
  </div>
);

render(<App />, document.getElementById('root'));

Hello.js你好.js

import React, {Component} from 'react';

export default class Hello extends Component
{
  constructor()
  {
    super();
    this.state = {img: null}

    this.get_image = this.get_image.bind(this);
  }

  get_image() 
  {
    let img = <img src="http://via.placeholder.com/350x150" alt="" />;
    //manipulate here maybe
    this.setState({
      img
    })
  }

  render()
  {
    return (
      <div>
        <button onClick={this.get_image}>Click me</button>
        test
        {this.state.img}
      </div>
    )
  }
}

I encountered the same issue, I was able to get the img height and width using ref and onLoad() .我遇到了同样的问题,我能够使用refonLoad()获得img高度和宽度。


Class component version (with 'old' ref usage):类组件版本(使用“旧”引用):


render() {
   return (
     <img
        src='https://via.placeholder.com/150'
        ref={el => this.imgEl = el}
        onLoad={() => console.log(this.imgEl.naturalHeight)} // print 150
     /> 
  )
}

Class component version (with 'new' ref):类组件版本(带有“新”引用):

import * as React from "react";
import ReactDOM from "react-dom";

class App extends React.Component {
  imgEl = React.createRef();

  render() {
    return (
      <img
        src="https://via.placeholder.com/150"
        ref={this.imgEl}
        onLoad={() => console.log(this.imgEl.current.naturalHeight)} // print 150
      />
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Functional component version:功能组件版本:

import React from "react";
import ReactDOM from "react-dom";

function App() {
  const imgElement = React.useRef(null);

  return (
    <img
      src="https://via.placeholder.com/150"
      ref={imgElement}
      onLoad={() => console.log(imgElement.current.naturalHeight)} // print 150
    />
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Also you should use img.naturalHeight你也应该使用img.naturalHeight

React supports a special attribute that you can attach to any component. React 支持可以附加到任何组件的特殊属性。 The ref attribute takes a callback function, and the callback will be executed immediately after the component is mounted or unmounted. ref 属性带有一个回调函数,回调会在组件挂载或卸载后立即执行。

When the ref attribute is used on an HTML element, the ref callback receives the underlying DOM element as its argument当 ref 属性用于 HTML 元素时,ref 回调接收底层 DOM 元素作为其参数

Taken from: https://facebook.github.io/react/docs/refs-and-the-dom.html摘自: https : //facebook.github.io/react/docs/refs-and-the-dom.html

 class MyComponent extends React.Component { constructor(props) { super(props); } handleSize(image) { console.log(image.offsetWidth, image.offsetHeight) } render() { return React.createElement("img", { src: "http://cdn.collider.com/wp-content/uploads/2016/07/narcos-season-2-image-5.jpg", ref: image => { this.handleSize(image); }, onLoad=(image)=>this.handleSize(image); }); } } ReactDOM.render(React.createElement(MyComponent, null), document.getElementById('root'))
 img { width: 200px; height: 133px; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-with-addons.min.js"> </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM