使用 PHP/GD,如何修剪图像?

发布于 2024-08-09 02:12:21 字数 162 浏览 4 评论 0原文

我想用 GD 和 PHP模仿 Photoshop 的修剪行为(裁剪所有侧面都被相同颜色覆盖的区域),但我缺乏关于如何实现的想法来实现这一目标。

如果有人对如何做到这一点有任何想法,我非常想听听。

提前致谢。

I want to mimic the trim behavior of Photoshop (crop the area that is covered by the same color on all sides) with GD and PHP but I'm lacking ideas on how to accomplish this.

If someone has any idea on how to do this I would very much like to hear it.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-08-16 02:12:21

我能想到的最简单的解决方案是:

  1. 找出左上角像素的颜色。
  2. 分别从顶部、底部、左侧和右侧开始,水平和垂直逐行浏览图像。如果一条线仅包含左上角遇到的颜色,则它是可修剪的空白空间。通过增加 imagecopy*() 函数的 x 或 y 坐标,从结果图像中省略该行。

未经测试但可能有效。

顺便说一句,ImageMagick 可以开箱即用:http://www.imagemagick.org/用法/裁剪/#trim

The easiest solution I can think of is:

  1. Find out the color of the top left pixel.
  2. Go through the image line by line, horizontally and vertically, starting on the top, bottom , left hand, and right hand side respectively. If a line consists of ONLY the color encountered in the top left corner it is trimmable empty space. Omit that line from the resulting image by increasing the x or y coordinate of your imagecopy*() function.

Untested but might work.

BTW, ImageMagick can do this out of the box: http://www.imagemagick.org/Usage/crop/#trim

没︽人懂的悲伤 2024-08-16 02:12:21

这是一个示例函数(也可以添加填充):

// Trims an image then optionally adds padding around it.
// $im  = Image link resource
// $bg  = The background color to trim from the image
// $pad = Amount of padding to add to the trimmed image
//        (acts simlar to the "padding" CSS property: "top [right [bottom [left]]]")
function imagetrim(&$im, $bg, $pad=null){

    // Calculate padding for each side.
    if (isset($pad)){
        $pp = explode(' ', $pad);
        if (isset($pp[3])){
            $p = array((int) $pp[0], (int) $pp[1], (int) $pp[2], (int) $pp[3]);
        }else if (isset($pp[2])){
            $p = array((int) $pp[0], (int) $pp[1], (int) $pp[2], (int) $pp[1]);
        }else if (isset($pp[1])){
            $p = array((int) $pp[0], (int) $pp[1], (int) $pp[0], (int) $pp[1]);
        }else{
            $p = array_fill(0, 4, (int) $pp[0]);
        }
    }else{
        $p = array_fill(0, 4, 0);
    }

    // Get the image width and height.
    $imw = imagesx($im);
    $imh = imagesy($im);

    // Set the X variables.
    $xmin = $imw;
    $xmax = 0;

    // Start scanning for the edges.
    for ($iy=0; $iy<$imh; $iy++){
        $first = true;
        for ($ix=0; $ix<$imw; $ix++){
            $ndx = imagecolorat($im, $ix, $iy);
            if ($ndx != $bg){
                if ($xmin > $ix){ $xmin = $ix; }
                if ($xmax < $ix){ $xmax = $ix; }
                if (!isset($ymin)){ $ymin = $iy; }
                $ymax = $iy;
                if ($first){ $ix = $xmax; $first = false; }
            }
        }
    }

    // The new width and height of the image. (not including padding)
    $imw = 1+$xmax-$xmin; // Image width in pixels
    $imh = 1+$ymax-$ymin; // Image height in pixels

    // Make another image to place the trimmed version in.
    $im2 = imagecreatetruecolor($imw+$p[1]+$p[3], $imh+$p[0]+$p[2]);

    // Make the background of the new image the same as the background of the old one.
    $bg2 = imagecolorallocate($im2, ($bg >> 16) & 0xFF, ($bg >> 8) & 0xFF, $bg & 0xFF);
    imagefill($im2, 0, 0, $bg2);

    // Copy it over to the new image.
    imagecopy($im2, $im, $p[3], $p[0], $xmin, $ymin, $imw, $imh);

    // To finish up, we replace the old image which is referenced.
    $im = $im2;
}

Here's a sample function (which also can add padding):

// Trims an image then optionally adds padding around it.
// $im  = Image link resource
// $bg  = The background color to trim from the image
// $pad = Amount of padding to add to the trimmed image
//        (acts simlar to the "padding" CSS property: "top [right [bottom [left]]]")
function imagetrim(&$im, $bg, $pad=null){

    // Calculate padding for each side.
    if (isset($pad)){
        $pp = explode(' ', $pad);
        if (isset($pp[3])){
            $p = array((int) $pp[0], (int) $pp[1], (int) $pp[2], (int) $pp[3]);
        }else if (isset($pp[2])){
            $p = array((int) $pp[0], (int) $pp[1], (int) $pp[2], (int) $pp[1]);
        }else if (isset($pp[1])){
            $p = array((int) $pp[0], (int) $pp[1], (int) $pp[0], (int) $pp[1]);
        }else{
            $p = array_fill(0, 4, (int) $pp[0]);
        }
    }else{
        $p = array_fill(0, 4, 0);
    }

    // Get the image width and height.
    $imw = imagesx($im);
    $imh = imagesy($im);

    // Set the X variables.
    $xmin = $imw;
    $xmax = 0;

    // Start scanning for the edges.
    for ($iy=0; $iy<$imh; $iy++){
        $first = true;
        for ($ix=0; $ix<$imw; $ix++){
            $ndx = imagecolorat($im, $ix, $iy);
            if ($ndx != $bg){
                if ($xmin > $ix){ $xmin = $ix; }
                if ($xmax < $ix){ $xmax = $ix; }
                if (!isset($ymin)){ $ymin = $iy; }
                $ymax = $iy;
                if ($first){ $ix = $xmax; $first = false; }
            }
        }
    }

    // The new width and height of the image. (not including padding)
    $imw = 1+$xmax-$xmin; // Image width in pixels
    $imh = 1+$ymax-$ymin; // Image height in pixels

    // Make another image to place the trimmed version in.
    $im2 = imagecreatetruecolor($imw+$p[1]+$p[3], $imh+$p[0]+$p[2]);

    // Make the background of the new image the same as the background of the old one.
    $bg2 = imagecolorallocate($im2, ($bg >> 16) & 0xFF, ($bg >> 8) & 0xFF, $bg & 0xFF);
    imagefill($im2, 0, 0, $bg2);

    // Copy it over to the new image.
    imagecopy($im2, $im, $p[3], $p[0], $xmin, $ymin, $imw, $imh);

    // To finish up, we replace the old image which is referenced.
    $im = $im2;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文