如何使用AWS S3进行图像处理(PHP)


How to use AWS S3 for image processing (PHP)

对于我的网站,我想使用使用API的AWS S3服务存储所有图像。

如何通过API/SDK来做这件事

1)如何上传图像/文件在不同的文件夹使用API(从我的网站)。

2)如何在飞行中调整/裁剪图像。例如50x50 px, 250x250 px.

3)强制下载

谢谢

我认为AWS没有内置的功能来调整S3中存储的任何图像的大小。正如eldblz所提到的,您必须自己调整大小。您可以使用S3流包装器。

S3流包装器非常神奇:http://docs.aws.amazon.com/aws-sdk-php/v2/guide/feature-s3-stream-wrapper.html

它将允许您使用内置的PHP函数,如file_get_contents和file_put_contents。

  1. 获取原始文件的详细信息:

    # If you have stream wrapper enabled, 
    # getimagesize will get information from the S3
    # you have to pass the S3 URI though
    list($width, $height) = getimagesize('s3://bucket/key');
    # making the new image 50x50
    $new_width = 50;
    $new_height = 50;
    $new_image = imagecreatetruecolor($new_width, $new_height);
    
  2. 使用file_get_contents(或fopen)获取图像数据:

    $data = file_get_contents('s3://bucket/key');
    
  3. 从数据中创建图像资源并调整大小:

    $source = imagecreatefromstring($data); 
    # Resize
    imagecopyresized($new_image, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
  4. 输出:
  5. header('Content-Type: image/jpeg');
    imagejpeg($thumb);
    

希望这对你有帮助!

你的问题有点模糊,所以答案不可能精确。我将给出一些建议或库开始:

  1. 如何使用API(从我的网站)上传图像/文件在不同的文件夹。

这个库应该是开始使用S3和文件的好地方:https://github.com/tpyo/amazon-s3-php-class

  • 如何在飞行中调整/裁剪图像。例如50x50 px, 250x250 px.
  • 应该可以:https://github.com/eventviva/php-image-resize或者你可以试试PHP Imagick: http://php.net/manual/en/book.imagick.php

  • 下载。
  • Stackoverflow已经为你提供了答案:如何使用PHP强制下载文件