Dynamic URL for pictures in SRC folder - reactjs

I have some storage with pictures in src folder:
const pictures: Array<CarouselPicture> = [
{
index: '1',
url: '../../data/carousel/carousel1.png',
description: ''
}
export default pictures;
Urls are corect for this file, I can reach all my pictures without any problems. Also I have a component where this storage is used:
import pictures from './CarouselData'
export default class ImageContainer extends Component {
render() {
return (
<div className='carousel_image-container'>
{pictures.map((pic) => {
return (
<div key={pic.index}>
<img src={pic.url}></img>
</div>
)})}
</div>
)
}
}
For this component URL's are not correct and I can't reach my pictrures. How can I resolve this problem? I want to write URL's once in my storage and then use the storage in several different components in different folders in my project without changing URL's of my pictures. In other words I want to get pathes dynamically.
I understand that {pic.url} isn't correct, but I dont know how to programm this correctly.
PS: Accordingly some questions here, it would be better to store pictures in src folder, not in public, so I guess 'process.env.PUBLIC_URL' isn't solution (but who knows).

Due to webpack configurations url: '../../data/carousel/carousel1.png', or inline <img src="../../data/carousel/carousel1.png"/> often work for external links such as url: 'https://external/image/link.png'
In your case, you should try require('../../data/carousel/carousel1.png') or import carousel1 from '../../data/carousel/carousel1.png'

Your hunch with using process.env.PUBLIC_URL is correct when using the public/ folder. Assuming your images are in public/data/:
const pictures: Array<CarouselPicture> = [
{
index: '1',
url: `${process.env.PUBLIC_URL}/data/carousel/carousel1.png`,
description: ''
}
]
And as you said you can also do it differently and move your image to your src/ folder. Either one is fine.
These can be located anywhere in src/ and you import them using the normal import rules:
import carousel1 from './images/carousel1.png'
import carousel2 from './images/carousel2.png'
const pictures: Array<CarouselPicture> = [
{
index: '1',
url: carousel1,
description: ''
},
{
index: '1',
url: carousel2,
description: ''
}
]

Make a file which will export images as variable
like this.
for example-
imageExporter.js
export const imageA = {id:1, url:"./your correct relative path for image", description: 'some description'};
and in your component import these image as variable and use them as image source
import {imageA} from './ imageExporter.js path here'
and then
<img src={imageA.url} />

Related

How to display images from object?

How to display image from object?
I was trying to copy relative path, but it's not working for me. Output on browser:
./images/avatars/image-maxblagun.png
data.json
"image": {
"png": "./images/avatars/image-amyrobson.png",
comment.tsx
import React from 'react';
import data from '../../data.json';
const Comment = () => {
const mapData = Object.values(data.comments).map((value) => (
<div key={value.id}>
<div>
<img src={value.user.image.png} alt={value.user.image.png} />
{value.content}
</div>
</div>
));
return <>{mapData}</>;
};
export default Comment;
Link to repository: https://github.com/xflameyoke/interactive-comment-section-app
To add to jsecksn's answer, usually you would import images and the like at the top of your file like this. This ensures that when your project is built, webpack will copy the necessary resources etc.
Since your requirement does not allow for such an import, I believe that just copying the resources to the public folder and changing the url's from relative to absolute is the easiest approach.
In that way the images are out of scope for data.json while rendering.
You must move your images folder images/avatars/ to the root of the /public folder in your project.
Meanwhile in data.json, modify the locations for each image, as if it (data.json) were inside the /public folder i.e. "png": "/images/avatars/image-juliusomo.png" and so forth.

import jpg image in Reactjs with NextJS some images import return long string

I am importing images in different scales and show them in my react app
some images are import correctly,
but some get really long and "strange" string name instead of their regular file name
for example
import generator from '../images/generators/generator-rental.jpg';
import generatorw205 from '../images/generators/generator-rental#0,4x.jpg';
import generatorw384 from '../images/generators/generator-rental#0,75x.jpg';
when I put 3 of them together in srcSet the smallest image get the long string.
the generatorw205 and generatorw384 are imported correctly and shown with this src property:
"/_next/static/images/generator-rental-9f9671c1ae14a9abd201cce092b95c0d.jpg 512w"
and
/_next/static/images/generator-rental#0,75x-0ff42525965adeb23850ef8dacd34c50.jpg 384w,
but the first image get this string:
data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/7gAOQWRvYmUAZMAAAAAB/9sAhAAIBgYHBgUIBwcHCQkICgwUDQwLCwwZEhMPFB0aHx4dGhwcICQuJyAiLCMcHCg3KSwwMTQ0NB8nOT04MjwuMzQyAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wgARCACZAM0DASIAAhEBAxEB/8QAHAABAAEFAQEAAAAAAAAAAAAAAAYBAwQFBwII/9oACAEBAAAAAO/gAAAAjW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XAgQ57OscoEYr0YXcOvccHVl8xAYOit+RuaU7UuejCH+UDhZYz5cxzyhgEAnLOQxUzO8G2GP7PRQcrFMtGVhEGcFdssQoN/MzOJXY/pENVte7CV4Y7lpYOXDqP30+Oj/AFVzMqxGHQZBTLsU9h22E5iTm281CL9NI+JtbzMwjMbAv2MbH2AkACYtgyJ/QeNNnhuGhoQ7hxlLrA5AXyH+H//Z 205w,
And this is my next.config.js file:
const withImages = require('next-images')
const withPlugins = require("next-compose-plugins");
const optimizedImages = require("next-optimized-images");
const withBundleAnalyzer = require('#next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true'
})
module.exports = withPlugins(
[
[optimizedImages, {
}],
[withBundleAnalyzer, {
}]
]
);
Update
For next-optimized-images. The plugin has similar config - https://github.com/cyrilwanner/next-optimized-images#inline
module.exports = withPlugins(
[
[optimizedImages, {
inlineImageLimit:4096
}],
[withBundleAnalyzer, {
}]
]
);
some images are import correctly, but some get really long and "strange" string name instead of their regular file name
The strange string name is the base64 representation that can be supplied to data url's by employing this technique you can avoid additional http downloads from the server.

How to import a sound file into react typescript component?

I have a small mp3 file located in project_root/src/audio/alarm.mp3.
I try to import like so:
import alarm from "./audio/alarm.mp3";
But when I try to import it into App.tsx, I get this compiler error:
Cannot find module './audio/alarm.mp3'.
But I can import images just fine.
Place the audio in the assets and write a custom typing file called audio.d.ts with the following code :-
declare module '*.mp3';
and place it in the root of your project under any name, ex: ./custom_typings/audio.d.ts.
Here, I have just done it for an mp3 file.
After this go to your tsconfig.json and under "typeRoots" add the custom_typings folder.
"typeRoots": [
"node_modules/#types",
"custom_typings/"
]
Once this is done you can just import the audio file from anywhere and use it normally as you would.
import {audioFile} from '../../../../assets/audio/audioFile.mp3';
TrackPlayer.add({
id: '1',
url: audioFile,
title: 'Example title',
artist: 'Example Artist',
artwork: 'https://picsum.photos/100',
});
Found a solution to the issue. Instead of using ES6 import use ES5 require() for audio files. It works and the typescript compiler doesn't complain.
const alarm = require("./audio/alarm.mp3");
You can add typing to the mp3 files, if you are using typescript. To do this, create a types directory at the root directory with a defined file asset.d.ts and include in your configuration file:
types/asset.d.ts
declare module "*.mp3" {
const value: any;
export default value;
}
Then, add this to tsconfig.json:
tsconfig.json
{
"compilerOptions": {
//...
},
"include": [
"src/*",
"types/*"
]
}
Place it in the assets folder in order to access it.
The following is on how to use it with images, however it works the same with audio files:
How to load image (and other assets) in Angular an project?

visual studio react image path cannot reach

So i have a visual studio project created with react.js.
I am trying to link to an image dynamically, but failing. This is the code I am trying:
At the top im setting a variable for the first part of the path:
this.LogoPath = '../images/'
And then im dynamically grabbing the name of the image from an api call.
this.setState({ imageNamePath: this.state.location.imageName })
And then im concatinating them:
{`${this.LogoPath}${this.state.location.imageName}`}
In the console, im getting:
img src='../images/the-images-name-here.png'
So it seems to be working, but it is not. I get no errors, and I have broken images. My best guess is that react is changing the images to something like:
image-name-here.6a131799.png
Surely someone has run across this before, but my google search pulled up little help.
So how do i get the images to show?
Webpack is a smart tool. One of it's strength is the trash code/files elimination from the bundle.
That means that if a file is not imported using import myFile from '../myPath/myFile.jpg'; or require('../myPath/myFile.jpg');` it won't be a part of the final bundle.
In your case you're not importing the file. You're creating a string variable instead which means nothing to webpack.
There are different options that could work in your case:
Option 1: Pre-import all images and map them in an object:
import React, {Component} from 'react';
import image1 from '../assets/image1.png';
import image2 from '../assets/image2.png';
import image3 from '../assets/image3.png';
const imageTypes = {
image1,
image2,
image3,
}
export default class MyComponent extends Component {
constructor(props) {
super(props);
this.state = {
imageType: 'image1'
}
}
render() {
return (
<img src={imageTypes[this.state.imageType]} />
)
}
}
Option 2: Not recommended - Use require to dynamically import files (webpack configurations might be needed in order to include all possible required images in the production bundle):
class MyComponent {
constructor(props) {
super(props);
this.state = {
image: 'file1.png'
}
}
render() {
return (
<img src={require(`./assets/${this.state.image}`)} />
)
}
}
Option 3: Send the image blob (in base64) from the API.
I suggest you to use the Option 1 or Option 3, based on your requirements, such as: how often will be images be changed/added/removed. It's not normal to import files dynamically from ReactJS bundle and you might end up having a non-existing image in your project requested by the data coming from an external source.
For Option 2 you also might have some problems configuring the webpack in order to include in the bundle all the images that you'll probably need to render, even though they are not imported (hardcoded) somewhere in the JS files. Keep in mind that the React Application in production ends up being a collection of static files. You'll need to treat them as follows.

React: How to reference an image url in require

I have a data file api that has bunch of images url stored locally
const url =[
{ title:img1,
img_src="./img/img1.png"
},
{ title:img2,
img_src="./img/img2.png"
},
{ title:img3,
img_src="./img/img3.png"
}
]
And using react/redux I pass the url state as props to my react components.Next I want to display them in my components by using require
<img src=require(?)/>
What's the appropriate syntax here? I've used es6 template string ${this.props.urls.img_src} but it throws an error that it couldn't resolve the path. I've tried to require("./img/img1.png") just to test to rule out broken path and it worked. But still wouldnt work if you reference it using a prop.
Solution
After researching, and thanks to Rei Dien for the input, I now can use variables in require by using context require
<img src={require("./img/"+this.props.img_src)}/>
Since you passed the url in the props, you can do this :
this.props.url.map(n => {
return (
<img src={n.img_src}/>
)
})
this will diplay all the images.
since require works in static mode during build process on node only so follow following steps.
1) take all image urls and store them in a javascript file
so
//imageURLs.js
import image1 from "path_to_image_file_1";
import image2 from "path_to_image_file_2";
import image3 from "path_to_image_file_3";/**do like this for all images stored locally, image1, image2,.. are the image identifiers(title in your case) you would get in api call**/
export const urls = {image1, image2, image3};
//image usage file
read api for identifier and
import imageURLs from './imageURLs.js';
<img src={imageURLs[image_id_from_api]}/>

Resources