douzi1350 2012-06-17 09:58
浏览 37
已采纳

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

I am using an upload script that works great:

            // 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);

I want to place this script inside a function so I can run it several times, each time using a different $max_width value and thereby create multiple copies of the same image in different sizes without having to duplicate all that code over and over again.

Here is my attempt:

        // 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');

As you can see, all I have done was put the code inside a function, add the global $image variable, change the hard-coded $max_width value to be equals to $target_max_width which is defined at the bottom when I call the function with the necessary parameter.

Instead of seeing a successful upload message I am seeing these symbols all over my screen: enter image description here

Why does my upload script not work when placed inside a function using PHP?

  • 写回答

1条回答 默认 最新

  • douya8978 2012-06-17 10:57
    关注

    Function imagejpeg() accepts 3 parameters $image, $filename and $quality. If the parameter $filename is null or not set, the image stream will be outputted directly to browser.

    So, your problem is that to save image you're using:

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

    But, variables $folder and $newFileName are undefined and passed to function as:

    imagejpeg($new, null, 100);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划