我正在尝试使用jquery裁剪图像...我正在为一个图像获得正确的裁剪图像但是没有获得第二个图像...这里是我关注的代码
$("#cropimage").click(function() {
var img_full_div_top = parseInt($(".full_single_image").position().top);
var img_full_div_left = parseInt($(".full_single_image").position().left);
var crop_tool_div_left = parseInt($("#crop_tool").position().left);
var crop_tool_div_top = parseInt($("#crop_tool").position().top);
img_full_div_top.toFixed();
img_full_div_left.toFixed();
crop_tool_div_left.toFixed();
crop_tool_div_top.toFixed();
var crop_start_x = crop_tool_div_left-img_full_div_left;
var crop_start_y = crop_tool_div_top-img_full_div_top;
var crop_tool_div_width = parseInt($("#crop_tool").width());
var crop_tool_div_height = parseInt($("#crop_tool").height());
crop_tool_div_width.toFixed();
crop_tool_div_height.toFixed();
var img_name = '<?php echo $name;?>';
$.ajax({
url: '<?php echo Yii::app()->createUrl('upload/cropImage'); ?>',
type: 'POST',
data: {crop_start_x:crop_start_x,crop_start_y:crop_start_y,crop_tool_div_width:crop_tool_div_width,crop_tool_div_height:crop_tool_div_height,img_name:img_name },
success : function(data){
//window.parent.location.href = window.parent.location;
}
});
});
上面的代码用于获取坐标和图像的高度和宽度 html代码如下
<div class="full_single_image">
<img src="<?php echo Yii::app()->request->baseUrl; ?>/directory/uploads/<?php echo $name; ?> " id="size_of_image">
<div id="crop_tool" style="display: block;"><div class="dragimageTo"><p> Drag or Resize Image</p></div></div>
</div>
<button type="button" id="cropimage" img_name="hi">Crop</button>
点击按钮,它将转到上面的功能
和php中的图像保存代码如下
$src_x = $_POST['crop_start_x'];
$src_y = $_POST['crop_start_y'];
$height = $_POST['crop_tool_div_height'];
$width = $_POST['crop_tool_div_width'];
$img_name = $_POST['img_name'];
$location = './directory/uploads/'.$img_name;
$target = './directory/cropped/'.$img_name;
$src_w = $width + $src_x;
$src_h = $height + $src_y;
$newImage = imagecreatetruecolor($width,$height);
$source=imagecreatefromjpeg($location);
imagecopyresampled($newImage,$source,0,0,$src_x,$src_y,$width,$height,$src_w,$src_h);
imagejpeg($newImage,$target);
答案 0 :(得分:0)
抱歉,我终于得到了答案
上传的文件大小必须等于裁剪图片
$source=imagecreatefromjpeg($location);
imagecopyresampled($newImage,$source,0,0,$src_x,$src_y,$width,$height,$src_w,$src_h);
$source != $src_w , $src_h