从缩小的图像中平移选择坐标

时间:2013-09-04 20:14:26

标签: 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

编辑:(代码已更新)

谢谢!

3 个答案:

答案 0 :(得分:1)

可能是我很想念,但我认为你在计算“比例”而不是“规模”。规模是原始尺寸与新尺寸的乘积。可能是:

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

答案 1 :(得分:0)

你可以试试这个:

original width * new height / original height = new width;

original height * new width / original width = new height;

[1] Get aspect ratio from width and height of image (PHP or JS)

答案 2 :(得分:0)

我不确定这是否是整个解决方案,但你在这里遇到了问题:

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