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

时间:2012-06-17 09:58:30

标签: php function upload

我正在使用效果很好的上传脚本:

            // 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,当我调用时,它定义在底部具有必要参数的函数。

我没有看到成功的上传消息,而是在屏幕上看到这些符号: enter image description here

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

1 个答案:

答案 0 :(得分:1)

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

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

imagejpeg($new, $folder.$newFileName, 100);

但是,变量$folder$newFileName未定义并传递给函数:

imagejpeg($new, null, 100);