调用move_uploaded_file()后无法使用getImageSize

时间:2015-11-24 12:26:11

标签: php oop file-upload image-resizing

我正在尝试将全尺寸图像移动到特定文件夹后创建缩略图。如果在if($this->moveImages($rawData, $uniqueID))之前调用它,该函数正在工作,但是当我将它放在if语句中时,它只返回以下错误:

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

在传递的值上返回var_dump时,返回的内容看起来像是正确的数据,但似乎无法访问它。我将在下面发布有问题的功能,感谢您的帮助。

提交功能(在提交表单时启动/启动功能):

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

移动全尺寸图片:

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

创建/移动缩略图功能:

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

1 个答案:

答案 0 :(得分:0)

我的建议:

在moveImages()

中的move_uploaded_file()之前调用createThumbnail()

我认为因为move_uploaded文件会移动并删除您正在修改错误的临时文件