php - 将图像上传到 tmp 文件夹时出现问题,调整大小然后上传到 S3 存储桶
问题描述
我花了一整天的时间研究如何将图像存储在 tmp 中,更改其大小,然后将其上传到我的 S3 存储桶。S3 存储桶工作正常,我可以在不调整大小时上传图片。resize 函数是 createThumbnail 函数,在不与 S3 结合使用时可以正常工作,可能是因为格式错误或其他原因。第二个文件“init.php”包含所有函数。图像被上传到临时文件夹,但是当我运行脚本时,它在调整大小后不会被上传。我在本地主机上没有收到任何错误日志,当我将代码上传到我的 AWS 实例时,它只会返回一个内部服务器错误,但我的实例中的错误日志却是奇怪的空......我需要对这个问题有一些新的看法。
横幅图像.php
<?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,但它不是免费的解决方案。因此,如果你们中的任何人提出了一个解决方案,允许您在不使用第三方工具的情况下调整大小,请随时发表评论,以便让下一个查看此问题的程序员的生活更轻松。
解决方案
推荐阅读
- indexeddb - Dexie.js:通过动态 id 更新嵌套对象
- flutter - Vscode 不显示 chrome 作为设备
- c# - 我正在制作炮塔,但它不会射击
- github - 如何在 .rmd 中打印日期?
- ios - iOS:将音频修改应用于音乐库内容
- function - PytelegramAPI 如何将机器人导入另一个文件
- excel - 确定 Excel 列是否有任何值?
- list - 创建包含另一种数据类型的特定值的数据类型
- python - 多个列表的 Itertools 组合,每个列表选择 n 个元素
- node.js - 使用 mysql2.ping 检查服务器是否启动或关闭