在 summernote 上插入图像后调整图像大小

Resizing image after insertimage on summernote

我目前正在使用 summernote,并且有一个 php 文件上传器,效果很好。但是,插入的图像有时可能非常大,1920x1080 甚至更大。

Summernote 带有一个很棒的 "scale" 图像功能,我很乐意以编程方式触发它。 所以当我上传并插入我的图片时,它会自动缩小到给定的百分比。

我知道我可以在服务器端调整图片的大小,但我要在图片上做一个灯箱,所以我想要它们的全尺寸,但按比例缩小。

有没有人做过类似的事情?

我遇到了同样的问题并通过为此创建一个插件解决了它。

在将图像放入 summernote 编辑器之前,它会自动调整大小、翻转和旋转图像。

为 Summernote 下载 Resized Data Image 插件 here

另外需要Exif.js读取图片的EXIF数据

同时将此 CSS 代码添加到您的页面,使 input[type=file] 按钮成为图标按钮:

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;
}

最后将插件 resizedDataImage 添加到您的 summernote 工具栏。

页面的最终设置可能如下所示:

<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>

我使用来自的解决方案解决了这个问题 (Summernote v0.8.1 图片上传) Summernote image upload

并使用 https://github.com/verot/class.upload.php 库制作 verify/resize 个图像

不要使用 Image for summernote v0.8.1 中的 php 文件,而是使用这个:

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();
      }
}
}

然后在您的 javascript 中将成功函数更改为:

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');
            }
        },

从我上面发布的链接中获取其余代码。