Ahh.... After a long hours of debugging I have finally found the issue. It was my foolishness because of which I struggled. I wanted to delete this question but thought that it would help new comers like me when the stuck to similar issues.

Problem was here

loader: 'url-loader?limit=10000'

I was setting the limit to file size of 10kb without really knowing what it does. And the image which I was loading was of size 21kb ! This happens when you copy some else webpack config files :) Sign of javascript fatigue !

Answer from WitVault on Stack Overflow
🌐
Htmlcsstoimage
htmlcsstoimage.com
HTML/CSS to Image API - Automate your image generation
Convert HTML to an image (jpg, png, webp). Renders images exactly like Google Chrome. Works with PHP, JavaScript, Ruby, .NET and more.
Pricing
Convert HTML to an image (jpg, png, webp). Renders images exactly like Google Chrome. Works with PHP, JavaScript, Ruby, .NET and more.
Examples
Quick start samples to help you generate images using automation.
🌐
HUGO
discourse.gohugo.io › support
Setting images as background-image in css/sass - support - HUGO
February 13, 2024 - Hi, just looking for some clarification. I am trying to set a background image using sass. I initially placed the images in /assets/images/image.png. I then called the image in my sass using background-image: url("/images/image.png"). It worked. I did it this way as I read somewhere online ...
🌐
GeeksforGeeks
geeksforgeeks.org › css › sass-variable-for-images-path
SASS variable for images path - GeeksforGeeks
June 27, 2020 - body { margin: 0 auto; background: url(/assets/images/gfg.gif); width: 100%; } We can also use multiple variables in a single path to an image.
🌐
GitHub
github.com › sass › sass › issues › 1015
SASS @import and maintaining URLs for images etc · Issue #1015 · sass/sass
October 8, 2013 - Shouldn't URLs be maintained when importing .scss files from other directories? e.g. File structure: css/ main.scss dir/ dir.scss main.scss: @import './dir/dir.scss'; dir.scss: body { background: url('../../arbitrary/bg.jpg'); } Generate...
Author   denniszhao
🌐
HTML/CSS to Image
docs.htmlcsstoimage.com
HTML/CSS to Image API | HTML/CSS to Image
Convert HTML to an image (jpg, png, webp). Renders images exactly like Google Chrome. Works with PHP, JavaScript, Ruby, .NET and more.
Find elsewhere
Top answer
1 of 3
35

depending on how your packs are structured/applied you might be able to use a loop to generate a bunch of generic styles. See the documentation here: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#id35

Do you really need 3 separate components to get your image url? wouldn't: $img and then setting that to /folder/img.ext be far easier?

Also, you don't need the #{} for repeat by the way.

I hope this is what you're after… the question is not very specific in terms of what you need the outcome to actually be.

Cheers, Jannis

Update:

Okay, I see you've updated your question (thanks for that). I believe this could be a little better for general use:

@mixin background($imgpath,$position:0 0,$repeat: no-repeat) {
    background: {
        image: url($imgpath);
        position: $position;
        repeat: $repeat;
    }
}
.testing {
    @include background('/my/img/path.png');
}

This will then output:

.testing {
  background-image: url("/my/img/path.png");
  background-position: 0 0;
  background-repeat: no-repeat;
}

Or you can use the shorthand version:

@mixin backgroundShorthand($imgpath,$position:0 0,$repeat: no-repeat) {
    background: transparent url(#{$imgpath}) $repeat $position;
}
.testing2 {
    @include backgroundShorthand('/my/img/path.png');
}

Which will generate:

.testing2 {
  background: transparent url(/my/img/path.png) no-repeat 0 0;
}

Lastly if you want to specify your base path to your image directory separately you can do the following:

$imagedir:'/static/images/'; // define the base path before the mixin

@mixin backgroundShorthandWithExternalVar($filename,$position:0 0,$repeat: no-repeat) {
    background: transparent url(#{$imagedir}#{$filename}) $repeat $position;
}
.testing3 {
    @include backgroundShorthandWithExternalVar('filename.png');
}

This will then generate:

.testing3 {
  background: transparent url(/static/images/filename.png) no-repeat 0 0;
}

Is this what you needed?

If not feel free to update the question or reply/comment.

2 of 3
4

some other sample:

path to the image:

$path--rel      : "../images";

color

$black: #000000;

creating the mixin:

@mixin background-image($img, $background-position, $background-color) {
    background-image: url('#{$path--rel}/#{$img}');
    background-repeat: no-repeat;
    background-position: $background-position;
    background-color: $background-color ;
} 

using the mixing:

.navbar-inverse {
  @include background-image("header.png", right, $black); 
}

result:

.navbar-inverse {
  background-image: url("../images/header.png");
  background-repeat: no-repeat;
  background-position: right;
  background-color: #000000;
}
🌐
Roots Discourse
discourse.roots.io › sage
Loading image in SCSS - sage - Roots Discourse
March 19, 2019 - Hi, I have problem with image loading. I tried everything from previous topics but nothing work. .hero-image { background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), url("‥/images/hero.jpg"); } also I tried ‥/dist/images/hero.jpg or ‥/resources/assets/images/hero.jpg and ‥/images/hero.jpg I used WAMPP server, localhost, my config.json { “entry”: { “main”: [ “./scripts/main.js”, “./styles/main.scss” ], “customizer”: [ “./scripts/customizer.js” ] }, “publ...
🌐
GitHub
github.com › lexich › css-image
GitHub - lexich/css-image: generator css and scss templates for images
var imagecss = require("css-image"); var files = [{ width: 400, height: 300, file: "t.png" }]; var result = imagecss(files ,{ css: true, scss: true, retina: true, squeeze: 2, root: "root" })
Starred by 4 users
Forked by 5 users
Languages   JavaScript 100.0% | JavaScript 100.0%
🌐
Caffeine Creations
caffeinecreations.ca › home page › sass variable for image path
SASS Variable for Image Path | Caffeine Creations
February 28, 2019 - Copy to clipboard Error: Invalid CSS after \"...path/hero-image\": expected \")\", was \".jpg) no-repeat...\
Price   $
Call   647-799-6330
Address   Toronto, Canada
🌐
Stack Overflow
stackoverflow.com › questions › 50550384 › handle-image-in-scss-with-webpack
Handle Image in SCSS with Webpack - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams · Get early access and see previews of new features. Learn more about Labs ... module: { rules: [ { test: /\.js$/, use: { loader: "babel-loader", options: { presets: ['babel-preset-env'] } } }, { test: /\.scss$/, use: [ MiniCssExtractPlugin.loader, "css-loader", "sass-loader" ] }, { test: /\.(jp(e)?g|png|gif|svg)$/, use: { loader: 'url-loader', options: { limit: 8192, name: 'css/images/[name].[ext]' } } } ]
🌐
RenderForm
renderform.io › convert-html-to-image
HTML/CSS/JS to Image | RenderForm
Convert any HTML with CSS and JavaScript to image with RenderForm API. Easy setup. No coding required.
🌐
Stack Overflow
stackoverflow.com › questions › 75629908 › scss-file-use-of-image-url-wont-appear-in-page-styles
sass - SCSS file use of image / url() won't appear in page styles - Stack Overflow
For this specific issue, the file path to the image may be correct according to the location of your variables.scss, but don't forget that ultimately the page runs off of the converted main.css file, so you must adjust your path to be correct from the main.css location.
Top answer
1 of 4
6

Client Side solution

In Client Side, you can use something like library (that uses HTML 5): http://html2canvas.hertzen.com/ =)

With it, you can use something like:

html2canvas(document.getElementById("element-id"), {
onrendered: function(canvas) {
  var image = new Image();
  image.src = canvas.toDataURL("image/png"); // This should be image/png as browsers (only) support it (to biggest compatibilty)
  // Append image (yes, it is a DOM element!) to the DOM and etc here..
}
});

Server Side solution

To get a server side solution, you can use PhantomJS (that uses Webkit) or SlimerJS (that uses Gecko).

A good library that is a wrapper to these two is CasperJS. Using CasperJS, a code that can be used is:

casper.start(casper.cli.args[0], function() {
    this.captureSelector(casper.cli.args[1], casper.cli.args[2]);
});

casper.run();

Save it as screenshot.js (just an example of name, you can choice a name too) and run it by using something like:

casperjs screenshot.js (URL) (image path) (selector)

From any language.

A (possible better) alternative to Server Side

Other option is use Selenium, but this is only valid if you can run Java in your server (and install browsers manually) (PhantomJS/SlimerJS/CasperJS, however, does not have these requeriments)

Use it only if you need to emulate a browser completely (maybe when using plugins...)

The best of Selenium is that you can use wrappers to connect to it (using Selenium Server), see the documentation to get the list: http://code.google.com/p/selenium/w/list

2 of 4
1

Server Side Solution

I've been using Guzzle + HCTI API to do this. You'll need an HCTI api key, but you can use a free account.

require 'vendor/autoload.php';

$html = "<div class='ping'>Pong </div>";
$css = ".ping { padding: 20px; font-family: 'sans-serif'; }";

$client = new GuzzleHttp\Client();
$res = $client->request('POST', 'https://hcti.io/v1/image', [
  'auth' => ['user_id', 'api_key'],
  'form_params' => ['html' => $html, 'css' => $css]
]);

echo $res->getBody();

Response returns a URL to the image:

{"url":"https://hcti.io/v1/image/5803a3f0-abd3-4f56-9e6c-3823d7466ed6"}

🌐
Ibexa
doc.ibexa.co › projects › connect › en › latest › apps › html-css-to-image
HTML/CSS to Image - Ibexa Connect Documentation
The HTML/CSS to Image modules enable you to convert HTML/CSS to an image in your HTML/CSS to Image account.
🌐
freeCodeCamp
forum.freecodecamp.org › t › setting-background-image-in-react-using-scss-webpack › 159995
Setting background image in React using SCSS / Webpack - The freeCodeCamp Forum
October 8, 2017 - I’m having trouble setting a background image for a react app, using SCSS. My app has the following structure, with most files removed for readability: --node_modules --public ----index.html ----bg-img.jpg --src ----components ----styles ------base --------_base.scss --------_settings.scss ------components ------styles.scss ----app.js I’m trying to reference the bg-img.jpg in the public folder with background-image: url('bg-img') in the _base.scss file.