php - 从缩小图像转换选择坐标

标签 php javascript gd

我正在为网站构建一个简单的图像裁剪实用程序。

我遇到了一个小问题:每次我这样做都会出现失真(例如,我的选择坐标是粗略估计的,因此以错误的尺寸绘制图像)。

我画这个是为了让你明白我的意思:

enter image description here

这里有一些代码可以帮助您实现我想要实现的目标:

    //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_width 和 $croptool_height 都是缩小后的尺寸。 $original_... 是原始的。

有人能帮帮我吗?

编辑:建议的回复很好,但由于某种原因,所做的任何事情都完全错误,请看这里:

enter image description here

编辑:(代码已更新)

谢谢!

最佳答案

可能是我理解错了,但我认为你计算的是“比例”而不是“比例”。比例尺是原始尺寸与新尺寸的乘积。可能是:

    $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
);

关于php - 从缩小图像转换选择坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18622958/

相关文章:

javascript - 波斯字母专用正则表达式

php - 从 php 网页上的 div 中提取类内容的 XPATH 查询是什么?

javascript - addEventListener 到 AngularJS 中的简单指令示例

php - 在 php 中保存 date_format 的问题

javascript - 图像上传不起作用(Froala 编辑器)

php - 处理图像的现成解决方案

php - imagettftext 函数使用来自远程服务器的字体

php - PHP 中的 "imagettfbbox()"是如何工作的?

php - 如何使用mysql数据(PHP)创建highchart?

PHP - 提交页面返回内部服务器错误