What is the easiest way to get book cover ?

295 views
Skip to first unread message

Mateusz Wilk

unread,
Aug 6, 2015, 2:39:53 PM8/6/15
to epubjs
Hello all,

I know how to get a book cover, well, I haven't implement any solution yet, but it shouldn't be that difficult when you have your instance of book open.

But the problem is, I'm uploading a books into a database, and I'm not using epub.js at this point. Then I'm displaying all the books I have in my db in a view. 
The thing is, I need to be able to extract epub cover before using

new ePub('/some/url');

is there any way, EPUBJS.core method, or any other method, which will allow me to extract only cover of a book, without opening it ?

It's very important feature.

Any help greatly appreciated!

Thanks!

Stelios Michalakis

unread,
Jul 2, 2016, 4:52:30 AM7/2/16
to epubjs

Hi Mat,

just found the way to do it after searching for a long time with chrome debugger.
I'm attaching my code below


function init() {
  var Book = new ePub('/some/url');
  Book.on('book:ready', getCover);
  
  function getCover() {
    var coverActualUrl = Book.zip.getUrl(Book.cover)._result;//finding this url is the key
    getDataUri(coverActualUrl, callback);
    
    function callback(img) {
      //cover is here and scaled to 280 x 200
    }
  }
}


function getDataUri(url, callback) {
    var image = new Image();
    image.onload = function () {
        var canvas = document.createElement('canvas');
        canvas.width = 200; // or 'width' if you want a special/scaled size
        canvas.height = 280; // or 'height' if you want a special/scaled size
        canvas.getContext('2d').drawImage(this, 0, 0, 200, 280);
        callback(canvas.toDataURL('image/png'));//get as data URI
    };
    image.src = url;
}



Reply all
Reply to author
Forward
0 new messages