Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP/GD,如何修剪图像?_Php_Image_Image Processing_Gd_Gdi - Fatal编程技术网

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

使用PHP/GD,如何修剪图像?,php,image,image-processing,gd,gdi,Php,Image,Image Processing,Gd,Gdi,我想用GD和PHP模仿Photoshop的修剪行为(裁剪所有侧面都被相同颜色覆盖的区域),但我对如何实现这一点缺乏想法 如果有人知道怎么做,我很想听听 提前感谢。我能想到的最简单的解决方案是: 找出左上角像素的颜色 从顶部、底部、左侧和右侧开始,水平和垂直地逐行检查图像。如果线条仅由左上角遇到的颜色组成,则它是可修剪的空白。通过增加imagecopy*()函数的x或y坐标,从结果图像中省略该行 未经测试,但可能有效 顺便说一句,ImageMagick可以做到这一点:示例函数(也可以添加填充):

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

如果有人知道怎么做,我很想听听


提前感谢。

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

  • 找出左上角像素的颜色
  • 从顶部、底部、左侧和右侧开始,水平和垂直地逐行检查图像。如果线条仅由左上角遇到的颜色组成,则它是可修剪的空白。通过增加imagecopy*()函数的x或y坐标,从结果图像中省略该行
  • 未经测试,但可能有效

    顺便说一句,ImageMagick可以做到这一点:示例函数(也可以添加填充):

    //修剪图像,然后可选地在其周围添加填充。
    //$im=图像链接资源
    //$bg=要从图像中修剪的背景色
    //$pad=要添加到修剪图像的填充量
    //(类似于“padding”CSS属性:“top[右[下[左]]]”)
    函数imagetrim(&$im,$bg,$pad=null){
    //计算每边的填充。
    如果(isset($pad)){
    $pp=爆炸(“”,$pad);
    如果(isset($pp[3])){
    $p=数组((int)$pp[0],(int)$pp[1],(int)$pp[2],(int)$pp[3]);
    }else if(isset($pp[2])){
    $p=数组((int)$pp[0],(int)$pp[1],(int)$pp[2],(int)$pp[1]);
    }else if(isset($pp[1])){
    $p=数组((int)$pp[0],(int)$pp[1],(int)$pp[0],(int)$pp[1]);
    }否则{
    $p=数组填充(0,4,(int)$pp[0]);
    }
    }否则{
    $p=数组填充(0,4,0);
    }
    //获取图像的宽度和高度。
    $imw=imagesx($im);
    $imh=imagesy($im);
    //设置X变量。
    $xmin=$imw;
    $xmax=0;
    //开始扫描边缘。
    对于($iy=0;$iy>16)和0xFF,($bg>>8)和0xFF,$bg&0xFF);
    imagefill($im2,0,0,$bg2);
    //将其复制到新图像。
    imagecopy($im2、$im、$p[3]、$p[0]、$xmin、$ymin、$imw、$imh);
    //最后,我们替换引用的旧图像。
    $im=$im2;
    }
    
    你为此与GD结婚了吗?ImageMagick已经简化了它。你能举一个$bg的预期输入的例子吗?
    // 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;
    }