Croppie: Image File Type

Created on 18 Jan 2016  路  2Comments  路  Source: Foliotek/Croppie

any way you can add options to restrict uploads to only images? or maybe include this code via demos...

Most helpful comment

try this!!!...

$( document ).ready(function() {
var $uploadCrop;

function readFile(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();          
        reader.onload = function (e) {
            result = e.target.result;
            arrTarget = result.split(';');
            tipo = arrTarget[0];
            if (tipo == 'data:image/jpeg' || tipo == 'data:image/png') {
                $uploadCrop.croppie('bind', {
                    url: e.target.result
                });
                $('.upload-demo').addClass('ready');
            } else {
                alert('Accept only .jpg o .png image types');
            }
        }           
        reader.readAsDataURL(input.files[0]);
    }
}

$uploadCrop = $('#upload-demo').croppie({
    viewport: {
        width: 200,
        height: 200,
        type: 'square'
    },
    boundary: {
        width: 300,
        height: 300
    }
});

$('#upload').on('change', function () { readFile(this); });
$('.upload-result').on('click', function (ev) {
    $uploadCrop.croppie('result', {
        type: 'canvas',
        size: 'viewport',
        quality: 0.8
    }).then(function (resp) {
        $('#imagebase64').val(resp);
        $('#form').submit();
    });
});

});

...

Send

All 2 comments

I believe this is what you're looking for: http://www.w3schools.com/tags/att_input_accept.asp

I'll throw that tag on the file input. Not a lot of time was spent in the upload demo, it's definitely not implemented with the best practices. But I suppose there's no harm in improving it.

try this!!!...

$( document ).ready(function() {
var $uploadCrop;

function readFile(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();          
        reader.onload = function (e) {
            result = e.target.result;
            arrTarget = result.split(';');
            tipo = arrTarget[0];
            if (tipo == 'data:image/jpeg' || tipo == 'data:image/png') {
                $uploadCrop.croppie('bind', {
                    url: e.target.result
                });
                $('.upload-demo').addClass('ready');
            } else {
                alert('Accept only .jpg o .png image types');
            }
        }           
        reader.readAsDataURL(input.files[0]);
    }
}

$uploadCrop = $('#upload-demo').croppie({
    viewport: {
        width: 200,
        height: 200,
        type: 'square'
    },
    boundary: {
        width: 300,
        height: 300
    }
});

$('#upload').on('change', function () { readFile(this); });
$('.upload-result').on('click', function (ev) {
    $uploadCrop.croppie('result', {
        type: 'canvas',
        size: 'viewport',
        quality: 0.8
    }).then(function (resp) {
        $('#imagebase64').val(resp);
        $('#form').submit();
    });
});

});

...

Send
Was this page helpful?
0 / 5 - 0 ratings

Related issues

TheAustinG picture TheAustinG  路  4Comments

powerbuoy picture powerbuoy  路  5Comments

nueverest picture nueverest  路  8Comments

iKonrad picture iKonrad  路  8Comments

Lenndev picture Lenndev  路  4Comments