【问题标题】:PHP + JCrop - Cropping wrong areaPHP + JCrop - 裁剪错误区域
【发布时间】:2014-07-18 06:14:03
【问题描述】:

我正在尝试使用 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.net/manual/en/function.imagecopyresampled.php imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height); 试试imagecopyresampled($dst_r, $img_r, 0, 0, $axis_x, $axis_y, $targ_w, $targ_h, $width, $height);。 (注意 $width 变量被移到了下一行。)
  • 我试试这个,但是不行。
  • 您在 PHP 函数中为 $_POST 变量获取了哪些值?

标签: php jcrop


【解决方案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);

原始.jpg:

裁剪.jpg:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 2011-08-21
    • 2012-05-01
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多