Php 将图像上载到tmp文件夹时出现问题,请调整大小,然后上载到S3 bucket
我花了一整天的时间来研究如何在tmp中存储一个图像,改变它的大小,然后将它上传到我的S3存储桶中。S3存储桶工作正常,我可以在不调整大小的情况下上传图像。resize函数是createThumbnail函数,如果不与S3结合使用,可能是因为它的格式错误或其他原因。第二个文件“init.php”包含所有函数。图像被上传到临时文件夹,但当我运行脚本时,它在调整大小后不会被上传。我没有在本地主机上获得任何错误日志,当我将代码上传到AWS实例时,它只返回一个内部服务器错误,但实例中的错误日志奇怪地为空。。。我需要对这个问题有一些新的看法 banner_image.phpPhp 将图像上载到tmp文件夹时出现问题,请调整大小,然后上载到S3 bucket,php,amazon-web-services,amazon-s3,aws-sdk,Php,Amazon Web Services,Amazon S3,Aws Sdk,我花了一整天的时间来研究如何在tmp中存储一个图像,改变它的大小,然后将它上传到我的S3存储桶中。S3存储桶工作正常,我可以在不调整大小的情况下上传图像。resize函数是createThumbnail函数,如果不与S3结合使用,可能是因为它的格式错误或其他原因。第二个文件“init.php”包含所有函数。图像被上传到临时文件夹,但当我运行脚本时,它在调整大小后不会被上传。我没有在本地主机上获得任何错误日志,当我将代码上传到AWS实例时,它只返回一个内部服务器错误,但实例中的错误日志奇怪地为空。
<?php
include "init.php";
if (@session($_SESSION["buddy"])){
$valid_file_formats = array("jpg","png","jpeg");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST" && !empty($_FILES['banner_image']['tmp_name']) && !empty($_FILES['banner_image']['name'])){
$folder = usernameFromEmail($_SESSION["user"]); // session based on users email
$buddy = $_SESSION["user"];
$name = $_FILES['banner_image']['name'];
$size = $_FILES['banner_image']['size'];
$path = $folder."/cover/"; // path to image folder
$temp = explode(".", $name);
$newfilenameKey = round(microtime(true)); // generating random file name
$newfilename = $newfilenameKey . "." . "png"; // always using png
if(strlen($newfilename) && strlen($name)) {
$ext = explode(".", $name);
$ext = end($ext);
if(in_array($ext,$valid_file_formats)) { // valid file format array check
if($size<=(10485760)) { // size check in bits
$tmp = $_FILES['banner_image']['tmp_name'];
$file_name = $path.$newfilename; // full path including file name
if(putS3IMG($bucket, $file_name, $tmp)){ // upload untouched image to S3 bucket
list($width, $height) = getimagesize($tmp);
$type = 2;
$w = 300;
$h = 300 * ($height / $width);
// getting new dimensions for the new image
if(createThumbnail(1,$tmp,$w,$h,$path,$file_name) == 1){ // function for smaller image
return json_encode(array("succ" => 1));
}
}
}
}
}
}
}
?>
下面是包含我的函数的banner_image.php文件中包含的init.php文件
<?php
// connection to S3Client .... $client
function createThumbnail($type,$image_name,$new_width,$new_height,$uploadDir,$moveToDir){
global $bucket;
//$type: defines the name of the new file
//image_name: tmp name
//uploadDir: path
//moveToDir: files new path incl. file name
$mime = getimagesize($image_name);
if($mime['mime']=='image/png') {
$src_img = imagecreatefrompng($image_name);
} else if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
$src_img = imagecreatefromjpeg($image_name);
}
$old_x = imageSX($src_img);
$old_y = imageSY($src_img);
$thumb_w = $new_width;
$thumb_h = $new_height;
$dst_img = ImageCreateTrueColor($thumb_w,$thumb_h);
$background = imagecolorallocate($dst_img, 0, 0, 0);
imagecolortransparent($dst_img, $background);
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
$image_name_new = explode(".", $moveToDir);
if ($type == 1){
$new_image_name = $image_name_new[0] . "_thumb." . $image_name_new[1];
} else if ($type == 2){
$new_image_name = $image_name_new[0] . "_image." . $image_name_new[1];
}
$tmpPath = tempnam(sys_get_temp_dir(), $new_image_name); // getting temporary directory
if($mime['mime']=='image/png') {
imagepng($dst_img,$tmpPath,8);
} else if($mime['mime']=='image/jpg' || $mime['mime']=='image/jpeg' || $mime['mime']=='image/pjpeg') {
imagejpeg($dst_img,$tmpPath,80);
}
imagedestroy($dst_img);
imagedestroy($src_img);
if(putS3IMG($bucket, $image_name, $tmpPath)){ // this part does not work
return 1;
} else {
return 2;
}
}
function putS3IMG($bucket, $file_name, $tmp){ // function uploading to my bucket
//file_name is with directories
//tmp is $_FILES tmp_name
global $client;
$result = $client->putObject([
'Bucket' => $bucket,
'Key' => $file_name,
'SourceFile' => $tmp
]);
if ($result){
return true;
}
return false;
}
旁注:这听起来像是使用AWS Lambda的绝佳选择。您可以将AmazonS3事件配置为在文件上载到AmazonS3时触发AWS Lambda函数。Lambda函数可以从S3下载图像,调整其大小,然后将其上载到S3中的新目标。所有这些都可以无服务器运行,这意味着它是按需运行的。例如,请参阅:@JohnRotenstein谢谢你,伙计!!正是我所需要的,仅供参考,您的两行imagedestroy()
将永远不会运行,因为您在there@phil注意到并改变了。。。