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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 脱敏项目合作,ner需求合作
  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴