我正在研究jqueryfileupload插件與AWS的集成.I已成功完成上傳部分,但現在我正在尋找集成圖像大小調整功能。
我正在使用this插件代碼。我已經使用最小代碼設置了一個示例,如下所示。
將JqueryUpload插件與AWS php sdk集成將圖片上傳到S3
的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 ---這是被選中的圖像後,我調用該文件。
<?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是我正在使用的S3類。
JqueryUploadPLugin附帶一個服務器端PHP類來上傳圖片,這很好。但是當我使用AWS時,我必須使用他們的API來上傳圖片,插件代碼將無法工作。正如我上面提到的,我已經實現了上傳部分,但需要幫助,以創建圖像縮略圖和不同大小的圖像,然後我upload.ie我想要圖像上傳3個變種ex: original,1024x768,100x100
。
UploadHandler.php幾乎沒有用於創建縮放圖像的功能,例如:用於縮放等的protected function create_scaled_image($file_name, $version, $options)
。由於我是OO php和AWS的新手,因此我被困在集成這些功能。
任何一個做過類似的東西,可以給投入將是有益的
謝謝
你還沒有提到你卡在哪裏?如果你已經有上傳工作,你有什麼問題只是添加更多的代碼來創建縮略圖? – rineez
是的,但我想使用與UploadHandler.php(我已經添加了一個鏈接)相同的功能。它有一些很棒的功能,但它涉及到一些依賴,這讓我感到困擾。我想要幫助使用這些函數的例子是''protected function create_scaled_image($ file_name,$ version,$ options)'縮放和其他 – KillABug