Stackedup
Stackedup

Reputation: 760

How to set image extensions for insert image dialog of Summernote

I am using Summernote and everything works fine but there is one thing I can't figure out. Below is the settings I am using for SummerNote:

        summerNoteElement.summernote({
        toolbar: [
            ['magic', ['style', 'h1', 'h2', 'h3', 'h4']],
            ['actions', ['undo', 'redo']],
            ['style', ['bold', 'italic', 'underline', 'clear']],
            ['font', ['fontname', 'strikethrough', 'superscript', 'subscript']],
            ['fontsize', ['fontsize']],
            ['color', ['color']],
            ['para', ['ul', 'ol', 'paragraph']],
            ['height', ['height']],
            ['media', ['picture', 'video', 'link', 'table', 'hr']],
            ['insert', ['map', 'localmap']],
            ['uploadcare', ['uploadcare']],
            ['misc', ['help', 'fullscreen']] 
        ],
        height: 300,
        maximumImageFileSize: maxImageFileSize,
    }

Now I attach screenshots here to show what I want to change. So I click on the insert image button in the bar below: enter image description here

Then click Choose Files: enter image description here

Now if you select the file extensions you will see the list of image formats allowed: enter image description here

What I like to do is to limit those extensions to only a few like JPG, BMP, PNG.

Is there any config I am missing or a trick to do this? Thanks.

Upvotes: 2

Views: 3740

Answers (1)

Navi
Navi

Reputation: 179

summernote does not have this option to control the extension, it accepts all kinds of images, but I needed to release the file uploaded in pdf and found the solution check:

in the main file of summernote.js look for the line:

'type = "file" name = "files" accept = "image / *" multiple = "multiple" />' +

and change the accept = "image / *" to the formats you want ex:

' type="file" name="files" accept="image/gif,image/jpeg,image/jpg,image/png,application/pdf,application/vnd.ms-excel,application/msword, application/vnd.ms-powerpoint,text/plain" multiple="multiple" />' +

Hope this helps

For those who need the complete code to send in addition to images also pdf files, doc, txt etc .. by the server via summernote follows below:

Js code

$ (document) .ready (function () {

        (summernote) summernote ({
           toolbar: [
            ['myotherbutton', ['parameters']],
            ['style', ['style']],
            ['font', ['bold', 'italic', 'underline', 'clear'
            ['fontname', ['fontname']],
['fontsize', ['fontsize']],
            ['color', ['color']]
            ['for', ['ul', 'ol', 'paragraph']]
            ['table', ['table']],
            ['insert', ['link', 'picture', 'video', 'hr', 'codeview']]
],
lang: 'pt-BR',
height: 300,
minHeight: null,
maxHeight: null,
            callbacks: {
                onImageUpload: function (files) {
                    uploadFile (files [0]);
                },
onMediaDelete: function (target) {
                alert (target [0] .src)
                deleteFile (target [0] .src);
}
            }
        });

    function uploadFile (file) {
       
   data = new FormData ();
        data.append ("file", file);

        $ .ajax ({
            date: date,
            type: "POST",
            url: "../midia/upload.php", // replace with your url
            cache: false,
            contentType: false,
            processData: false,
            success: function (url) {

alert (url);

function checkURL (url) {
return (url.match (/ \. (jpeg | jpg | gif | png) $ /)! = null);
}

if (checkURL (url)) {

                var linkfile = url.split ("Images /");

alert (linkfile [1]);

$ image = "../midia/Images/"+linkfile[1];

$ ('# publisher'). summernote ("insertImage", $ image);

} else {

var linkfile = url.split ("Images /");

$ ('# publisher'). summernote ('createLink', {
text: linkfile [1],
url: url,
isNewWindow: true
});

}

            }
        });
    }

function deleteFile (src) {

    $ .ajax ({
        date: {src: src},
        type: "POST",
        url: "../midia/delete.php", // replace with your url
        cache: false,
        success: function (resp) {
            console.log (resp);
        }
    });
}


});

send php files to folder / media /

upload.php

  

<? php
      $ allowed = array ('png', 'jpg', 'gif', 'pdf', 'doc', 'docx', 'txt', 'mp3');
        if (isset ($ _ FILES ['file']) && $ _FILES ['file'] ['error'] == 0) {
            $ extension = pathinfo ($ _FILES ['file'] ['name'], PATHINFO_EXTENSION);
            if (! in_array (strtolower ($ extension), $ allowed)) {
                 echo 'AN ERROR OCCURED - INVALID IMAGE';
                exit;
            }
            if (move_uploaded_file ($ _FILES ['file'] ['tmp_name'], '../midia/Images/'.$_FILES['file']['name'])) {
                echo $ _SERVER ['SERVER_NAME']. dirname ($ _ SERVER ['PHP_SELF']). '/ Images /'.$_ FILES [' file '] [' name ']; // echo absolute file_url
                exit;
            }
        }
        echo 'AN ERROR OCCURED';
        exit;
?>

delete.php

<? php

$ url = explode ("Images", $ _ POST ['src']);

        $ file_name = str_replace ($ url [0], '', $ _ POST ['src']); // striping host to get relative path

        if (unlink ($ file_name))
        {
            echo 'file deleted!';
        }
?>

Upvotes: 3

Related Questions