通过POST发送的图像(base64格式),如果超过1.5 MB,则永远不会到达服务器。 Canvas resize也没有帮助

时间:2016-06-18 15:25:38

标签: javascript php canvas http-post joomla3.0

我尝试加载,并通过ajax(post)将图像发送到服务器。如果文件大于1MB,则php JRequest :: getVar(' filedata')将返回空。

我尝试在发送之前使用画布调整图像大小,但仍然没有运气。

如果有人可以提出任何解决方案(顺便说一下,这是一个joomla网站,那么我会很高兴。)

的javascript:

<ul class="adminformlist" id="uploading">
    <li><input type="file" name="attachment" id="attachment" multiple /><img id="spinner"
        style="display: none; margin-left: 10px;"
        src="<?php echo JURI::root(); ?>components/com_bt_property/assets/img/spinner.gif">
        <div style="clear: both"></div>
        <div id="btss-message"></div></li>
</ul>
<img src="" id="image">
<script type="text/javascript">
(function($){
            var files = [];
            $("#attachment").change(function(event) {
            $.each(event.target.files, function(index, file) {

             // Create an image
             var img = document.createElement("img");

              var reader = new FileReader();
              reader.onload = function(event) {

                 img.src = event.target.result;


                var canvas = document.createElement("canvas");
                //var canvas = $("<canvas>", {"id":"testing"})[0];
                var ctx = canvas.getContext("2d");
                ctx.drawImage(img, 0, 0);

                var MAX_WIDTH = 400;
                var MAX_HEIGHT = 300;
                var width = img.width;
                var height = img.height;

                if (width > height) {
                  if (width > MAX_WIDTH) {
                    height *= MAX_WIDTH / width;
                    width = MAX_WIDTH;
                  }
                } else {
                  if (height > MAX_HEIGHT) {
                    width *= MAX_HEIGHT / height;
                    height = MAX_HEIGHT;
                  }
                }
                canvas.width = width;
                canvas.height = height;
                var ctx = canvas.getContext("2d");
                ctx.drawImage(img, 0, 0, width, height);

                var dataurl = canvas.toDataURL("image/png", 0.2);
                document.getElementById('image').src = dataurl; 



                object = {};
                object.filename = file.name;
                object.data = event.target.result;
                files.push(object);
                if(files.length==1){
                    uploadFile(index);
                    $('#spinner').show();
                    $("#btss-message").show();
                }

              };  
              reader.readAsDataURL(file);
            });
          });

          function uploadFile(index){
              $.ajax({url: "index.php?option=com_bt_property&task=properties.upload",
                    type: 'POST',
                    data: {filename: files[index].filename, filedata: files[index].data},
                    success: function(response, status, xhr){
                        //console.log("resp: " + response + " " + status + " " + xhr);
                        uploadHandler(response, status, xhr);
                        if(index == files.length-1){
                            $('#spinner').hide();
                            files = [];
                            $("#attachment").val('');
                            $("#btss-message").delay(1000).slideUp(function(){
                                $("#btss-message").html('');
                            });
                        }else{
                            index++;
                            uploadFile(index);

                        }
                    }
              });
          }

Php上传:

// upload function
    function upload($fe = false) {
        $db = JFactory::getDBO();
        $id = JRequest::getInt('id');
        $query = "SELECT params from #__bt_properties where id=$id";
        $db->setQuery($query);
        $params = $db->loadResult();

        if ($params) {
            $registry = new JRegistry();
            $registry->loadString($params);
            $this->overrideConfigs($registry);
        }
        $pid = 'tmp';
        $allowedExtensions = array('jpg', 'jpeg', 'JPG', 'png', 'gif');

        $this->prepareFolders($pid);

        $validated = true;
        $objFile = new stdClass();

        /****
        if ($fe) {
            $file = $_FILES["Filedata"];
            $max_upload = (int) (ini_get('upload_max_filesize'));
            $max_post = (int) (ini_get('post_max_size'));
            $memory_limit = (int) (ini_get('memory_limit'));
            $sv_uploadAllow = min($max_upload, $max_post, $memory_limit);
            $fe_uploadAllow = $this->params->get('fe_file_max_size', 0);
            $size = $file['size'] / (2048 * 2048);
            if ($fe_uploadAllow > 0 && $fe_uploadAllow <= $sv_uploadAllow) {
                if ($size > $fe_uploadAllow) {
                    $result['message'] = 'File is too large. Allow file less than ' . $fe_uploadAllow . ' MB';
                    $validated = false;
                }
            }
            if ($fe_uploadAllow > 0 && $fe_uploadAllow > $sv_uploadAllow) {
                if ($size > $sv_uploadAllow) {
                    $result['message'] = 'File is too large. Server allow file less than ' . $sv_uploadAllow . ' MB';
                    $validated = false;
                }
            }
        }

        *****/

        $filename = JRequest::getVar('filename');
        $filedata = JRequest::getVar('filedata');   


        list($type, $filedata) = explode(';', $filedata);
        list(, $filedata) = explode(',', $filedata);

        $filedata = base64_decode($filedata);

        list(, $imageExt) = explode('/', $type);

        $imageName = strtotime("now").'-'. $filename;
        $path_image = $this->images_path . $pid . '/';

        if (in_array($imageExt, $allowedExtensions)) {

            if (!file_put_contents($path_image . 'original-' . $imageName, $filedata)) {
                $result['message'] = 'Could not save!';
                $validated = false;
            } else {

                BTImageHelper::resize($path_image . 'original-' . $imageName, $path_image . 'large-' . $imageName, $this->crop_width, $this->crop_height, $this->largeimgprocess, $this->crop_pos,
                        $this->jpeg_com);

                BTImageHelper::resize($path_image . 'original-' . $imageName, $path_image . 'thumb-' . $imageName, $this->thumb_width, $this->thumb_height, $this->thumbimgprocess, $this->crop_pos,
                        $this->jpeg_com);
                BTImageHelper::resize($path_image . 'original-' . $imageName, $path_image . 'ssthumb-' . $imageName, $this->ssthumb_width, $this->ssthumb_height, $this->thumbimgprocess,
                        $this->crop_pos, $this->jpeg_com);

                $objFile->filename = $imageName;
                $objFile->title = $filename;
            }
        } else {            
            $result['message'] = 'File extension invalid!';
            $validated = false;
        }
        if ($validated) {
            $result["success"] = true;
            $result["files"] = $objFile;
        }
        $obLevel = ob_get_level();
        while ($obLevel > 0) {
            ob_end_clean();
            $obLevel--;
        }
        echo json_encode($result);
    }

0 个答案:

没有答案