如何使用Laravel 5处理从AWS S3下载的图像?

问题描述:

我想从AWS S3下载图像并使用php进行处理。我使用“imagecreatefromjpeg”和“getimagesize”来处理我的图像,但它似乎是如何使用Laravel 5处理从AWS S3下载的图像?

Storage :: disk('s3') - > get(imageUrlonS3);

检索二进制图像,并给我错误。这是我的代码:

function createSlices($imagePath) { 

       //create transform driver object 
       $im = imagecreatefromjpeg($imagePath); 
       $sizeArray = getimagesize($imagePath); 

       //Set the Image dimensions 
       $imageWidth = $sizeArray[0]; 
       $imageHeight = $sizeArray[1]; 

       //See how many zoom levels are required for the width and height 
       $widthLog = ceil(log($imageWidth/256,2)); 
       $heightLog = ceil(log($imageHeight/256,2)); 


       //more code here to slice the image 
       . 
       . 
       . 
       . 
      } 

      // ex: https://s3-us-west-2.amazonaws.com/bucketname/image.jpg 
      $content = Storage::disk('s3')->get(imageUrlonS3); 
      createSlices($content); 

我在这里错过了什么?

感谢

+0

你能提供确切的错误,你看到了吗? – Castaglia

我想你是对你的问题是什么问题 - 在get方法返回的自身形象,而不是图像的位置的来源。当您将其传递给createSlices时,您传递的是二进制数据,而不是其文件路径。在createSlices的内部,您可以拨打imagecreatefromjpeg,它需要一个文件路径,而不是图像本身。

如果确实如此,您应该能够使用createimagefromstring而不是createimagefromjpeggetimagesizefromstring而不是getimagesize。函数createimagefromstringgetimagesizefromstring每个都需要图像的二进制字符串,我相信这是你的。

这里的相关文件:

createimagefromstring - http://php.net/manual/en/function.imagecreatefromstring.php

getimagesizefromstring - http://php.net/manual/en/function.getimagesizefromstring.php

产生的代码可能是这个样子:

function createSlices($imageData) { 
    $im = imagecreatefromstring($imageData); 
    $sizeArray = getimagesizefromstring($imageData); 

    //Everything else can probably be the same 
    . 
    . 
    . 
    . 
} 

$contents = Storage::disk('s3')->get($imageUrlOnS3); 
createSlices($contents); 

请注意我没有测试过这,但我相信我在你的问题和w中能看到的我在文档中看到这可能就是这样做的。

+0

谢谢xjstratedgebx工作:) –