繁体   English   中英

如何使用js和php在远程服务器上保存文件?

[英]How can I save a file on remote server with js and php?

我使用html2canvas.js保存图像。 如果与php文件位于同一位置/服务器,我就知道如何使用它。

但是现在我想使用eBay列表模板中的脚本并将图像保存到我的服务器。 这可能吗?

这是我的php文件:

<?php
function ImageFillAlpha($image, $color) {
    imagefilledrectangle($image, 0, 0, imagesx($image), imagesy($image), $color);
}
function imageCreateCorners($sourceImageFile, $name, $radius) {
    # test source image
    if (file_exists($sourceImageFile)) {
        $res = is_array($info = getimagesize($sourceImageFile));
    }
    else
        $res = false;
    # open image
    if ($res) {
        $w = $info[0];
        $h = $info[1];
        switch ($info['mime']) {
            case 'image/jpeg': $src = imagecreatefromjpeg($sourceImageFile);
                break;
            case 'image/gif': $src = imagecreatefromgif($sourceImageFile);
                break;
            case 'image/png': $src = imagecreatefrompng($sourceImageFile);
                break;
            default:
                $res = false;
        }
    }
    # create corners
    if ($res) {
        $q = 10; #change this if you want
        $radius *= $q;
        $r = 0;
        $g = 0;
        $b = 0;
        $nw = $w * $q;
        $nh = $h * $q;
        $img = imagecreatetruecolor($nw, $nh);
        $alphacolor = imagecolorallocate($img, 0, 0, 0);
        imagealphablending($img, false);
        imagesavealpha($img, true);
        imagefilledrectangle($img, 0, 0, $nw, $nh, $alphacolor);
        imagefill($img, 0, 0, $alphacolor);
        imagecopyresampled($img, $src, 0, 0, 0, 0, $nw, $nh, $w, $h);
        # resize image down
        $dest = imagecreatetruecolor($w, $h);
        imagealphablending($dest, false);
        imagesavealpha($dest, true);
        imagefilledrectangle($dest, 0, 0, $w, $h, $alphacolor);
        imagecopyresampled($dest, $img, 0, 0, 0, 0, $w, $h, $nw, $nh);
        # output image
        $res = $dest;
        imagedestroy($src);
        imagedestroy($img);
        imagepng($res, 'cart/'.$name);
    }
    return $res;
}
$image = $_POST['plate_image'];
$ebay_user = $_POST['ebay_user'];
$decoded = base64_decode(str_replace('data:image/png;base64,', '', $image));
$date = date('m-d-Y', time());
$name = "evgrave-".$ebay_user."-".$date.".png";
file_put_contents("/var/www/html/SERVERLOCATION/img/" . $name, $decoded);
$full_path = "/var/www/html/SERVERLOCATION/img/" . $name;
imageCreateCorners($full_path, $name, 25);
$name1 = 'img/'.$name;
echo '<div class="download">';
echo '<br /><p>Result:</p><br />';
echo "<img src='$name1' alt='image' id='img' />";
echo '<p class="download-btn"><a id="download" class="left" href="download.php?&img=' . $name1 .'">Download Image</a><a id="send" class="right" href="#">Send Order</a></p>';
echo '</div>';
?>

这是js函数:

                html2canvas($('#output'), {
                    "logging": true,
                    "onrendered": function(canvas){
                        var dataURL = canvas.toDataURL("image/png");
                        $.post('REMOTESERVER/image.php',{
                            plate_image: dataURL,
                            ebay_user: ebay_user
                        },function(data){
                            $('.overlay').remove();
                            $('#result').html(data);
                        });  
                    }
                });

有什么帮助吗? 谢谢

您可以通过多种方式进行操作。 这是其中两个(尽管它们的概念相同)。

方法1-SCP

  • 将文件上传到本地服务器
  • 创建一个在后台运行的脚本,并在文件写入后通过cron / straight将ssh2_scp_send()本地文件通过cron / straight传输到远程服务器。

方法2-FTP

这与方法1非常相似,但是使用不同的协议。 的FTP

  • 将文件上传到本地服务器
  • 创建一个脚本,该脚本通过FTP连接到远程服务器,并在文件写入后通过cron / straight将本地文件上传/复制到远程服务器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM