0

我正在使用 jquery 以下列方式调用我的 php 文件。

$.ajax({ url: '/my/site',
         data: {action: 'test'},
         type: 'post',
         success: function(output) {
                      alert(output);
                  }
});

在服务器端动作 POST 参数正在被读取,并根据命令模式调用相应的方法

if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    switch($action) {
        case 'test' :uploadImageFile();break;
    }
}

我的测试方法是假设返回一个图像,但它没有返回任何东西,任何人都可以建议我我的程序有什么问题

function uploadImageFile() { 

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        /*Configuration part*/
        extract($_POST);
        $desired_width = 200; //desired image result width
        $desired_height = 200; //desired image result height
        $img_quality = 100;
        $upload_dir = 'upload/';

        if ($_FILES) {
            $file = $_FILES['image_file'];
            if (! $file['error'] && $file['size'] < 350 * 1024) {
                if (is_uploaded_file($file['tmp_name'])) {

                    // unique name of the file
                    $temp_file = $upload_dir. md5(time().rand(0,2000));

                    // upload the file in appropriate folder
                    move_uploaded_file($file['tmp_name'], $temp_file);

                    @chmod($temp_file, 0644);

                    //Again check if the file was uploaded properly without any error
                    if (file_exists($temp_file) && filesize($temp_file) > 0) {
                        $file_size_arr = getimagesize($temp_file); // get the image detail
                        if (!$file_size_arr) {
                            @unlink($temp_file); //if file size array not exits then delete it
                            return;
                        }
                        $mime = $file_size_arr['mime'];
                        $virtual_img_arr = get_virtual_img($mime,$temp_file);
                        $virtual_img = $virtual_img_arr['img'];
                        $virtual_img_ext = $virtual_img_arr['ext'];
                        // create a new true color image
                        $true_color_img = @imagecreatetruecolor( $desired_width, $desired_height );

                        // copy and resize part of an image with resampling
                        imagecopyresampled($true_color_img, $virtual_img, 0, 0, (int)$x1, (int)$y1, $desired_width, $desired_height, (int)$w, (int)$h);

                        // define a result image filename
                        $result = $temp_file . $virtual_img_ext;

                        // upload resultant file to the folder
                        imagejpeg($true_color_img, $result, $img_quality);
                        @unlink($temp_file); //delete the main temporary uploaded file

                        return $result;
                    }
                }
            }
        }
    }
}
4

2 回答 2

1

您必须实际echo返回值:

switch ($action) {
    case 'test':
        echo uploadImageFile();
    break;
}
于 2013-06-05T02:39:39.980 回答
1
switch ($action) {
    case 'test':
        echo uploadImageFile();
    break;
}

您必须使用echo,否则返回 null

于 2013-06-05T06:32:55.307 回答