PHP + JCrop - 裁剪错误的区域

问题描述 投票:7回答:1

我正在尝试使用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);

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

我做错了什么?

php jcrop
1个回答
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);

original.jpg:original.jpg

cropped.jpg:cropped.jpg

© www.soinside.com 2019 - 2024. All rights reserved.