Rotate Image in Amazon S3
Asked Answered
S

3

6

I have a bunch of images in Amazon S3 that I need to physically rotate. I currently do this by downloading the image to my server, rotating it using GD and overwriting it back to S3.

This process takes ~5 secs per image. I was wondering if there is any AWS API or such that can do this rotation directly in S3, preferably as in a batch mode?

I would appreciate it if anyone who has any experience with that kind of stuff can give me any pointers!

Senseless answered 4/12, 2015 at 18:53 Comment(3)
Check AWS Lambda. Also, this is very broad and primarily opinion based. Will probably get flagged.Missis
@NitaiJ.Perez: Thank you for the comment. I hope this does not get flagged. Checking if a software solution/API exists should not yield opinion based answers...Senseless
I tend to agree, though I find this question is exactly in the gray area. Decided not to flag, of course.Missis
O
6

There is no way to rotate an image 'on' S3. Any method you employ is going to have to read the file from S3, do the rotation, and write it back to S3.

If the server you are doing it on now is not an EC2 instance, than its worth a try to do it there - the latency will be reduced quite a bit. Lambda is another option for you in that it will run within the AWS infrastructure, so network overhead will be reduced.

Obvert answered 4/12, 2015 at 21:50 Comment(1)
Thank you for your comment, I appreciate your input. I will checkout lambda.Senseless
S
2

Not quite sure what your constraints might be, but if you're preparing the images for a webpage, you could rotate them client-side with CSS, and eliminate the additional requests to S3 and the load on your server.

index.css

img {
  transform: rotate(90deg);
}
Shevat answered 18/12, 2016 at 3:57 Comment(0)
S
0

I just did the procedure in PHP. First you should download the image from S3. Then locally it must rotate and then go up again in S3 https://chat.openai.com/share/175bd4d7-109c-4239-9b3e-4e7d6cff1395

My function as:

 function rotate(
    $src, float $angle = 90
) {

    $result = $this->S3->getObject([
        'Bucket' => $this->S3_BUCKET_FOTOS,
        'Key' => $src
    ]);

    $temporaria = tempnam(sys_get_temp_dir(), 'rotacionado');
    file_put_contents($temporaria, $result['Body']);

    $source_image = imagecreatefromjpeg($temporaria);

    $rotated = imagerotate($source_image, $angle, 0);
    imagejpeg($rotated, $temporaria);
    imagedestroy($source_image);
    imagedestroy($rotated);

    $result = $this->S3->putObject([
        'Bucket' => $this->S3_BUCKET_FOTOS,
        'Key' => $src,
        'Body'   => fopen($temporaria, 'rb'),
        // 'SourceFile' => $temporaria
    ]);
    unlink($temporaria);
    return $result;
}
Silsby answered 30/1 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.