5

私はjcropを使用してphpアプリケーションで画像をトリミングしています。以下のコードを使用して、ajaxを使用して座標値と画像パスを渡します。

function checkCoords(index)
    {
            if (parseInt(jQuery('#w').val())){
                    jQuery.ajax({
                        type    : "POST",
                        cache: false,
                        dataType: 'html',
                        data    : {
                                x : jQuery('#x').val(),
                                y : jQuery('#y').val(),
                                w : jQuery('#w').val(),
                                h : jQuery('#h').val(),
                       image_path : jQuery('#jc-hidden-image'+index).attr('src')
                        },
                        url     : BASE_URL+'apps/configure/cropimage',
                        success : function(response) { 
                                jQuery(".preview_crop").html(response);
                        }
                    });                     
            } 
            else{
                alert('Please select a crop region then press Crop button.');
            }

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応答でトリミングされた画像を取得する必要があります。私はこれで何を間違えましたか?

4

1 に答える 1

8

完全な画像データを応答として送り返す代わりに、画像をサーバーに保存し、その 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 + '" />');
}
于 2012-06-09T09:46:41.660 に答える