-1

For my website i want to store all image using AWS S3 service using API.

How to do that thing via API/SDK

1) How to upload image/file in different folder using API (from my website).

2) How to resize/crop image on the fly. eg 50x50 px, 250x250 px.

3) Force download.

Thanks

Yogesh Saroya
  • 1,401
  • 5
  • 23
  • 52
  • Assuming you've already looked through the AWS SDK for PHP, what have you got so far? As for resizing and cropping, I believe your application's server should be taking care of that and then send to your S3 bucket. – flauntster Aug 13 '15 at 07:31

2 Answers2

3

I don't think AWS has a built-in feature to resize any stored image in S3. As eldblz mentioned, you have to do the resizing on your own. You can use S3 stream wrapper.

The S3 stream wrapper is pretty amazing: http://docs.aws.amazon.com/aws-sdk-php/v2/guide/feature-s3-stream-wrapper.html

It will allow you to use built-in PHP functions like file_get_contents and file_put_contents.

  1. Get details of the original file:

    # 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. Get the image data with file_get_contents(or fopen):

    $data = file_get_contents('s3://bucket/key');
    
  3. create an image resource from the data and resize:

    $source = imagecreatefromstring($data); 
    
    # Resize
    imagecopyresized($new_image, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
    
  4. Output:

    header('Content-Type: image/jpeg');
    imagejpeg($thumb);
    

Hope this helps!

Community
  • 1
  • 1
mmohiudd
  • 310
  • 2
  • 10
0

Your question is a bit vague, so the answer cannot be precise. I'll give some advice or library to start:

  1. How to upload image/file in different folder using API (from my website).

This library should be a nice place to start working with S3 and files: https://github.com/tpyo/amazon-s3-php-class

  1. How to resize/crop image on the fly. eg 50x50 px, 250x250 px.

This should do the trick: https://github.com/eventviva/php-image-resize Or you can try PHP Imagick: http://php.net/manual/en/book.imagick.php

  1. Force download.

Stackoverflow already has the answer for you: How to force file download with PHP

Hope this helps get you started.

Community
  • 1
  • 1
eldblz
  • 758
  • 3
  • 11
  • 24
  • 1st and 3rd answer are fine. 2nd > resize/crop is for remote image (hosted in asw ). eg ... asw.com/abc.jpg?h=200&w=200&q=100 – Yogesh Saroya Aug 14 '15 at 09:40
  • You can use s3 library to get object S3::getObject($bucketName, $uploadName) then use the image library – eldblz Aug 14 '15 at 09:43