我有这个功能

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服务器。你认为我的密码哪里出错了?不过,这在我的本地主机上运行得非常好。
准确地说,imagejpeg()是不起作用的
新信息;
问题似乎发生在getimagesize($photo_tmp_name)返回false
更新信息:
当我回显时,tmp_name上给出的getimagesize()是正确的,但它返回false。

最佳答案

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

关于php - getimagesize()函数不适用于linux,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38733051/

10-10 07:16