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

@call0fcode
Copy link

call0fcode commented Feb 10, 2024

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

@ivorydevrimoalt
Copy link

VM97:3 Uncaught TypeError: console.image is not a function
at :3:9 you liar

@agustinomana
Copy link

Funciona perfectamente con imagen base64, para agregar texto después de la imagen recomiendo que se use la función setTimeout()

@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