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?