npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

image-resolver

v0.6.1

Published

Extract main image for a given URL

Downloads

36

Readme

ImageResolver.js

ImageResolver.js is a library that extracts the main image of a URL while saving resources. Instead of loading all images of a URL, it will try to guess the main image from the URL or the webpage. It's like Readability for images.

Demo : http://mauricesvay.github.com/ImageResolver/

ImageResolver works in browsers and Node.js.

To detect images, ImageResolver comes with built-in plugins:

  • FileExtension: use file extension in urls
  • ImgurPage: extract image from imgur.com urls
  • NineGag: extract image from 9gag.com urls
  • Instagram: extract image from instagram.com urls
  • MimeType: use MIME type to detect images
  • ImgurAlbum: extract image from imgur.com albums
  • Flickr: extract image from Flickr urls (requires API key)
  • Opengraph: use opengraph meta to extract image
  • Webpage: parse HTML to extract image

Of course, you can create your own plugins.

How to install

In Node.js:

npm install image-resolver

In a browser:

<script src="dist/ImageResolver.js" type="text/javascript"></script>

How to use

var resolver = new ImageResolver();
resolver.register(new ImageResolver.FileExtension());
resolver.register(new ImageResolver.MimeType());
resolver.register(new ImageResolver.Opengraph());
resolver.register(new ImageResolver.Webpage());

resolver.resolve( "http://example.com/", function( result ){
    if ( result ) {
        console.log( result.image );
    } else {
        console.log( "No image found" );
    }
});

API

ImageResolver( [options] )

Create a new instance of ImageResolver

ImageResolver.register( plugin )

Register the given plugin for resolving images. You must register at least one plugin. Plugins are executed in the order of their registration.

ImageResolver.resolve( url, callback )

Extract main image from given url. Callback will be called with null when no image is found, or an object when the image is found.

How to write your own plugin

To create a plugin, create an object that has resolve method. The resolve method must have this signature:

function( url, clbk, options, utils ) {
    ...
}
  • url : url to resolve
  • clbk : callback
  • options : ImageResolver options
  • utils : util functions

When your plugin has found an image, you must call clbk with the image as parameter:

clbk( image_url );

If your plugin can not find an image, you must call clbk with null as parameter

clbk( null );

If your plugin needs to make HTTP GET requests, it is recommended to use utils.fetch. This function lets you make GET requests, works in browsers and Node.js, and the result will be cached and shared between plugins.

If you need more control over HTTP requests, you can use utils.request that gives you access to the raw superagent library.

Running tests

ImageResolver comes with a series of tests.

To run those tests:

  • npm install -g jasmine-node
  • npm test

Compiling the browser lib

  • npm install
  • npm install -g gulp
  • gulp to build the browser lib