gpt4 book ai didi

php - 我如何使用 jquery ajax 获取图像?

转载 作者:可可西里 更新时间:2023-11-01 13:50:55 25 4
gpt4 key购买 nike

我正在使用 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 响应中获取裁剪图像。我做错了什么?

最佳答案

您正在将完整的图像数据作为响应发回,而不是将图像保存在服务器上并将 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 + '" />');
}

关于php - 我如何使用 jquery ajax 获取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10959826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com