getimagesize()函数无法使用linux

时间:2016-08-03 01:48:46

标签: php linux gd photo

我有这个功能

public static function upload($path_folder, $input_file_name) {

    $photo_file = $_FILES[$input_file_name]; // FILE NAME
    $photo_name = $photo_file['name']; // PHOTO NAME
    $photo_tmp_name = $photo_file['tmp_name'];  // TMP NAME
    $photo_extension = pathinfo($photo_name, PATHINFO_EXTENSION); // EXTENSION
    // soon GIFs

    $rand_num = random_int(1, 3);
    switch ($rand_num) {
        case 1:
            $hash = mb_strtoupper(Hash::create(), 'UTF-8');
        break;
        case 2:
            $hash = mb_strtolower(Hash::create(), 'UTF-8');
        break;
        case 3:
            $hash = str_shuffle(Hash::create());
        break;
        default:
            $hash = Hash::create();
        break;
    }

    $photo_with_extension = $hash . '.' . $photo_extension; // NEW FILE NAME
    $factory_destination = 'photos/factory/' . $photo_with_extension; // FACTORY

    if(move_uploaded_file($photo_tmp_name, $factory_destination)) {
    if(file_exists($factory_destination)) {
    if(
        ($photo_extension === 'jpg') ||
        ($photo_extension === 'jpeg') ||
        ($photo_extension === 'JPG') ||
        ($photo_extension === 'JPEG') ||
        ($photo_extension === 'png') ||
        ($photo_extension === 'PNG')
    ) {

        if(
            ($photo_extension === 'jpg') ||
            ($photo_extension === 'jpeg') ||
            ($photo_extension === 'JPG') ||
            ($photo_extension === 'JPEG')
        ) {
            if(imagecreatefromjpeg($factory_destination)) { // JPEG
                $photo_create = imagecreatefromjpeg($factory_destination);
            }
        }

        if(
            ($photo_extension === 'png') ||
            ($photo_extension === 'PNG')
        ) {
            if(imagecreatefrompng($factory_destination)) { // PNG
                $photo_create = imagecreatefrompng($factory_destination);
            }
        }

        if(getimagesize($photo_tmp_name)) {

            list($photo_width, $photo_height) = getimagesize($photo_tmp_name); // WIDTH & HEIGHT
            $photo_new_width = 300; // NEW WIDTH
            $photo_new_height = 300;
            //$photo_new_height = ($photo_height / $photo_width) * $photo_new_width; // NEW HEIGHT
            $true_color = imagecreatetruecolor($photo_new_width, $photo_new_height); // TRUE COLOR

            if(imagecopyresampled($true_color, $photo_create, 0, 0, 0, 0, $photo_new_width, $photo_new_height, $photo_width, $photo_height)) {


                $photo_copy = imagecopyresampled($true_color, $photo_create, 0, 0, 0, 0, $photo_new_width, $photo_new_height, $photo_width, $photo_height); // COPY
                    $mime = mime_content_type($factory_destination);

                    if(($mime === 'image/jpeg') || ($mime === 'image/png')) {

                        switch ($path_folder) {
                            case 1:
                                $folder = 'profile';
                            break;
                            case 2:
                                $folder = 'identities';
                            break;

                            default:
                                $folder = 'errors';
                            break;
                        }

                        $new_destination = 'photos/' . $folder . '/';
                        //$image = imagecreatefromstring(file_get_contents($photo_tmp_name));
                        $exif = exif_read_data($factory_destination);
                        if(!empty($exif['Orientation'])) {
                            switch($exif['Orientation']) {
                                case 8:
                                    $true_color = imagerotate($true_color,90,0);
                                    break;
                                case 3:
                                    $true_color = imagerotate($true_color,180,0);
                                    break;
                                case 6:
                                    $true_color = imagerotate($true_color,-90,0);
                                    break;
                            }
                        }
                        if(imagejpeg($true_color, $new_destination . $photo_with_extension, 75)) { // DESIGNATED PATHS
                            imagedestroy($true_color); // return 

                            if(unlink($factory_destination)) {
                                return $photo_with_extension; // AUTO UPDATE ADMIN THAT THERE IS A PROBLEM
                            } else { return 'unlink last'; }        
                        } else { return 'imagejpeg'; }
                    } else { return 'mime'; }
                /*move*/ 
            } else { return 'resampled'; }
        } else { return 'get image:' . $photo_tmp_name; }
    } else { return 'wrong ext'; }
    } else { return 'default2.png'; }
    } else { return 'move'; }
    return empty($photo_with_extension) ? '' : 'false';
}

在没有任何事情发生之后,该函数最多可以move_uploaded_file()

这是在生产,GD启用和兼容的Linux服务器上。您认为我的代码出错了哪里?这虽然在我的localhost上完全正常。

准确地说,imagejpeg()是无效的

新信息;

问题似乎发生在getimagesize($photo_tmp_name)上,它返回false

较新信息:

tmp_name上给出的getimagesize()在我回复时是正确的,但它返回false。

1 个答案:

答案 0 :(得分:0)

如果在move_uploaded_file() func之后,它应该销毁tmp_file,你可以用已经移动的文件替换tmp_file,因为php将执行阻止的进程,这样就安全了要做!