试图将图像卷入S3

时间:2017-01-12 06:30:50

标签: php laravel amazon-s3

到目前为止,我有这个。但我似乎无法让它发挥作用。

$ch = curl_init('https://urlofIMAGEIwanttoretrieve.php');
$fp = fopen('php://temp', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
fwrite($fp, $data);

$s3 = Storage::disk('s3');
$filename = sha1(date('Y-m-d H:i:s')).".jpg";
$s3->put('products/large/'.$filename, $fp, 'public');

这样可以很好地上传文件,但是它的0字节。

如果我这样做

$file = Image::make($fp)->encode('jpg');
$s3->put('products/large/'.$filename, $file, 'public');

我得到了

Unable to init from given binary data.

如何将图片从PHP网址直接卷入S3

1 个答案:

答案 0 :(得分:0)

使用PHP内存包装器存储图像的内容,并使用$s3->putObjectFile()方法:

方法1:

   $ch = curl_init('https://urlofIMAGEIwanttoretrieve.php');
    $fp = fopen('php://temp', 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $data = curl_exec($ch);
    fwrite($fp, $data);

    $s3 = Storage::disk('s3');
    $filename = sha1(date('Y-m-d H:i:s')).".jpg";
    $s3->put('products/large/'.$filename, file_get_contents($fp), 'public');

方法2:

$ch = curl_init('https://urlofIMAGEIwanttoretrieve.php');
$fp = fopen('php://temp', 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
fwrite($fp, $data);

$s3 = Storage::disk('s3');
$filename = sha1(date('Y-m-d H:i:s')).".jpg";
$s3->put('products/large/'.$filename, $fp->__toString(), 'public');

方法3:

$ch = curl_init('https://urlofIMAGEIwanttoretrieve.php');
    $fp = fopen('php://temp', 'wb');
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $data = curl_exec($ch);
    fwrite($fp, $data);

    $s3 = Storage::disk('s3');
    $filename = sha1(date('Y-m-d H:i:s')).".jpg";
    $fp  = $fp->stream();
    $s3->put('products/large/'.$filename, $fp->__toString(), 'public');