Php 如何在Laravel控制器中的函数中使用cal函数

Php 如何在Laravel控制器中的函数中使用cal函数,php,jquery,laravel,laravel-4,laravel-routing,Php,Jquery,Laravel,Laravel 4,Laravel Routing,如何在Laravel控制器中的函数中使用cal函数 我得到的错误如下所示 message: "Call to undefined function createThumb()" 我认为错误是在jquery\u寄存器函数中调用CreateTumb时发生的 这是我的路线: Route::post('up', 'UploadController@jquery_register'); 以下是我的看法: <!doctype html> <html> <head> &

如何在Laravel控制器中的函数中使用cal函数

我得到的错误如下所示

message: "Call to undefined function createThumb()"
我认为错误是在jquery\u寄存器函数中调用CreateTumb时发生的

这是我的路线:

Route::post('up', 'UploadController@jquery_register');
以下是我的看法:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Image upload and generate thumbnail using ajax in PHP</title>
<link href="assets/temp/css/style.css" rel="stylesheet">
<script src="assets/temp/js/jquery.min.js"></script>
<script src="assets/temp/js/jquery.form.js"></script>
<script>
$(document).on('change', '#image_upload_file', function () {
var progressBar = $('.progressBar'), bar = $('.progressBar .bar'), percent = $('.progressBar .percent');

$('#image_upload_form').ajaxForm({
    beforeSend: function() {
        progressBar.fadeIn();
        var percentVal = '0%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    uploadProgress: function(event, position, total, percentComplete) {
        var percentVal = percentComplete + '%';
        bar.width(percentVal)
        percent.html(percentVal);
    },
    success: function(html, statusText, xhr, $form) {       
        obj = $.parseJSON(html);    
        if(obj.status){     
            var percentVal = '100%';
            bar.width(percentVal)
            percent.html(percentVal);
            $("#imgArea>img").prop('src',obj.image_medium);         
        }else{

      alert(obj.error);
        }
    },
    complete: function(xhr) {
        progressBar.fadeOut();          
    }   
}).submit();        

});
</script>
</head>

<body>



<br>


<div id="imgContainer">
  <form enctype="multipart/form-data" action="up" method="post" name="image_upload_form" id="image_upload_form">
    <div id="imgArea"><img src="assets/temp/img/default.jpg">
      <div class="progressBar">
        <div class="bar"></div>
        <div class="percent">0%</div>
      </div>
      <div id="imgChange"><span>Change Photo</span>
        <input type="file" accept="image/*" name="image_upload_file" id="image_upload_file">
      </div>
    </div>
  </form>
</div>
</body>
</html>

在PHP中使用ajax上传图像并生成缩略图
$(document).on('change','#image_upload_file',函数(){
var progressBar=$('.progressBar')、bar=$('.progressBar.bar')、percent=$('.progressBar.percent');
$(“#图像_上传_表单”).ajaxForm({
beforeSend:function(){
progressBar.fadeIn();
var percentVal='0%';
条形宽度(百分比值)
html(percentVal);
},
上载进度:功能(事件、位置、总数、完成百分比){
var percentVal=percentComplete+“%”;
条形宽度(百分比值)
html(percentVal);
},
成功:函数(html、statusText、xhr、$form){
obj=$.parseJSON(html);
如果(目标状态){
var percentVal=‘100%’;
条形宽度(百分比值)
html(percentVal);
$(“#imgArea>img”).prop('src',obj.image_medium);
}否则{
警报(对象错误);
}
},
完成:函数(xhr){
progressBar.fadeOut();
}   
}).submit();
});

0% 换照片
这是我的控制器:

<?php

class UploadController extends BaseController {

    //start of jquery register



    public function jquery_register()
    {   

ini_set("memory_limit", "99M");
ini_set('post_max_size', '20M');
ini_set('max_execution_time', 600);
define('IMAGE_SMALL_DIR', '');
define('IMAGE_SMALL_SIZE', 50);
define('IMAGE_MEDIUM_DIR', '');
define('IMAGE_MEDIUM_SIZE', 250);

if(isset($_FILES['image_upload_file'])){
    $output['status']=FALSE;
    set_time_limit(0);
    $allowedImageType = array("image/gif",   "image/jpeg",   "image/pjpeg",   "image/png",   "image/x-png"  );

    if ($_FILES['image_upload_file']["error"] > 0) {
        $output['error']= "Error in File";
    }
    elseif (!in_array($_FILES['image_upload_file']["type"], $allowedImageType)) {
        $output['error']= "You can only upload JPG, PNG and GIF file";
    }
    elseif (round($_FILES['image_upload_file']["size"] / 1024) > 4096) {
        $output['error']= "You can upload file size up to 4 MB";
    } else {
        /*create directory with 777 permission if not exist - start*/
        /*create directory with 777 permission if not exist - end*/
        $path[0] = $_FILES['image_upload_file']['tmp_name'];
        $file = pathinfo($_FILES['image_upload_file']['name']);
        $fileType = $file["extension"];
        $desiredExt='jpg';
        $fileNameNew = rand(333, 999) . time() . ".$desiredExt";
        $path[1] = IMAGE_MEDIUM_DIR . $fileNameNew;
        $path[2] = IMAGE_SMALL_DIR . $fileNameNew;


        $output['status']=TRUE;
                $output['image_medium']= $path[1];

        if (createThumb($path[0], $path[1], $fileType, IMAGE_MEDIUM_SIZE, IMAGE_MEDIUM_SIZE,IMAGE_MEDIUM_SIZE)) {

            if (createThumb($path[1], $path[2],"$desiredExt", IMAGE_SMALL_SIZE, IMAGE_SMALL_SIZE,IMAGE_SMALL_SIZE)) {
                $output['status']=TRUE;
                $output['image_medium']= $path[1];
                $output['image_small']= $path[2];
            }
        }
    }
    echo json_encode($output);
}





        }


    public  function createThumb($path1, $path2, $file_type, $new_w, $new_h, $squareSize = '')
{

    /* read the source image */
    $source_image = FALSE;

    if (preg_match("/jpg|JPG|jpeg|JPEG/", $file_type)) {
        $source_image = imagecreatefromjpeg($path1);
    }
    elseif (preg_match("/png|PNG/", $file_type)) {

        if (!$source_image = @imagecreatefrompng($path1)) {
            $source_image = imagecreatefromjpeg($path1);
        }
    }
    elseif (preg_match("/gif|GIF/", $file_type)) {
        $source_image = imagecreatefromgif($path1);
    }       
    if ($source_image == FALSE) {
        $source_image = imagecreatefromjpeg($path1);
    }

    $orig_w = imageSX($source_image);
    $orig_h = imageSY($source_image);

    if ($orig_w < $new_w && $orig_h < $new_h) {
        $desired_width = $orig_w;
        $desired_height = $orig_h;
    } else {
        $scale = min($new_w / $orig_w, $new_h / $orig_h);
        $desired_width = ceil($scale * $orig_w);
        $desired_height = ceil($scale * $orig_h);
    }

    if ($squareSize != '') {
        $desired_width = $desired_height = $squareSize;
    }

    /* create a new, "virtual" image */
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);
    // for PNG background white----------->
    $kek = imagecolorallocate($virtual_image, 255, 255, 255);
    imagefill($virtual_image, 0, 0, $kek);

    if ($squareSize == '') {
        /* copy source image at a resized size */
        imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $orig_w, $orig_h);
    } else {
        $wm = $orig_w / $squareSize;
        $hm = $orig_h / $squareSize;
        $h_height = $squareSize / 2;
        $w_height = $squareSize / 2;

        if ($orig_w > $orig_h) {
            $adjusted_width = $orig_w / $hm;
            $half_width = $adjusted_width / 2;
            $int_width = $half_width - $w_height;
            imagecopyresampled($virtual_image, $source_image, -$int_width, 0, 0, 0, $adjusted_width, $squareSize, $orig_w, $orig_h);
        }

        elseif (($orig_w <= $orig_h)) {
            $adjusted_height = $orig_h / $wm;
            $half_height = $adjusted_height / 2;
            imagecopyresampled($virtual_image, $source_image, 0,0, 0, 0, $squareSize, $adjusted_height, $orig_w, $orig_h);
        } else {
            imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $squareSize, $squareSize, $orig_w, $orig_h);
        }
    }

    if (@imagejpeg($virtual_image, $path2, 90)) {
        imagedestroy($virtual_image);
        imagedestroy($source_image);
        return TRUE;
    } else {
        return FALSE;
    }
}   

    }  // end of the controller

要在同一类内调用方法,应使用$this变量

class Test {
     public function main() {
         bee(); // won't work as this will try to find the method "bee" in \ namespace
         $q = strpos("test", "e"); // will work as "strpos" method is in the \ namespace
         $this->bee(); // will work as "bee" method is present in the same class
         $q = $this->strpos("test", "e"); // will fail as "strpos" method does not exist in class
     }

     public function ae(){
         echo "a";
     }

     public function bee(){
          echo "b";
     }
}
在您的情况下,解决方案只是将“CreateTumb”()替换为:


希望有帮助。

使用$this->createThumb(…在这里-->if(createThumb($p,…?是的,要从同一个类中的方法调用同一个类中的方法,你应该使用$this->lol。我认为这是一个快速的ans,所以我在这里给出了它。无论如何,我会写一个答案等待它并标记它:plol在提出类似的问题之前,你应该正确地学习php基础知识。当然,如果你来这里问问题,人们会投反对票帮助解决你犯的小错误不管怎样,只要你投了我的票,我就投你的票(mods请原谅我:p)谢谢,我会删除问答中的评论,因为这是有意义的
$this->createThumb(