As a back-end for vue.js I use laravel (port 8000)
In my db I have the user and the name of it's profile photo (this.user.photo).
So, I want to show this photo.
<img :src="require(`http://localhost:8000/images/${this.user.photo}`)" alt="Profile Photo">
When I go to http://localhost:8000/images/1.png I actually see the image, but the Vue says:
Module not found: Error: Can't resolve 'http://localhost:8000'
PS: console.log(this.user.photo) outputs 1.png
UPD: I've seen many solutions, like this or this, but they do not work
Looks like your images are already hosted on the server and you are not bundling them during build. In this case, you need not use require and directly refer to your images in the template as
<img :src="'http://localhost:8000/images/' + user.photo" alt="Profile Photo">
Remember that require is a method provided by webpack to resolve your dependancy URLs during build such that you need not worry about the absolute URLs. require helps us refer to URLs relative to the modules which are resolved by webpack during build.
In short, use require when you have static assets inside your Vue project module
You can call the image directly with path if the image is not in public path
<img :src=`../../images/${user.photo}` alt="Profile Photo">
This will build image in the path where the compiling the JS and the image public path will automatically added.
If you're trying to add image from public path, you can initialise the origin path in the data section and call the image like follows
data:()=>{
return {
path: document.location.origin
}
}
<img :src=`${path}/images/${user.photo}` alt="Profile Photo">
Note: you don't need to use this in the template section.
You should use require and point to the image.
Make sure that the path is relative to the module that uses it.
Write a function to return the image URL
var app = new Vue({
el: '#app',
methods: {
getPicture() {
return 'https://vuejs.org/images/logo.png'
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<img :src="getPicture()" alt="Profile Photo">
</div>
See the image, it misses out on some CSS -
If report includes external or internal CSS, you try to convert them into inline css, then the mail will display with style, we did this by a nodejs package: inline-css
If report includes external image, you can try to replace the image src with data url like: <img src="data:image/png;base64,xxxxxxxxxxxx, you can search maybe there is some tool can did that for you.
Image data url for reference.
Another things need to consider, after convert image to data url and css to inline css, the report file size will increase and exceed your mailbox attachment file size limitation.
Including the image data in the URI never worked well for me when embedding the report in an email. Why not just reference the image URLs off the maven site instead. Like this....
img[src*="warning_sml.gif"]{content:url(http://maven.apache.org/images/icon_warning_sml.gif) !important;}
img[src*="error_sml.gif"]{content:url(http://maven.apache.org/images/icon_error_sml.gif) !important;}
img[src*="success_sml.gif"]{content:url(http://maven.apache.org/images/icon_success_sml.gif) !important;}
img[src*="maven-feather.png"]{content:url(http://maven.apache.org/images/logos/maven-feather.png) !important;}
I have installed React using create-react-app. It installed fine, but I am trying to load an image in one of my components (Header.js, file path: src/components/common/Header.js) but it's not loading. Here is my code:
import React from 'react';
export default () => {
var logo1 = (
<img
//src="https://s3.amazonaws.com/codecademy-content/courses/React/react_photo-goose.jpg"
src={'/src/images/logo.png'}
alt="Canvas Logo"
/>
);
return (
<div id="header-wrap">
<div className="container clearfix">
<div id="primary-menu-trigger">
<i className="icon-reorder"></i>
</div>
<div id="logo">
<a href="/" className="standard-logo" data-dark-logo='/images/logo-dark.png'>{logo1}</a>
<a href="/" className="retina-logo" data-dark-logo='/images/logo-dark#2x.png'>
<img src='/var/www/html/react-demo/src/images/logo#2x.png' alt="Canvas Logo" />
</a>
</div>
</div>
</div>
);
}
If I write the image path as src={require('./src/images/logo.png')} in my logo1 variable, it gives the error:
Failed to compile.
Error in ./src/Components/common/Header.js
Module not found: ./src/images/logo.png in /var/www/html/wistful/src/Components/common
Please help me solve this. Let me know what I am doing wrong here.
If you have questions about creating React App I encourage you to read its User Guide.
It answers this and many other questions you may have.
Specifically, to include a local image you have two options:
Use imports:
// Assuming logo.png is in the same folder as JS file
import logo from './logo.png';
// ...later
<img src={logo} alt="logo" />
This approach is great because all assets are handled by the build system and will get filenames with hashes in the production build. You’ll also get an error if the file is moved or deleted.
The downside is it can get cumbersome if you have hundreds of images because you can’t have arbitrary import paths.
Use the public folder:
// Assuming logo.png is in public/ folder of your project
<img src={process.env.PUBLIC_URL + '/logo.png'} alt="logo" />
This approach is generally not recommended, but it is great if you have hundreds of images and importing them one by one is too much hassle. The downside is that you have to think about cache busting and watch out for moved or deleted files yourself.
If you want load image with a local relative URL as you are doing. React project has a default public folder. You should put your images folder inside. It will work.
In React or any Javascript modules that internally use Webpack, if the src attribute value of img is given as a path in string format as given below
e.g. <img src={'/src/images/logo.png'} /> or <img src='/src/images/logo.png' />
then during build, the final HTML page built contains src='/src/images/logo.png'. This path is not read during build time, but is read during rendering in browser. At the rendering time, if the logo.png is not found in the /src/images directory, then the image would not render. If you open the console in browser, you can see the 404 error for the image. I believe you meant to use ./src directory instead of /src directory. In that case, the development directory ./src is not available to the browser. When the page is loaded in browser only the files in the 'public' directory are available to the browser. So, the relative path ./src is assumed to be public/src directory and if the logo.png is not found in public/src/images/ directory, it would not render the image.
So, the solution for this problem is either to put your image in the public directory and reference the relative path from public directory or use import or require keywords in React or any Javascript module to inform the Webpack to read this path during build phase and include the image in the final build output. The details of both these methods has been elaborated by Dan Abramov in his answer, please refer to it or use the link: https://create-react-app.dev/docs/adding-images-fonts-and-files/
There are lot of good answers here and more expert opinions than myself. But I will just share my experience and what worked for me.
I was irritated by the fact that there is so much go around just to have a simple inclusion of images. Hence here is what I did-
Create a seperate component (file) myimages.jsx
import image1 from "../img/someimage.png";
import image2 from "../img/otherimage.png";
const ImageData=[image1,image2,image3]
export default ImageData;
I then just imported this ImageData component in the file (component) I as using the images. This way I turned a cpmponent into a 'folder' to get all my images.
Like I said, not an expert but this resolved my frustration with lack of importing images quickly in React.
You have diferent ways to achieve this, here is an example:
import myimage from './...' // wherever is it.
in your img tag just put this into src:
<img src={myimage}...>
You can also check official docs here: https://facebook.github.io/react-native/docs/image.html
In order to load local images to your React.js application, you need to add require parameter in media sections like or Image tags, as below:
image={require('./../uploads/temp.jpg')}
In React.js latest version v17.0.1,
we can not require the local image we have to import it.
like we use to do before = require('../../src/Assets/images/fruits.png');
Now we have to import it like =
import fruits from '../../src/Assets/images/fruits.png';
Before React V17.0.1 we can use require(../) and it is working fine.
Instead of use img src="", try to create a div and set background-image as the image you want.
Right now it's working for me.
example:
App.js
import React, { Component } from 'react';
import './App.css';
class App extends Component {
render() {
return (
<div>
<div className="myImage"> </div>
</div>
);
}
}
export default App;
App.css
.myImage {
width: 60px;
height: 60px;
background-image: url("./icons/add-circle.png");
background-repeat: no-repeat;
background-size: 100%;
}
Best approach is to import image in js file and use it. Adding images in public folder have some downside:
Files inside public folder not get minified or post-processed,
You can't use hashed name (need to set in webpack config) for images , if you do then you have to change names again and again,
Can't find files at runtime (compilation), result in 404 error at client side.
First, you need to create a folder in src directory then put images you want.
Create a folder structure like
src->images->linechart.png
then import these images in JSX file
import linechart from './../../images/linechart.png';
then you need use in images src like below.
<img src={linechart} alt="piechart" height="400px" width="400px"></img>
We don't need base64, just give your image path and dimensions as shown below.
import Logo from './Logo.png' //local path
var doc = new jsPDF("p", "mm", "a4");
var img = new Image();
img.src = Logo;
doc.addImage(img, 'png', 10, 78, 12, 15)
I'm learning Glade.
I'd like to define some style classes into a separate CSS file, and "include" this CSS file such that it can be loaded with my application. Currently this is what I'm doing with Python3
builder = Gtk.Builder ()
builder.add_from_file ("ui.glade")
cssProvider = Gtk.CssProvider()
cssProvider.load_from_path('css.css')
screen = Gdk.Screen.get_default()
styleContext = Gtk.StyleContext()
styleContext.add_provider_for_screen(screen, cssProvider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
window = builder.get_object("window1")
window.show_all()
Gtk.main()
I would like to know if it's possible to remove all this Python code that loads the CSS, and just add a line within my ui.glade file, something like <style> css file name here </style>, such that it will be loaded automatically.
I have a javascript script that changes an image's src when clicked. When I had the same application in pure php without Codeigniter I simply had the images' src be something like "images/image_name" where images was the images folder, but now I'm using Codeigniter I don't know how to do it. I can't use php inside the JS script obviously so can't use the base_url() or site_url() functions or anything, so I suppose only the absolute path would work. And my images are in an images folder that's inside an assets folder which is at the root (i.e. at the same level as the Application, System and User_guide folders). So what should the images' src be? Thanks.
You can use php code inside script.
<script>
var imgSrc="<?php echo base_url('assets/images/image_name.jpg'); ?>";
</script>
Or you can also declare a global variable in header file to store base path.
<script>
basePath='http://www.yourdomain.com/';
</script>
and it can be used as
var imgSrc=basePath+"assets/images/image_name.jpg";
in addition to piyush's answer, I need to mention something that will help to use site_url() & base_url() properly.
site_url("controller/method/args") == base_url() + $config['index_page'] + /controller/method/args. You should use this to access pages.(Reference)
base_url("public/img/logo.png") == base_url() + /public/img/logo.png. You should use this to access resources such as images, stylesheets etc.(Reference)
Main difference between both functions is this part $config['index_page']. If you have already removed the $config['index_page'] from the config.php and you are using .htaccess to rewrite the URLs, then both function will result the same. But it is a best practice to use the correct function for the correct purposes.
Hope this helps.