从URL上传Summernote图像

hak*_*iko 6 javascript php ajax image-uploading summernote

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

目前,summernote仅获取src属性的图像源.我想将图像存储在我自己的Amazon S3 Bucket或VPS上.

关于summernote图片上传的文档很多,但是所有这些文档都是从pc上传而不是从URL上传的.

我该如何覆盖此功能? 在此输入图像描述


图像对话框 在此输入图像描述

Ale*_*kov 1

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

该示例包括客户端和后端代码。两者都经过测试。您所需要做的就是将这两个脚本放入您的 Web 服务器目录之一并尝试一下。

索引.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>
Run Code Online (Sandbox Code Playgroud)

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);
}
Run Code Online (Sandbox Code Playgroud)

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

更新(对你关于 img 样式的评论)

将以下行放入 Summernote.js 中,以便在编辑器处理图像 url 时触发特殊事件。

$(document).trigger('imageUrlInserted', src);
Run Code Online (Sandbox Code Playgroud)

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

$image.css('width', Math.min($editable.width(), $image.width()));
Run Code Online (Sandbox Code Playgroud)

现在在index.php里面

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

...
});
Run Code Online (Sandbox Code Playgroud)

用这个替换所有代码

// 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');
        });
    }
});
Run Code Online (Sandbox Code Playgroud)