为什么我的上传脚本在使用PHP的函数中不起作用?

为什么我的上传脚本在使用PHP的函数中不起作用?,php,function,upload,Php,Function,Upload,我使用的上传脚本非常有效: // Create image from file switch(strtolower($_FILES['fileField']['type'])) { case 'image/jpeg': $image = imagecreatefromjpeg($_FILES['fileField']['tmp_name']);

我使用的上传脚本非常有效:

            // Create image from file
            switch(strtolower($_FILES['fileField']['type'])) {
                case 'image/jpeg':
                    $image = imagecreatefromjpeg($_FILES['fileField']['tmp_name']);
                    break;
                case 'image/png':
                    $image = imagecreatefrompng($_FILES['fileField']['tmp_name']);
                    break;
                case 'image/gif':
                    $image = imagecreatefromgif($_FILES['fileField']['tmp_name']);
                    break;
                default:
                    exit('Unsupported type: '.$_FILES['fileField']['type']);
            }

            // Get current dimensions
            $old_width  = imagesx($image);
            $old_height = imagesy($image);

            // Target dimensions for large version
            $max_width = '600';

            if($max_width > $old_width) {
                $max_width = $old_width;
            }

            $max_height = ($old_height/$old_width)* $max_width;


            // Get current dimensions
            $old_width  = imagesx($image);
            $old_height = imagesy($image);

            // Calculate the scaling we need to do to fit the image inside our frame
            $scale = min($max_width/$old_width, $max_height/$old_height);

            // Get the new dimensions
            $new_width  = ceil($scale*$old_width);
            $new_height = ceil($scale*$old_height);


            // Create new empty image
            $new = imagecreatetruecolor($new_width, $new_height);

            // Resize old image into new
            imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);

            //Output the image to a file
            imagejpeg($new, $folder.$newFileName,100);

            // Destroy resources
            imagedestroy($image);
            imagedestroy($new);
我想把这个脚本放在一个函数中,这样我就可以运行它几次,每次都使用不同的$max_width值,从而以不同的大小创建同一图像的多个副本,而不必重复所有代码

以下是我的尝试:

        // Create image from file
        switch(strtolower($_FILES['fileField']['type'])) {
            case 'image/jpeg':
                $image = imagecreatefromjpeg($_FILES['fileField']['tmp_name']);
                break;
            case 'image/png':
                $image = imagecreatefrompng($_FILES['fileField']['tmp_name']);
                break;
            case 'image/gif':
                $image = imagecreatefromgif($_FILES['fileField']['tmp_name']);
                break;
            default:
                exit('Unsupported type: '.$_FILES['fileField']['type']);
        }


        function resizeAndPlaceFile($target_max_width) {

            global $image;

            // Get current dimensions
            $old_width  = imagesx($image);
            $old_height = imagesy($image);

            // Target dimensions for large version
            $max_width = $target_max_width;

            if($max_width > $old_width) {
                $max_width = $old_width;
            }

            $max_height = ($old_height/$old_width)* $max_width;


            // Get current dimensions
            $old_width  = imagesx($image);
            $old_height = imagesy($image);

            // Calculate the scaling we need to do to fit the image inside our frame
            $scale = min($max_width/$old_width, $max_height/$old_height);

            // Get the new dimensions
            $new_width  = ceil($scale*$old_width);
            $new_height = ceil($scale*$old_height);


            // Create new empty image
            $new = imagecreatetruecolor($new_width, $new_height);

            // Resize old image into new
            imagecopyresampled($new, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);

            //Output the image to a file
            imagejpeg($new, $folder.$newFileName,100);

            // Destroy resources
            imagedestroy($image);
            imagedestroy($new);
        }

        resizeAndPlaceFile('600');
如您所见,我所做的只是将代码放入一个函数中,添加全局$image变量,将硬编码的$max_width值更改为等于$target_max_width,当我使用必要的参数调用函数时,它在底部定义

我在屏幕上看到的不是成功上传的消息,而是这些符号:

为什么我的上传脚本在使用PHP放入函数时不起作用?

函数
imagejpeg()
接受3个参数
$image
$filename
$quality
。如果参数
$filename
null
或未设置,图像流将直接输出到浏览器

所以,您的问题是要保存您正在使用的图像:

imagejpeg($new, $folder.$newFileName, 100);
但是,变量
$folder
$newFileName
未定义并传递给函数:

imagejpeg($new, null, 100);

谢谢你的回答!在我在函数中添加了global$文件夹和global$newFileName之后,它工作得非常好!