【问题标题】:Summernote image upload from URL从 URL 上传 Summernote 图片
【发布时间】:2017-07-16 18:39:45
【问题描述】:

我在我的网站上使用 Summernote html 文本编辑器。当用户将图像 URL 放到图像 URL 区域然后按插入图像按钮时,我想将图像下载到我的服务器。

目前summernote只获取图片的src属性来源。我想将图像存储在我自己的 Amazon S3 Bucket 或 VPS 上。

关于summernote图片上传的文档有很多,但都是从电脑上传而不是从URL上传。

如何覆盖此功能?


图像对话框

【问题讨论】:

标签: javascript php ajax image-uploading summernote


【解决方案1】:

因此,由于您无法在客户端脚本中读取跨域图像的 dataUrl,因此决定从图像 URL 区域获取 url 并将其发送到您的后端。在那里你可以使用一个简单的 php 脚本来下载图像。

该示例包括客户端和后端代码。两者都经过测试。您只需将这两个脚本放到您的 Web 服务器的一个目录中并试一试。

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Summernote</title>
        <!-- include libraries(jQuery, bootstrap) -->
        <link href="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.css" rel="stylesheet">
        <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script> 
        <script src="http://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.js"></script> 

        <!-- include summernote css/js-->
        <link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.2/summernote.css" rel="stylesheet">
        <script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.2/summernote.js"></script>
    </head>
    <body>
        <div id="summernote">Hello Summernote</div>
        <script type="text/javascript">
            $(document).ready(function() {
                $('#summernote').summernote();
                $('button[data-original-title="Picture"]').click(function(){
                    // Set handler on Inset Image button when dialog window is opened  
                    $('.modal-dialog .note-image-btn').one('click', function(e) {
                        // Get Image URL area
                        var imageUrl = $('.modal-dialog .note-image-url').val();
                        // Send it to your backend
                        $.ajax({
                            url: "image_loader.php",
                            data: "url="+imageUrl,
                            type: "POST",
                            dataType: 'json'
                        }).success(function(data) {
                            if (typeof data[0] === 'string') {
                                $('img[src="'+imageUrl+'"]').attr('src', data);
                            } else {
                                // What to do if image downloading failed
                                window.alert('oops');
                            }
                        }).error(function() {
                            // What to do if ajax request failed
                            window.alert('oops');
                        });
                    });
                });  
            });
        </script>
    </body>
</html>

image_loader.php

<?php
if ($_POST['url']) {
    // Here you'd better put some logic to check that $_POST['url'] is a correct url before use it
    $image = file_get_contents($_POST['url']);

    if ($image) {
        // Put downloaded image on your server
        $file = fopen('imagename.jpeg', 'w');
        fwrite($file, $image);
        fclose($file);
    }

    /** 
     * Now your code needs to echo one of the following:
     * string Url of an uploaded image on your server
     * bool False if it failed
     * 
     * To avoid bool to string conversion during output response needs to be sent as JSON
     */
    $response = ($image) ? array('/PATH_TO_IMAGE_DIRECTORY_IF_NEEDED/imagename.jpeg') : array(false);
    echo json_encode($response);
}

例如这张图片https://imgsnap.com/images/2015/02/23/abstract_0005.jpg

更新(针对您对 img 样式的评论)

在summernote.js中加入下面一行,当图片url被编辑器处理后触发一个特殊事件。

$(document).trigger('imageUrlInserted', src);

将它放在insertImage()方法之前的第4095行(根据我的文件版本)

$image.css('width', Math.min($editable.width(), $image.width()));

现在在index.php里面

$('.modal-dialog .note-image-btn').one('click', function(e) {
...

...
});

用这个替换所有代码

// Get Image URL area
var imageUrl = $('.modal-dialog .note-image-url').val();
// Send it to your backend after the image been handled by the editor
$(document).on('imageUrlInserted', function(e, sourceUrl) {
    if (sourceUrl === imageUrl) {
        $.ajax({
            url: "image_loader.php",
            data: "url="+imageUrl,
            type: "POST",
            dataType: 'json'
        }).success(function(data) {
            if (typeof data[0] === 'string') {
                $('img[src="'+imageUrl+'"]').attr('src', data).removeAttr('style');
            } else {
                // What to do if image downloading failed
                window.alert('oops');
            }
        }).error(function() {
            // What to do if ajax request failed
            window.alert('oops');
        });
    }
});

【讨论】:

  • 这是解决我问题的好方法。它会将此图像添加到summernote 编辑器的当前内容区域吗?现在我不在家。我会尝试的:)
  • "它会将此图像添加到summernote 编辑器的当前内容区域。" - 是的,因为它不会改变summernote的逻辑。只需将两个额外的处理程序附加到它的节点。
  • 如何将summernote的img src=""值改为上传图片的路径。现在有了这个解决方案,我可以将图像下载到服务器,但 Summernote 的 src 路径仍然具有原始远程 URL
  • 查看 image_loader.php 中第 13-21 行和 index.php 中第 34-44 行的答案更新。请注意,如果您将无效 url 传递给 file_get_contents(),它将引发警告,因此您将无法在客户端获得正确的 json 响应。为避免这种情况,请在您的开发代码中使用 @file_get_contents()。在生产中,您将关闭显示错误,因此警告将不再是问题。
  • 会只替换新上传图片的src路径吗?如果summernote的内容区有不止一张图片怎么办?
猜你喜欢
  • 2014-03-04
  • 2016-06-13
  • 2015-01-03
  • 2016-01-02
  • 2014-07-27
  • 2018-07-13
  • 2019-07-01
  • 2015-07-04
  • 2017-01-01
相关资源
最近更新 更多