3

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

            // 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 放置在函数中时不起作用?

4

1 回答 1

1

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

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

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

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

imagejpeg($new, null, 100);
于 2012-06-17T10:57:11.273 回答