简体   繁体   中英

Can't use getImageSize after calling move_uploaded_file()

I'm trying to create thumbnails after moving the full-size images to a specific folder. The function is working if it's called before if($this->moveImages($rawData, $uniqueID)) but when I put it inside the if statement it just returns the following errors:

Warning: getimagesize(/Applications/MAMP/tmp/php/phpax7wRA): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 66
Warning: Division by zero in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 70
Warning: imagecreatetruecolor(): Invalid image dimensions in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 77 
Warning: imagecreatefromjpeg(/Applications/MAMP/tmp/php/phpax7wRA): failed to open stream: No such file or directory in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 83   
Warning: imagecopyresized() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 91 
Warning: imagejpeg() expects parameter 1 to be resource, boolean given in /Applications/MAMP/htdocs/Projects/Coilerz/classes/Submit.php on line 92

When returning a var_dump on the values passed in it returns what looks like correct data but it seems that it is unable to access it. I will post the functions in question below, thanks for your help.

Submit Function (Called when form is submitted / kicks off functions):

public function submitCoil ($data) {
    $db = Database::getInstance();
    $uniqueID = $this->hash();
    $rawData = $this->interpretData($data, $uniqueID);
    $queryData = $this->interpretData($data, $uniqueID)['queryData'];
    $imageData = $this->interpretData($data, $uniqueID)['imageData'];

    if ($this->nullCheck($data)) {
        mkdir('coils/' . $uniqueID);
        if($this->moveImages($rawData, $uniqueID)) {


            //SUPRESS SLEEP ON UPLOADsleep(5);

            for($i = 0; $i < count($data['image_name']); $i++) {
                $this->createThumbnail($imageData['image_name'][$i], $imageData['image_tmp_name'][$i], $imageData['image_type'][$i], $uniqueID, $i);
            }
            $db->insertFew("coils", array_keys($queryData), array_values($queryData));
            echo "<script>alert('Your coil was uploaded!');</script>";
        } 
    } else {
        echo "Please fill in all fields";
    }       
}

Move full-size images:

private function moveImages ($data, $uniqueID) {
    $queryData = $data['queryData'];
    $data = $data['imageData'];

    $return = true;
    $allowedTypes = array('image/jpg', 'image/png', 'image/jpeg');

    if ($data['image_name'][0] != "") {
        for($i = 0; $i < count($data['image_name']); $i++) {
            if (!in_array($data['image_type'][$i], $allowedTypes)) {
                $return = false;
                echo "<script>alert('Files must be either jpeg or png');</script>";
            } else {
                $ext = pathinfo($data['image_name'][$i], PATHINFO_EXTENSION);
                move_uploaded_file($data['image_tmp_name'][$i], $queryData['images'] . $i . '.' . $ext);
            }
        }
    } else {
        $return = true;
    }

    return $return;
}

Create/move thumbnails function:

public function createThumbnail ($fileName, $tmpName, $fileType, $location, $imageNumber) {
    $name = $fileName;
    $image = $tmpName;
    $file_type = $fileType;


    var_dump($name);
    var_dump($image);

    $image_size = getimagesize($image);
    $image_width = $image_size[0] . "<br>";
    $image_height = $image_size[1];

    $new_size = ($image_width + $image_height) / ($image_width * ($image_height / 1000));
    $new_width = $image_width * $new_size . "<br>";
    $new_height = $image_height * $new_size;


        $allowed = array("image/jpeg", "image/png");
        if(in_array($file_type, $allowed)) {
            $new_image = imagecreatetruecolor($new_width, $new_height);

            //Switch on filetype 

            switch($file_type) {
                case "image/jpeg":
                    $old_image = imagecreatefromjpeg($image);
                break;
                case "image/png":
                    $old_image = imagecreatefrompng($image);
                break;
            }


        imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
        imagejpeg($new_image, 'coils/' . $location . '/' . 'thumb-' . $imageNumber . '.jpg');
    } 
}

my suggestion:

call createThumbnail() before move_uploaded_file() in moveImages()

I think because of move_uploaded file moves and deletes the temporary file you are geeting the error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM