0

I was trying to get a flip-book to accept not only JPG files but also (animated) GIF files.
This is a simple question I guess, I'm sure that for some of it you will be very easy to figure out.
The main idea is to get the string of the file to check if it is a JPG or a GIF and then do:

img.attr('src', 'pages/' + page + '.jpg');

or

img.attr ('src', 'pages/' + page + '.gif');

Code (.js) coming from: http://www.turnjs.com/

function loadPage(page, pageElement) {

// Create an image element

var img = $('<img />');

img.mousedown(function(e) {
    e.preventDefault();
});

img.load(function() {

    // Set the size
    $(this).css({width: '100%', height: '100%'});

    // Add the image to the page after loaded

    $(this).appendTo(pageElement);

    // Remove the loader indicator

    pageElement.find('.loader').remove();
});

// Load the page

img.attr('src', 'pages/' +  page + '.jpg');

loadRegions(page, pageElement);

When I replace '.jpg' with '.gif' I'm than able to put GIF file into the flipbook but no JPG anymore.

I tried check the extension of the file with something like:

// Get file extension
function getExtension(filename) {
return filename.split('.').pop().toLowerCase();
}

function openFile(file) { 
switch(getExtension(file)) {
    //if gif
    case 'gif':
    alert('was gif');  
        /* handle */
        break;
    //if jpg
    case 'jpg':
    alert('was gif'); 
        break;
}

But It seems that I can't find a way to get the filename there. It should be placed there: openFile("somestring.gif") or openFile("somestring.jpg")

Maybe something like that:

//img.attr('src', 'pages/' +  page + '.jpg');
img.attr('src', 'pages/' +  page + '.' + extension );
satolas
  • 179
  • 1
  • 1
  • 6
  • Possible duplicate of [Display image and Validation of image extension before uploading file Using Javascript](https://stackoverflow.com/questions/21396279/display-image-and-validation-of-image-extension-before-uploading-file-using-java) – Nisarg Sep 12 '18 at 14:32
  • The idea here is not to upload an image but to check the image's extension while loading from the server. – satolas Sep 12 '18 at 15:19
  • If somebody could help me on that one, would be awesome :-) – satolas Sep 14 '18 at 04:05

0 Answers0