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


How to processing an image downloaded from AWS S3 with Laravel 5?

我想从 AWS S3 下载一个图像并使用 php 进行处理。我正在使用"imagecreatefromjpeg"和"getimagesize"来处理我的图像,但似乎

Storage::d isk('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);

我在这里错过了什么?

谢谢

我认为你的问题是正确的 - 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);

请注意,我还没有对此进行测试,但我相信从我在您的问题中看到的内容以及我在文档中阅读的内容来看,这可能会做到这一点。