问题
I am working on the integration of jqueryfileupload plugin with AWS.I have completed the upload section successfully,but now i am looking to integrate the image resize feature.
I am using this plugin code.I have set up an example using minimum code which is as below.
index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>jQuery File Upload Example</title>
</head>
<body>
<input id="fileupload" type="file" name="files[]" data-url="aws/" multiple>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/vendor/jquery.ui.widget.js"></script>
<script src="js/jquery.iframe-transport.js"></script>
<script src="js/jquery.fileupload.js"></script>
<script>
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
done: function (e, data) {
$.each(data.result.files, function (index, file) {
$('<p/>').text(file.name).appendTo(document.body);
});
}
});
});
</script>
</body>
</html>
awssdk.php---This is the file I call after image is selected.
<?php
$bucket = "my bucket name";
$subFolder = ""; // leave blank for upload into the bucket directly
if (!class_exists('S3'))require_once('S3.php');
//AWS access info
if (!defined('awsAccessKey')) define('awsAccessKey', 'my key');
if (!defined('awsSecretKey')) define('awsSecretKey', 'my secret key');
$options = array( 'image_versions' => array(
'small' => array(
'max_width' => 1920,
'max_height' => 1200,
'jpeg_quality' => 95
),
'medium' => array(
'max_width' => 800,
'max_height' => 600,
'jpeg_quality' => 80
),
'thumbnail' => array(
'max_width' => 80,
'max_height' => 80
)
)
);
//instantiate the class
$s3 = new S3(awsAccessKey, awsSecretKey);
function getFileInfo($bucket, $fileName) {
global $s3;
$fileArray = "";
$size = $s3->getBucket($bucket);
$furl = "http://" . $bucket . ".s3.amazonaws.com/".$fileName;
$fileArray['name'] = $fileName;
$fileArray['size'] = $size;
$fileArray['url'] = $furl;
$fileArray['thumbnail'] = $furl;
$fileArray['delete_url'] = "server/php/index.php?file=".$fileName;
$fileArray['delete_type'] = "DELETE";
return $fileArray;
}
function uploadFiles($bucket, $prefix="") {
global $s3;
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
return "";
}
$upload = isset($_FILES['files']) ? $_FILES['files'] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
foreach($upload['tmp_name'] as $index => $value) {
$fileTempName = $upload['tmp_name'][$index];
$fileName = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name'][$index]);
$fileName = $prefix.str_replace(" ", "_", $fileName);
// $response = $s3->create_object($bucket, $fileName, array('fileUpload' => $fileTempName, 'acl' => AmazonS3::ACL_PUBLIC, 'meta' => array('keywords' => 'example, test'),));
$response = $s3->putObjectFile($fileTempName,$bucket,'images/'.$fileName,S3::ACL_PUBLIC_READ);
//print_r($response);
if ($response==1) {
$info[] = getFileInfo($bucket, $fileName);
} else {
echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
}
} else {
if ($upload || isset($_SERVER['HTTP_X_FILE_NAME'])) {
$fileTempName = $upload['tmp_name'];
$fileName = (isset($_SERVER['HTTP_X_FILE_NAME']) ? $_SERVER['HTTP_X_FILE_NAME'] : $upload['name']);
$fileName = $prefix.str_replace(" ", "_", $fileName);
//$response = $s3->create_object($bucket, $fileName, array('fileUpload' => $fileTempName, 'acl' => AmazonS3::ACL_PUBLIC, 'meta' => array('keywords' => 'example, test'),));
$response = $s3->putObjectFile($upload['tmp_name'],$bucket,$fileName,S3::ACL_PUBLIC_READ);
if ($response->isOK()) {
$info[] = getFileInfo($bucket, $fileName);
} else {
echo "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
}
}
header('Vary: Accept');
$json = json_encode($info);
$redirect = isset($_REQUEST['redirect']) ? stripslashes($_REQUEST['redirect']) : null;
if ($redirect) {
header('Location: ' . sprintf($redirect, rawurlencode($json)));
return;
}
if (isset($_SERVER['HTTP_ACCEPT']) && (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
header('Content-type: application/json');
} else {
header('Content-type: text/plain');
}
return $info;
}
?>
Here is the S3 class I am using.
The JqueryUploadPLugin ships with a server side PHP class to upload images which is great.But as I am using AWS I have to use their API to upload images and the plugin code won't work.As I mentioned above I have implemented the upload portion,but need help to create image thumbnails and different size images before I upload.i.e I want images to upload in 3 variations ex: original,1024x768,100x100
.
The UploadHandler.php has few functions for creating scaled images example:protected function create_scaled_image($file_name, $version, $options)
for scaling and others. I am stuck at integrating these functions as I am new to OO php and AWS.
Any one done something similar and can give inputs would be helpful
Thank you
回答1:
It seems you are trying to use the methods from UploadHandler class inside your code in awssdk.php.
I think the right way for you to go will be by customizing the UploadHandler class - more specifically the handle_file_upload
function. This will be probably more beneficial for you as you get access to all the good features of UploadHandler class this way.
And you can put just following lines in your awssdk.php
require('UploadHandler.php');
$upload_handler = new UploadHandler();
You can see currently the code in this function is storing the uploaded files in the path set in the "upload_dir" option. You just need to make an object of the S3
class inside that function and change the code to store uploaded file to S3.
I think the line you will have to change in UploadHandler is probably Line 703.
move_uploaded_file($uploaded_file, $file_path);
Should become:
$s3 = new S3(awsAccessKey, awsSecretKey);
$response = $s3->putObjectFile($uploaded_file,$bucket,$file->name,S3::ACL_PUBLIC_READ);
if ($response->isOK()) {
$info[] = getFileInfo($bucket, $fileName);
} else {
$file->error = "<strong>Something went wrong while uploading your file... sorry.</strong>";
}
You may also need to bring related code - for example the getFileInfo function - into the UploadHandler class.
Lines 707-711 appear to be for handling file uploads through PUT method. To handle this case you will have to keep those lines to let the file be uploaded to your server first and then transfer the file to S3, and then you can unlink the file in your server. But it is also safe to just comment out those lines if you are allowing POST method only.
Lines 697-701 appear to be for handling split uploads(I'm not sure). You will have to change that also if you wish to handle that case as well.
回答2:
Maybe what are you looking for is the Stream Wrapper. http://docs.aws.amazon.com/aws-sdk-php/guide/latest/service-s3.html#amazon-s3-stream-wrapper
"The Amazon S3 stream wrapper allows you to store and retrieve data from Amazon S3 using built-in PHP functions like file_get_contents, fopen, copy, rename, unlink, mkdir, rmdir, etc."
I'm looking the for the same solution too. I've founded this https://gist.github.com/tim-peterson/8172999 my it can help. I'm still waiting AWS approve my account so I couldn't test any solution.
来源:https://stackoverflow.com/questions/17490045/integrating-jqueryupload-plugin-with-aws-php-sdk-to-upload-images-to-s3