1

I'm trying to create a rotate image function, the image I am trying to rotate is in my S3 bucket.

So I am trying to create a function to grab the image from my bucket, save a local copy, rotate the image, upload the image back to S3.

My Script:

$s3 = new S3(myAccessKey, mySecretKey);
$target_path = CLOUDFRONTURLFORPHOTOS; //Cloudfront URL for photos

$filetodownload = $target_path."filename.jpg";
$savetofile = "foldername/filename.jpg";

$s3->getObject(MYBUCKET,CLOUDFRONTURLFORPHOTOS, array("filename.jpg" => $savetofile));

rotateImage($destpath, $destpath, $degree);

if ($s3->putObjectFile($destpath, MYBUCKET, $imagepath, S3::ACL_PUBLIC_READ)) {  
  echo 'Photo has been rotated and uploaded to the Photo Bucket.';
}
function rotateImage($sourceFile,$destImageName,$degreeOfRotation)
{
  //get the detail of the image
  $imageinfo=getimagesize($sourceFile);
  switch($imageinfo['mime'])
  {
   //create the image according to the content type
   case "image/jpg":
   case "image/jpeg":
   case "image/pjpeg": //for IE
        $src_img=imagecreatefromjpeg("$sourceFile");
                break; 
    case "image/gif":
        $src_img = imagecreatefromgif("$sourceFile");
                break;
    case "image/png":
    case "image/x-png": //for IE
        $src_img = imagecreatefrompng("$sourceFile");
                break;
  } 

  //rotate the image according to the spcified degree
  $src_img = imagerotate($src_img, $degreeOfRotation, 0);

  //output the image to a file
  imagejpeg ($src_img,$destImageName);
}

I get this error:

Warning: S3::getObject(bucketname, public-read): [0] Unable to open save file for writing: Array in S3.php on line 326 Warning: imagerotate() expects parameter 1 to be resource, null given in myfile.php on line 300 Warning: imagejpeg() expects parameter 1 to be resource, boolean given in myfile.php on line 303

Please advise. What is missing in my code? Or is there a better way to rotate an image directly in the S3 bucket without downloading the file locally?

martti d
  • 2,552
  • 2
  • 17
  • 20
  • for grabbing the picture itself, there is a good post here: http://stackoverflow.com/questions/724391/saving-image-from-php-url-using-php. As for the rest... once you got the picture locally, i'm sure your code will be fine or close to fine. – Viridis Dec 11 '13 at 08:43
  • i tried those already but i get warnings like failed to open stream: No such file or directory even though the file exists. so i'm guessing the PHP copy and fopen functions do not work on S3 objects. – martti d Dec 11 '13 at 09:15
  • Can we rotate image without downloading? – Chetan Gaikwad May 08 '15 at 11:01

0 Answers0