Skip to content

Instantly share code, notes, and snippets.

@mikamboo
Last active July 25, 2024 22:33
Show Gist options
  • Save mikamboo/96fcba65822cf088ce410a95dce9f5b5 to your computer and use it in GitHub Desktop.
Save mikamboo/96fcba65822cf088ce410a95dce9f5b5 to your computer and use it in GitHub Desktop.
JS : Display image in browser console

Function to display image in js console

Source : https://stackoverflow.com/a/47177899

console.image = function(url, size = 100) {
  const image = new Image();
  image.src = url;
  image.onload = function() {
    var style = [
      'font-size: 1px;',
      'padding: ' + this.height/100*size + 'px ' + this.width/100*size + 'px;',
      'background: url('+ url +') no-repeat;',
      'background-size: contain;'
     ].join(' ');
     console.log('%c ', style);
  };
};

Use function

const url = 'https://images.unsplash.com/photo-1593642632823-8f785ba67e45?auto=format&fit=crop&w=300&q=80'

console.image(url, 50);

image

@amirehsan
Copy link

It's not working anymore using a URL (at least in Chrome). Use a base64 "data:" image instead and it'll work 😉

Thanks, man. This is working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment