将图片上传到tmp文件夹,调整大小然后上传到S3存储桶时出现问题

时间:2019-06-18 23:28:46

标签: php amazon-web-services amazon-s3 aws-sdk

我已经花了一整天的时间弄清楚如何在tmp中存储图像,更改其大小,然后将其上传到我的S3存储桶中。 S3存储桶工作正常,当我不调整大小时,我能够上传图像。调整大小功能是createThumbnail函数,当未与S3结合使用时可能效果很好,可能是因为格式错误或其他原因。第二个文件“ init.php”包含所有功能。图像被上传到temp文件夹,但是当我运行脚本时,它在调整大小后不会被上传。我没有在本地主机上收到任何错误日志,当我将代码上传到我的AWS实例时,它仅返回内部服务器错误,但是我的实例中的错误日志奇怪地为空...我需要对此问题有新的了解。

banner_image.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));
                        }
                    }
                }
            }
        }
    } 
}

?>

这是我的init.php文件,它包含在banner_image.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来解决了问题,但这不是免费的解决方案。因此,如果您中的任何人提出了一种无需使用第三方工具即可调整大小的解决方案,请随时对其进行注释,以使下一个查看此问题的程序员更加轻松。

0 个答案:

没有答案