5

I am currently using summernote and have a php fileuploader for it that works great. However, the images inserted can sometimes be really large, 1920x1080 and larger.

Summernote comes with a great "scale" function for images, and i would love to programmatically trigger it. So when i upload and insert my image it would automatically be scaled down to a given percentage.

I know i can resize the image serverside, but im gonna do a lightbox on the images so i want them in their full size, but scaled down.

Has anyone done something similar?

Havihavi
  • 652
  • 1
  • 9
  • 26
  • 1
    I know this is old, but for further reference, I suggested a solution to automatically resize upon insertion (no user intervention). See [issue#2344](https://github.com/summernote/summernote/issues/2344) – Juliomac Jun 02 '17 at 11:50

2 Answers2

2

I faced the same problem and solved it by creating a plugin for that.

It automatically resizes, flips and rotates images before putting them into the summernote editor.

Download the Resized Data Image Plugin for Summernote here.

In addition you need Exif.js to read the EXIF data of images.

Also add this CSS Code to your page to make the input[type=file] button as an icon button:

button[data-name=resizedDataImage] {
    position: relative;
    overflow: hidden;
}

button[data-name=resizedDataImage] input {
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    opacity: 0;
    font-size: 200px;
    max-width: 100%;
    -ms-filter: 'alpha(opacity=0)';
    direction: ltr;
    cursor: pointer;
}

Finally add the plugin resizedDataImage to your summernote toolbar.

The final setup of your page could look like this:

<link rel="stylesheet" type="text/css" href="design/css/summernote.min.css">
<script type="text/javascript" src="js/exif.js"></script>
<script type="text/javascript" src="js/summernote.min.js"></script>
<script type="text/javascript" src="js/summernote-ext-resized-data-image.js"></script>  
<style type="text/css">
    button[data-name=resizedDataImage]  {
        position: relative;
        overflow: hidden;
    }

    button[data-name=resizedDataImage] input {
        position: absolute;
        top: 0;
        right: 0;
        margin: 0;
        opacity: 0;
        font-size: 200px;
        max-width: 100%;
        -ms-filter: 'alpha(opacity=0)';
        direction: ltr;
        cursor: pointer;
    }
</style>

<script type="text/javascript">
    $(function () {
        $('.summernote').summernote({
            height: 250,
            toolbar: [
                ['insert', ['resizedDataImage', 'link']]
            ]
        });
    });
</script>
raphael
  • 342
  • 2
  • 13
  • Hey, great stuff! This does what i need and is tweakable :D – Havihavi Aug 30 '15 at 22:41
  • I just updated the code again, now all transformations (flipping and rotating) work properly: [Resized Data Image Plugin for Summernote](https://github.com/raphaelbeckmann/summernote/blob/patch-1/plugin/summernote-ext-resized-data-image.js) – raphael Aug 31 '15 at 12:01
  • Any chance of this being updated to take in account the latest changes? https://github.com/summernote/summernote/issues/1361 – dave Mar 15 '16 at 12:40
  • 2
    I got `summernote-ext-resized-data-image.self-41628b1……:18 Uncaught TypeError: Cannot read property 'getTemplate' of undefined`. It seems `$.summernote.renderer` is undefined. – JNN May 29 '16 at 03:45
0

I fixed this issue using solutions from (Image Upload for Summernote v0.8.1) Summernote image upload

And using https://github.com/verot/class.upload.php library to verify/resize images

Instead of using the php file in Image for summernote v0.8.1 use this:

require_once("pathtoclassfile/src/class.upload.php");
if(!empty($_FILES['image'])){
    $handle = new upload($_FILES['image']);

if ($handle->uploaded) {
    $handle->allowed = array('image/*');
    $handle->mime_check = true; //this is set to true by default
    $handle->no_script = true;  //this is set to true by default
    $handle->forbidden = array('application/*','audio/*','multipart/*','text/*','video/*');
    $name = md5(uniqid()); //generate a new random name for your file
    $handle->file_new_name_body   = $name; 
    $handle->file_auto_rename = true;
    $handle->image_resize         = true;
    $handle->image_x              = 350;  //resize the image to what you want
    $handle->image_ratio_y        = true;
    $handle->image_convert = 'jpg';   //I converted to jpg so that all my extensions are jpg
    $handle->jpeg_quality = 50;   //you can adjust the image quality to resize further
    $handle->process('uploads'); //set the upload path
  if ($handle->processed) {

    $imagename = "$name.jpg"; //attach the extenstion to the file name
    $baseurl = $_SERVER['HTTP_HOST']; //your server host or use "example.com/";
    echo $baseurl.'uploads/summernote/'.$imagename;

    $handle->clean();
      }
}
}

Then in your javascript change success function to:

success: function(url) {
            if(url !== ''){
            var image = $('<img>').attr('src', 'http://' + url);
            $('#summernote').summernote("insertNode", image[0]);
            }else{
                alert('File is not an image!\nOnly jpg, png, jpeg and gif are allowed');
            }
        },

Get the rest of the codes from the links I posted above.

Vee Joe
  • 31
  • 3
  • Nice explanition, this `$handle->file_dst_name;` handles file name, you can do like this `$baseurl = $_SERVER['HTTP_HOST'];echo $baseurl.'uploads/summernote/'.$handle->file_dst_name;` –  May 08 '22 at 12:30