I want to display an image in my VuePress markdown file. Normally, I'd go with:
![My Image](./resources/myimg.png)
However, I'd like to create a custom Vue component that will style the images in a specific way. Then, some images would be displayed using the "standard" markdown syntax (like above), and some others using my custom component.
With my custom component, I'd display the images like this:
<MyComponent src="./resources/myimg.png"/>
As you can see, the images are placed alongside my markdowns, in a resources directory. This makes sense for me, because the image is close to the markdown where it gets displayed.
Unfortunately, the image does not get displayed when I use MyComponent. VuePress (webpack?) handles the images during build and places them in some other directory with a different name. The "standard" Markdown image reference works fine, its URL to the image is set up correctly by VuePress. However, MyComponent does not work, because the src parameter is just a string for VuePress and it does not transform it in any way.
I know that one solution would be to place my images in the /vuepress/public folder. However, I would want to keep the same organization as I have now - images alongside documents.
How can I achieve that?
I had the same issue.
I used the answer #papey provides for a Vue question here
Here is one thing he suggests
<template>
<div id="app">
<img :src="require('./assets/logo.png')"/>
</div>
</template>
<script>
export default {
}
</script>
<style lang="css">
</style>
Related
I have been using Svelte for a little while and now I have switched to SvelteKit so I can add multiple pages. I want to add some images to my site but I don't know where to put them. In Svelte I would just put them in public/images but there is no public folder with SvelteKit (I set it up with npm init svelte#next my-app if that matters). Would I put them in static?
Thanks!
I added the images in static/images and referenced them with src="/images/photo.jpg" like #b2m9 said and it works perfectly.
I recommend putting images under src/lib, not static. For example you could make a src/lib/images or src/lib/assets folder and put them there.
The reason is performance:
For files imported from anywhere under src, at compile time Vite adds a hash to the filename. myImage.png might end up as myImage-a89cfcb3.png. The hash is based on the image contents. So if you change the image, it gets a new hash. This enables the server to send a very long cache expiration to the browser, so the browser can cache it forever or until it changes. It's key-based cache expiration, which IMO is the best kind: cached exactly as long as it needs to be. (Whether the server actually sends the right caching headers in the response may depend on which SvelteKit adapter you use and what host you're on.)
By contrast, images under static don't have a hash added to their name. You can use the static directory for things like robots.txt that need to have a specific filename. Since the filename stays unchanged even if its contents change these files by necessity end up having a cache-control value that includes max-age=0, must-revalidate and an etag, which means even if the browser caches the image it still has to make a server round-trip to validate that the cached image is correct. This slows down every image on your site.
Usage:
When putting images under src/lib, you reference them like this:
<script>
import img from '$lib/images/img.png';
</script>
<img src={img} alt="Image" />
I recommend simplifying by adding svelte-preprocess-import-assets to your project, which automates the process of importing images and cleans up your code. You wrote the following and it generates the code above:
<img src="$lib/images/img.png" alt="Image" />
As Sveltekit uses Vitejs, there is a easy solution mentioned in Vitejs official web site (Click Here).
First inside the script tag :
<script>
const imgUrl = new URL('./img.png', import.meta.url).href
</script>
then inside your Image tag just use that variable,
<img src="{imgUrl}" alt="" />
or,
<div class=" h-screen w-full" style="background-image: url('{bgUrl}') ;">
</div>
You can import static images from any relative path.
there is also svelte-image.
"Svelte image is a pre-processor which automates image optimization using sharp.
It parses your img tags, optimizes or inlines them and replaces src accordingly. (External images are not optimized.)
Image component enables lazyloading and serving multiple sizes via srcset.
This package is heavily inspired by gatsby image.
Kudos to #jkdoshi for great video tutorial to Svelte Image."
-https://github.com/matyunya/svelte-image
If you make a Gallery in Batflat CMS, the template tag it creates will generate its only Bootstrap HTML for a gallery. What if I just want to emit IMG tags for the gallery items, instead?
Create a Gallerymod custom module. That way, your customization may likely survive a Batflat Update.
Copy inc/modules/galleries as inc/modules/gallerymod.
Remove the lang folder and Admin.php in your gallerymod folder.
Change the Name and Description inside the gallerymod/Info.php, as well as the comments. I used static strings instead of code. Also in this file, inside the install function and uninstall function, remove code inside those so that it does nothing on install or uninstall.
In your gallerymod/Site.php, look for the $assign[$gallery['slug']] assignment, and on the following line, add:
$assign[$gallery['slug'] . '-alt1'] = $this->draw('gallery-alt1.html', ['gallery' => $tempAssign]);
Also, where you have the namespace line set as namespace Inc\Modules\Galleries;, change it to namespace Inc\Modules\Gallerymod;.
In your gallerymod/view folder, create a gallery-alt1.html file and add these contents:
{loop: $gallery.items}
<img class="photo-{if: $value.title}{$value.title}{/if}" alt="" class="img-responsive" src="{?=url($value.src.lg)?}">
{/loop}
Now activate this inactive module in Batflat's admin system. You'll notice that it has no admin panel -- because it doesn't need one. You already have the Galleries one. Do not deactivate the Galleries module because the Gallerymod module relies on the Galleries module.
Now, from your custom theme template, you can call this by varying how you called the old slug. So, if your old way of calling the gallery was something like {$gallery.home-photos}, then you would merely tack on the "-alt1" on the end and call it like {$gallery.home-photos-alt1}. I like to wrap these in a DIV wrapper with an ID on it so that I can address it with CSS, jQuery, or Javascript.
In the Batflat Admin system, go back and edit your image titles in the gallery. Treat those titles like a slug (lowercase alphanumeric phrase with dashes) because these are used as class names on the IMG tags in gallery-alt1.html, and you may want to address these individually in CSS, jQuery, or Javascript, later on.
Refresh your browser and you may see the source code display something similar to:
<div id="hidden-images" class="hidden">
<img class="photo-man2" alt="" class="img-responsive" src="https://example.com/uploads/galleries/2/15831273220.jpg">
<img class="photo-woman1" alt="" class="img-responsive" src="https://example.com/uploads/galleries/2/15831272980.jpg">
<img class="photo-man1" alt="" class="img-responsive" src="https://example.com/uploads/galleries/2/15831272540.jpg">
</div><!-- hidden-images -->
Just remember that if you update your version of Batflat, that you may need to reapply this customization again -- it depends on what was done in the update to the existing Galleries module.
If you have different tastes as to how you want to format your images, just edit your gallery-alt1.html file. Plus, you can make multiples of these for different situations, such as gallery-alt2.html, gallery-alt3.html, etc. You can even make it emit JSON instead of html so that you can insert it into a Javascript block in your theme.
Another tip for debugging, in case your site won't load or the admin system breaks, is to edit inc/core/defines.php and change the DEV_MODE to false. That way, PHP will show you every error and that might help you in debugging what might be wrong.
I'm using Webpack to compile my Vue production bundle and it loads my images properly because they are defined used <img> tags in my component. I want to load an additional image and show it based on a condition. Since the image isn't set initially in an <img> tag Webpack isn't loading it using file-loader. When I call:
event.currentTarget.children[2].innerHTML = "<img src='../assets/imgs/heart.png' />"
This doesn't work because heart.png was never processed by file-loader, while my previous images were. Any ideas on how to force Webpack to process this image without defining it in HTML my component. My other images (that work) look like this : <img src="data:image/png;base64,iVBORw0KGgo...=="> I understand why my image isn't loading (it's defined in a string), buy any suggestions?
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)
Actually the question is in the subj...
Is it possible to make handlebars template framework, to recognize templates within a div tag and not in script tag?
For example I would like to create template with this markup:
<style>
div.text-x-handlebars {display:none;}
</style>
<div class="text-x-handlebars-template">
<h2>I'm template</h2>
<p>{{welcomeMessage}}</p>
</div>
Yes you can put your templates in <div>s rather than <script>s, for example:
http://jsfiddle.net/ambiguous/RucqP/
However, doing so is fraught with danger. If you put your template inside a <div> then the browser will interpret it as HTML before you've filled it in; so, if your template's HTML isn't valid until after it has been filled in, the browser may attempt to correct it and make a mess of things. Also, if you have id attributes in your templates, then you will end up with duplicate ids (one in the template <div> and a repeat in the filled in template that you put in the DOM) and that will cause all sorts of strange and interesting bugs. The browser will also try to download any images inside the templates in a <div>, this may or may not be a problem (if might even be desirable but probably not desirable if the image uses a template variable in its src attribute).
Basically, you can do it but you shouldn't, you should put your templates in <script id="..." type="text/x-handlebars-template"> elements instead.