3

我有一个脚本允许我使用 jcrop 裁剪图像。为此,我必须使用input type file. 我想扩展我的应用程序的功能,允许用户从他们的 facebook albuns 中选择照片,这段代码正在工作,但它返回了图像的 URL。从 URL 我创建一个像这样的图像对象

var oImage = document.getElementById('preview');

但这是一个图像对象,而不是一个文件对象,它不允许我获得一些文件属性,如oImage.sizeoImage.type. 原代码是这样的

<input type="file" name="image_file" id="image_file" onchange="fileSelectHandler()" />

和javascript代码

function fileSelectHandler() {
   var oFile = $('#image_file')[0].files[0];
   ...
}

现在我有一个回调函数是 Facebook 图像 URL,我希望它像我从计算机上传图像一样工作,因为这个输入类型文件被提交到服务器。

var oImage = document.getElementById('preview');
oImage.src = photo.source;

photo.source我的网址在哪里。

所以我的问题是:如何使用 javascript/ajax 将 URL 转换为文件类型?我还需要将该类型放入文件输入中,因为这是将要提交的表单。提前致谢。

4

1 回答 1

1

您应该将 URL 发布到您的服务器,然后您可以使用 PHP 下载远程图像并对其进行操作。

提示代码:

if(isset($_POST['image_url'])){
    //Make sure to validate $_POST['image_url'] before using it
    $image = file_get_contents($_POST['image_url']);
} else {
    $image = file_get_contents($_FILES["file"]["tmp_name"]);
}

我通常使用 JQuery, JQuery sample ajax post:

$.post('server.php', {image_url:photo.source}, function(data){
  //do stuff on server response
});
于 2013-05-20T14:24:19.237 回答