【问题标题】:Php transform file from url to blob to javascriptphp 将文件从 url 转换为 blob 到 javascript
【发布时间】:2019-10-27 20:05:05
【问题描述】:

我正在尝试将图像从 url 转换为可以在 javascript 中使用的 blob 文件,但我失败了,我该怎么做或者这甚至可能:

到目前为止,我已经这样做了:

    // $request->location is the url to the file in this case an image
    $img = file_get_contents($request->location);

    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, $request->location);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HEADER, 0);

    // grab URL and pass it to the browser
    $img = curl_exec($ch);

    // close cURL resource, and free up system resources
    curl_close($ch);

    return $img;

我想返回该 blob 并将其附加到我的客户端/javascript 端的 FileReader 对象。

【问题讨论】:

标签: javascript php laravel blob


【解决方案1】:

只需使用file_get_contents 即可为您提供图像的原始内容。无需再执行 curl 请求。

return file_get_contents($request->location);

【讨论】:

  • 位置也可以是 pdf,因为我想将 javascript 中的 blob 附加到我的 Filereader 对象
  • 是的,file_get_contents 将加载任何文件,无论其类型如何。
  • 您是否有将其放入 FileReader 图像对象的示例?目前它看起来像这样:array(5) { ["type"]=> string(10) "image/jpeg" ["file"]=> string(346734) "ÿØÿàJFIFÿÛCÿÂQ°"ÿÄÿÄÌEN"­@ê³J©Î9¯.ÀJ±É)»I ËuÙYÔeÞfʰ5önù¬9dô¬ú -t(-+\õí<(÷"çöÈÑøf¬R'­¨Ü 但它应该传递一个对象
【解决方案2】:

您曾经使用过“image-to-blob” javascript 库吗?也许可以试一试。 请查看here

或者您也可以尝试使用“JavaScript-Canvas-to-Blob”,更多详情请查看here

我曾经尝试使用javascript将图片url转换为base64,如果你有兴趣我可以分享代码。

【讨论】:

    猜你喜欢
    • 2017-01-05
    • 2011-08-31
    • 1970-01-01
    • 2020-07-20
    • 2020-06-16
    • 2015-02-17
    • 2019-11-22
    • 2015-01-25
    • 2014-07-18
    相关资源
    最近更新 更多