Search code examples
phpamazon-web-servicesamazon-s3imagick

Use imagick with S3 AWS


These are my first steps with AWS S3, so and my goal is to use imagick for manipulating the image before I upload to S3 AWS.

I use this function for resize and scale the image (works perfect on output browser):

function resizeImg($img, $width, $height) {

    $i = new Imagick($img);

    $gig = $i->getImageGeometry();

    // crop the image
    if(($gig['width']/$width) < ($gig['height']/$height)) {

        $i->cropImage($gig['width'], floor($height * $gig['width'] / $width), 0, (($gig['height'] - ($height * $gig['width'] / $width)) / 2));

    } else {

        $i->cropImage(ceil($width * $gig['height'] / $height), $gig['height'], (($gig['width'] - ($width * $gig['height'] / $height)) / 2), 0);
    }

    $i->ThumbnailImage($width, $height, true);

    $i->setImageFormat("jpeg");
    $i->setImageCompressionQuality(90);

    $i->getimageblob(); // I tried it with and without getimageblob

    return $i;
}

Thats my attempt, Upload to S3:

$tmpImg = $_FILES['inputImage']['tmp_name'];

$newImgFile = resizeImg($tmpImg, 100, 100); 

$s3->putObjectFile($newImgFile, BUCKET_NAME, $newfilename, S3::ACL_PUBLIC_READ);

But I get this error:

Warning: S3::inputFile(): Unable to open input file: ���� in.....

What I`m doing wrong?

Is imagick not compatible with S3 AWS?

Or is there a better way?

And my next goal is to put different image resizes to S3, e.g 100x100, 300x300.... from same input file, but of course I have to solve before the first goal.

EXTRA:

With this solution from here: PHP Imagick: Write Image directly to Amazon S3?

I get this Error:

Warning: S3::putObject(): [SignatureDoesNotMatch] The request signature we calculated does not match the signature you provided. Check your key and signing method.

If I do this: $new = urlencode($new); or $new = strtolower($new); ...Works to upload but no readable!

The error came from extra spaces or slashes in there, so far as I have understood

And if its important, here the Url from the image in S3

https->awsCount.xxxx.xxxxx/img.jpg?AWSAccessKeyId=xxxx&Expires=1383787878&Signature=xxxx&x-amz-security-token=AQoDYXdzEGAasAJlJXrt/7FPf84wE9stfgBfEoWaPMDHlubQBlQ6oMY6sNMT4cizkEm9khypHulLB/zJ%2BbqqAErvFBKKs2I9bKDBzrKYKhgRn%2Bta057CZaLougsxHLRGquhd5H26br/Odkq98%2BoDTnfK0LHFa9vYbX6sXDIzCSHcZx4%2B5o0y3cKlxCMsYLqw6wYD1DNjJ%2BHlWWuh%2B6V0FtpbYaErB1XUZfRRZdx3ZPEOvyZxQS7uzP8C3B1nK0wo3uqqSAhn9PPtQt5jrRutYRao2KugxK8TbkZbr/v5NOYSbpc%2BmI2iYYrUjylqgenzf85Avss0CA1GfOzg%2BMs2/TQ7evH7epr09B8Vyd89Gk1XQpVMyrTSvbzDYE8UCcgrUrXgdHTYWdGLVZ%2BBHzft9nHtNhggePD6AXMuIP%2Ba6ZMF


Solution

  • I'll have the answer, I show the whole code for each one:

    if (!empty($_FILES["input_file"])) {    
    
            if ($_FILES["input_file"]["error"] !== UPLOAD_ERR_OK) {
    
                echo "<p>An error occurred.</p>";
                exit;
            }    
    
            // Move/Copy from temporary file to local file
    
            $success = move_uploaded_file($_FILES["input_file"]["tmp_name"],
                    'local_temp_file_directory/' . $_FILES["input_file"]["name"]);
    
            if (!$success) {
    
                echo "<p>Unable to save file.</p>";
                exit;
            }    
        }
    
        // I make a function to get the local file and save the edited file
    
        function resizeImg($img_from_local_file, $width, $height, $pathToSaveImg) {
    
            $i = new Imagick($img_from_local_file);
            $gig = $i->getImageGeometry();
    
            if(($gig['width']/$width) < ($gig['height']/$height)) {
    
                $i->cropImage($gig['width'], floor($height * $gig['width'] / $width), 0, (($gig['height'] - ($height * $gig['width'] / $width)) / 2));
    
            } else {
    
                $i->cropImage(ceil($width * $gig['height'] / $height), $gig['height'], (($gig['width'] - ($width * $gig['height'] / $height)) / 2), 0);
            }
    
            $i->ThumbnailImage($width, $height,true);
            $i->setImageFormat("jpeg");
            $i->setImageCompressionQuality(90);
            $i->writeImage($pathToSaveImg);
            return $i->getimage();
        }
    
        // Call the resizeImg function
    
        resizeImg("local_temp_file_directory/".$_FILES["input_file"]["name"], 40, 40, "local_temp_file_directory/resized_".$_FILES["input_file"]["name"]);
    
        $s3->putObject(
            S3::inputFile("local_temp_file_directory/resized_".$_FILES["input_file"]["name"]),
            BUCKET_NAME, 
            "make_a_new_image_name".date("his").".jpg", 
            S3::ACL_PUBLIC_READ, 
            array(), 
            array("Content-Type"=>'image/jpeg'));
    
     // Remove the temporary local file
     unlink("local_temp_file_directory/".$_FILES["input_file"]["name"]);
     unlink("local_temp_file_directory/resized_".$_FILES["input_file"]["name"]);
    

    Thats it... Enjoy it!