[英]Php image cropping
I'm trying to crop an image using the 'jcrop' library, and after sending coords to server in ajax. 我正在尝试使用'jcrop'库裁剪图像,并在ajax中将坐标发送到服务器后。 When applying changes to the image in PHP I do not get the selected area but something not regular; 当在PHP中对图像应用更改时,我没有得到所选区域,但不是常规区域; sometimes a zoom of the image and sometimes a different area cropping. 有时会放大图像,有时会裁剪其他区域。
I would appreciate any suggestion, because I'm working on this since two days and slowly becoming crazy! 我将不胜感激任何建议,因为我从两天开始从事此工作,并逐渐变得疯狂!
Thanks :) 谢谢 :)
PHP
<?php
header('Access-Control-Allow-Origin: *');
//header('Content-Type: image/jpeg');
$x1 = $_POST['x1'];
$y1 = $_POST['y1'];
$x2 = $_POST['x2'];
$y2 = $_POST['y2'];
$nw = $_POST['nw']; // new width
$nh = $_POST['nh']; // new height
$src = './uploads/hello.jpeg';
$image = @imagecreatefromjpeg($src)
or die('error imagecreatefromjpeg()');
$width = imagesx($image);
$height = imagesy($image);
$cropped = @imagecreatetruecolor($nw, $nh)
or die('error imagecreatetruecolor()');
$result = @imagecopyresampled($cropped, $image, 0, 0, $x1, $y1, $nw, $nh, $width, $height)
or die("error imagecopyresampled()");
// save image
imagejpeg($cropped, 'test.jpeg', 100);
?>
jQuery
$(document).ready(function () {
var x1, y1, x2, y2, nw, nh;
var width = $('#image').prop('naturalWidth');
var height = $('#image').prop('naturalHeight');
function updateCoords(a) {
x1 = a.x; y1 = a.y;
x2 = a.x2; y2 = a.y2;
nw = a.w; nh = a.h;
}
$('#image').Jcrop({
trueSize: [width, height],
onSelect: updateCoords,
onChange: updateCoords,
boxWidth: width,
boxHeight: height
});
$('#button').click(function (e) {
e.preventDefault();
$.ajax({
url: 'http://webaddress/server/crop.php',
type: 'POST',
crossDomain: true,
data: {
x1: x1, y1: y1,
x2: x2, y2: y2,
nw: nw, nh: nh
}
}).done(function (response) {
console.log("Server:\n" + response);
});
});
});
HTML
<img id="image" src="hello.jpeg" alt="image"/>
<button id="button">Crop</button>
In your case you used incorrect function. 在您的情况下,您使用了不正确的功能。 imagecopyresampled
can change size of image. imagecopyresampled
可以更改图像的大小。
For crop function best choice is imagecopy
对于作物功能,最好的选择是imagecopy
Just try change to this 只需尝试更改为此
$result = @imagecopy($cropped, $image, 0, 0, $x1, $y1, $nw, $nh)
or die("error imagecopy()");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.