PHP + JCrop - 裁剪错误的区域

Vit*_*lar 7 php jcrop

我正在尝试使用jcrop保存裁剪的图像,基于x,y,w,h.我发送到我的PHP文件,轴x,y和宽度/高度,但裁剪区域是错误的.

这是我的php功能

$axis_x = $_POST["x"];
$axis_y = $_POST["y"];
$width = $_POST["w"];
$height = $_POST["h"];
$path_foto = "imgs/3.jpg";
$targ_w = $width;
$targ_h =  $height;
$jpeg_quality = 90;
$src = $path_foto;
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor($targ_w, $targ_h);

imagecopyresampled($dst_r, $img_r, 0, 0, $axis_x, $axis_y, $width, $targ_w, $targ_h, $height);

imagejpeg($dst_r, $path_foto, $jpeg_quality);
Run Code Online (Sandbox Code Playgroud)

每当图像被重新显示时,此坐标由jcrop在每次隐藏的输入中设置.问题始终是错误的区域.

我做错了什么?

Gui*_*eau 1

(如果不知道结果有什么“问题”,就很难帮助你。)

但您正在/可能遇到一些明显的问题:

  1. 调用中参数的顺序imagecopyresampled()错误:最后 4 个参数应该是$targ_w, $targ_h, $width, $height Ref

  2. “坐标是指左上角。” Ref
    这意味着y = 0位于图像的顶部,而不是底部。因此,如果您的$_POST["y"]值是距图像底部的像素数,则需要从原始图像的高度中减去该值,然后才能按预期工作。

获取您的代码并使用一些硬编码值:

<?php
$axis_x = 115;
$axis_y = 128;
$width = 95;
$height = 128;
$path_foto = "/Users/gb/Downloads/original.jpg";
$targ_w = $width;
$targ_h =  $height;
$jpeg_quality = 90;
$src = $path_foto;
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor($targ_w, $targ_h);

imagecopyresampled($dst_r, $img_r, 0, 0, $axis_x, $axis_y, $targ_w, $targ_h, $width, $height);

imagejpeg($dst_r, "/Users/gb/Downloads/cropped.jpg", $jpeg_quality);
Run Code Online (Sandbox Code Playgroud)

原始.jpg: 原始.jpg

裁剪后的.jpg: 裁剪后的.jpg