Skip to content
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

Black image issue. #531

Open
fluxx030 opened this issue Jan 30, 2023 · 0 comments
Open

Black image issue. #531

fluxx030 opened this issue Jan 30, 2023 · 0 comments

Comments

@fluxx030
Copy link

fluxx030 commented Jan 30, 2023

please help me to fix this , after crop getting black image .

here is my code :

import { useState } from 'react';
import ReactCrop from 'react-image-crop';
import Button from '@mui/material/Button';
import 'react-image-crop/dist/ReactCrop.css';

function CropImageNow() {
  const [src, setSrc] = useState(null);
  const [crop, setCrop] = useState({ aspect: 16 / 9 });
  const [image, setImage] = useState(null);
  const [output, setOutput] = useState(null);

  const selectImage = (file) => {
    setSrc(URL.createObjectURL(file));
    setImage(file);
  };

  const cropImageNow = () => {
    console.log(image)
    // const img = document.createElement("img");
    // img.src = src
    const img = document.getElementById("xyz");

    const canvas = document.createElement('canvas');
    const scaleX = image.naturalWidth / image.width;
    const scaleY = image.naturalHeight / image.height;
    canvas.width = crop.width;
    canvas.height = crop.height;
    const ctx = canvas.getContext('2d');

    const pixelRatio = window.devicePixelRatio;
    canvas.width = crop.width * pixelRatio;
    canvas.height = crop.height * pixelRatio;
    ctx.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
    ctx.imageSmoothingQuality = 'high';


    ctx.drawImage(
      img,
      crop.x * scaleX,
      crop.y * scaleY,
      crop.width * scaleX,
      crop.height * scaleY,
      0,
      0,
      crop.width,
      crop.height,
    );


    // Converting to base64
    const base64Image = canvas.toDataURL('image/jpeg');
    setOutput(base64Image);
  };

  return (
    <div className="App">
      <center>
        <input
          type="file"
          accept="image/*"
          onChange={(e) => {
            selectImage(e.target.files[0]);
          }}
        />
        <br />
        <br />
        <div>
          {src && (
            <div>
              <ReactCrop src={src} onImageLoaded={setImage}
                crop={crop} onChange={setCrop} >
                <img src={src} alt=""  id='xyz'/>
              </ReactCrop>
              {/* <ReactCrop src={src} onImageLoaded={setImage}
								crop={crop} onChange={setCrop} /> */}
              <br />
              <Button onClick={cropImageNow}>Crop</Button>

              <br />
              <br />
              {/* <img src={src} alt="" /> */}
            </div>
          )}
        </div>
        <div>{output && <img src={output} alt="" />}</div>
      </center>
    </div>
  );
}

export default CropImageNow;
@fluxx030 fluxx030 changed the title Black sreen issue. Black image issue. Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant