Php 如何使用jQueryAjax获取图像?

Php 如何使用jQueryAjax获取图像?,php,jquery,ajax,jcrop,Php,Jquery,Ajax,Jcrop,我在php应用程序中使用jcrop裁剪图像。我使用下面的代码使用ajax传递坐标值和图像路径 在Controller中,我使用ajax值,如下所示 public function cropimageAction(){ $params = $this->getRequest()->getParams(); //d($params); if ($_SERVER['REQUEST_METHOD'] == 'POST') {

我在php应用程序中使用jcrop裁剪图像。我使用下面的代码使用ajax传递坐标值和图像路径

在Controller中,我使用ajax值,如下所示

  public function cropimageAction(){
        $params = $this->getRequest()->getParams();
        //d($params);
        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
                $targ_w = $targ_h = 150;
                $jpeg_quality = 90;

                $src = $params['image_path'];
                $img_r = imagecreatefromjpeg($src);
                $dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

                $image  = imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],$targ_w,$targ_h,$_POST['w'],$_POST['h']);

               header('Content-type: image/jpeg');
                imagejpeg($dst_r,null,$jpeg_quality);

                exit;
        }        
    }
我得到的回答是

��(��(��(��(��


得到了一些符号,而不是剪切图像。需要在ajax响应中获得剪切图像。我在这方面做错了什么?

您将完整图像数据作为响应发送回,而不是将图像保存在服务器上,并将URL作为响应发送给它

反而

header('Content-type: image/jpeg');
imagejpeg($dst_r,null,$jpeg_quality);
有这个吗

imagejpeg($dst_r,"path/where/to/save/image.jpg",$jpeg_quality);
echo "path/where/to/save/image.jpg";
此外,您的成功函数应该如下所示

success : function(url) { 
    jQuery(".preview_crop").html('<img src="' + url + '" />');
}
成功:函数(url){
jQuery(“.preview\u crop”).html(“”);
}
success : function(url) { 
    jQuery(".preview_crop").html('<img src="' + url + '" />');
}