screen_rotation
Copied to Clipboard
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.development.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.26.0/babel.min.js"></script> </head> <body> <div id="app"></div> <script type="text/babel"> const { useState, useEffect } = React; function App() { const [imageUrl, setImageUrl] = useState( 'https://www.sarkarinaukriexams.com/images/bio/1695897671-bio.png' ); const [base64Image, setBase64Image] = useState(''); useEffect(() => { if (imageUrl) { fetchAndConvertImage(); } }, [imageUrl]); const fetchAndConvertImage = async () => { try { const base64data = await fetchAndConvert(imageUrl); setBase64Image(base64data); } catch (error) { console.error('Error fetching and converting image:', error); } }; const fetchAndConvert = async (url) => { const data = await fetch(url); const blob = await data.blob(); return new Promise((resolve) => { const reader = new FileReader(); reader.readAsDataURL(blob); reader.onloadend = () => { const base64data = reader.result; resolve(base64data); }; }); }; return ( <div className='container'> <h3>React Js Convert Image from URl to Base64 </h3> <input type="text" className='input-field' // added className to the input element placeholder="Enter image URL" value={imageUrl} onChange={(e) => setImageUrl(e.target.value)} /> <br /> <p className='base64-text'>Base64 : {base64Image}</p> {/* added className to the paragraph element */} {base64Image && <img className='converted-image' src={base64Image} alt="Converted" />} {/* added className to the image element */} </div> ); } ReactDOM.render(<App />, document.getElementById('app')); </script> <style> /* Style the container */ .container { max-width: 400px; margin: 0 auto; padding: 20px; text-align: center; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.2); } /* Style the input field */ .input-field { width: 100%; padding: 10px; margin-bottom: 10px; border: 1px solid #ccc; border-radius: 5px; font-size: 16px; box-sizing: border-box; } /* Style the paragraph displaying Base64 */ .base64-text { font-size: 18px; font-weight: bold; margin-top: 20px; overflow-x: scroll; } /* Style the converted image */ .converted-image { max-width: 100%; margin-top: 10px; border-radius: 5px; box-shadow: 0 0 5px rgba(0, 0, 0, 0.2); } </style> </body> </html>