Php 从缩小的图像转换选择坐标
我正在为网站构建一个简单的图像裁剪工具 我遇到了一个小问题:每次我这样做都会有失真(例如,我的选择坐标是粗略估计的,因此图像是用错误的尺寸绘制的) 我画这个草图是为了让你明白我的意思: 这里有一些代码可以帮助您实现我的目标:Php 从缩小的图像转换选择坐标,php,javascript,gd,Php,Javascript,Gd,我正在为网站构建一个简单的图像裁剪工具 我遇到了一个小问题:每次我这样做都会有失真(例如,我的选择坐标是粗略估计的,因此图像是用错误的尺寸绘制的) 我画这个草图是为了让你明白我的意思: 这里有一些代码可以帮助您实现我的目标: //Get the new coordinates to crop the image. $x1 = $request->getPost('x1'); $y1 = $request->getPost('y1'); $x2 = $
//Get the new coordinates to crop the image.
$x1 = $request->getPost('x1');
$y1 = $request->getPost('y1');
$x2 = $request->getPost('x2');
$y2 = $request->getPost('y2');
$w = $request->getPost('w');
$h = $request->getPost('h');
$croptool_width = $request->getPost('croptool_width');
$croptool_height = $request->getPost('croptool_height');
$original_width = $request->getPost('original_width');
$original_height = $request->getPost('original_height');
$infos = pathinfo($original_image_location);
$extension = strtolower($infos['extension']);
switch($extension) {
case 'gif':
$sourceImage = imagecreatefromgif($original_image_location);
break;
case 'jpg':
case 'jpeg':
$sourceImage = imagecreatefromjpeg($original_image_location);
break;
case 'png':
$sourceImage = imagecreatefrompng($original_image_location);
break;
}
// Take croptool's dimension, transpose selection coordinates to fit the original image.
$scale_y = $original_height / $croptool_height;
$scale_x = $original_width / $croptool_width;
$scaled_x1 = $x1 * $scale_x;
$scaled_y1 = $y1 * $scale_y;
$scaled_x2 = $x2 * $scale_x;
$scaled_y2 = $y2 * $scale_y;
// Crop selection and save to disk
$cropImage = imagecreatetruecolor(Model_Wineries::getCoverImageWidth(), Model_Wineries::getCoverImageHeight());
imagecopyresampled(
$cropImage, $sourceImage,
0, 0,
$scaled_x1, $scaled_y1,
Model_Wineries::getLogoImageWidth(), Model_Wineries::getLogoImageHeight(),
$scaled_x2, $scaled_y2
);
if (file_exists($cover_image_location))
unlink($cover_image_location);
imagejpeg($cropImage, $cover_image_location, 90);
chmod($cover_image_location, 0777);
if (file_exists($original_image_location))
unlink($original_image_location);
我很难弄清楚的部分是如何正确计算$scale
变量
$croptool_宽度和$croptool_高度均为缩小尺寸$原版。。。都是原版的
有人能帮我吗
编辑:建议的回答是好的,但由于某些原因,所做的一切都是完全错误的,请看这里:
编辑:(代码已更新)
谢谢 也许我理解错了,但我认为你是在计算“比例”而不是“比例”。刻度是原始尺寸与新尺寸的乘积。可能是:
$scale_y = $original_height / $croptool_height;
$scale_x = $original_width / $croptool_width;
$scaled_x1 = $x1 * $scale_x;
$scaled_y1 = $y1 * $scale_y;
$scaled_x2 = $x2 * $scale_x;
$scaled_y2 = $y2 * $scale_y;
//Original height = 1000 and Croptool height = 100 ==> scale_y = 10; Pixel (0, 45) on crop space == 45 * 10 == 450 on original image.
//Original height = 100 and Croptool height = 1000 ==> scale_y = 0.10; Pixel (0, 450) on crop space == 450 * 0.10 == 45 on original image.
希望这有帮助
编辑:
我不熟悉PHP API,但它看起来提供的源代码宽度和源代码高度是错误的,可能是您可以计算为
imagecopyresampled(
$cropImage, $sourceImage,
0, 0,
$scaled_x1, $scaled_y1,
Model_Wineries::getLogoImageWidth(), Model_Wineries::getLogoImageHeight(),
$scaled_x2 - $scaled_x1, $scaled_y2 - $scaled_y1
);
您可以尝试以下方法:
original width * new height / original height = new width;
original height * new width / original width = new height;
[1] 我不确定这是否是整个解决方案,但您遇到了一个问题:
imagecopyresampled(
$cropImage, $sourceImage,
0, 0,
$scaled_x1, $scaled_y1,
Model_Wineries::getLogoImageWidth(), Model_Wineries::getLogoImageHeight(),
$scaled_x2, $scaled_y2
);
根据这个函数的php文档,最后两个参数应该是源图像的宽度和高度,但是您传递的是坐标。所以不是$scaled_x2,而是通过$scaled_x2-$scaled_x1,而不是$scaled_y2,通过$scaled_y2-$scaled_y1它几乎可以工作,但它们仍然有点失真。例如,如果我选择这个:cl.ly/image/0L1Q1w441500,我会得到这个结果:cl.ly/image/0c1x3H0K0Z1k非常感谢!退一步,让我们忽略该代码。你想要实现什么,而不是你已经拥有的。脚本是否接收到图像和裁剪坐标/尺寸,然后应该生成该裁剪?如果是这样的话,考虑使用它来为你做这项工作。你试过改变宽高比吗?我已经发布了一个anwser.ImageMagick通过命令行工作,您可以从PHP或Java执行()或任何东西,它经过了难以置信的良好测试、稳定并得到广泛支持。您还可以独立于服务器测试命令行调用和坐标,以获得所需的。。我支持@Mike'Pomax'Kamermansthere还有一个PHP imagemagick API,所以您不需要exec。大多数函数都可以直接从PHP获得