Issue when uploading image to tmp folder, resize and then upload to S3 bucket

Viewed 3238

I've spend all day figuring out how to store an image in tmp, change its size and then upload it to my S3 bucket. The S3 bucket works fine and i am able to upload images when i don't resize. The resize function is the createThumbnail function and works fine when not combined with S3 probably because it's a wrong format or something. The second file "init.php" contains all the functions. The images are uploaded to the temp folder but when i run the script it doesn't get uploaded after it's resized. I don't get any error logs on localhost and when i upload the code to my AWS instance it just returns a internal server error but the error log in my instance is oddly empty... I need some fresh eyes on this issue.

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

?>

here is my init.php file included in the banner_image.php file which contains my functions.

     <?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;
    }

SOLUTION:okay, so eventually i ended up using AWS lambda which solved the issue but it isn't a free solution. So, if any of you come up with a solution that allow you to resize without using third party tools feel free to comment it in order to make life easier for the next programmers viewing this question.

0 Answers
Related