我正在将 jqueryfileupload 插件与 AWS 集成。我已经成功完成了上传部分,但现在我正在寻找集成图像调整大小功能。
我正在使用这个插件代码。我已经使用最低代码设置了一个示例,如下所示。
索引.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;
}
?>
这是我正在使用的 S3 类。
JqueryUploadPLugin 附带一个服务器端 PHP 类来上传图片,这很棒。但是当我使用 AWS 时,我必须使用他们的 API 上传图片,插件代码不起作用。正如我上面提到的,我已经实现了上传部分,但在我上传之前需要帮助创建图像缩略图和不同尺寸的图像。即我希望图像以 3 种变体上传ex: original,1024x768,100x100
。UploadHandler.php几乎没有用于创建缩放图像示例的函数:
用于缩放和其他。因为我是 OO php 和 AWS 的新手,所以我一直在集成这些功能。任何做过类似事情并且可以提供意见的人都会有所帮助
谢谢protected function create_scaled_image($file_name, $version, $options)