doubang4881 2013-09-19 10:34
浏览 189
已采纳

修剪图像中的空白,然后使用“填充”调整图像大小

I don't mind if this is done via a PHP script (ideally Codeigniter but any is fine) or via a Photoshop action.

Essentially I want to get an image which I can do the following:

  1. Trim all the 'boring' whitespace around the image
  2. Resize to a maximum of 380px (height & width)
  3. Finally pad the image out to 400px x 400px (e.g put my own 'white' padding around the modified image)

Can anyone give me any tips on how to do this using GD/ImageMagick or any suitable PHP script/library?

I have tried the following so far...

  $gd = imagecreatefromjpeg('./assets/temp/whitespace-img.jpg');
  imagetrim($gd, '#ffffff', null);

    function imagetrim($im, $bg, $pad=null, $output=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);

    if ($output)
    {
        imagejpeg($im2, $output);           
    }

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

1条回答 默认 最新

  • drutcs7210 2013-09-19 11:27
    关注

    This Photoshop script will do what you want. Pleas bear in mind that the trim function only works of the bottom right or top left pixel colour. This won't work if that border area is "dirty"

    var srcDoc = app.activeDocument;
    
    //trim image to transparent width
    srcDoc.trim(TrimType.TOPLEFT, true, true, true, true);
    
    var w = srcDoc.width.value;
    var h = srcDoc.height.value;
    
    if (w<h)
      {
        var imageWidth = 380;
        var imageHeight = Math.floor(w*(380/h))
      }
    else
      {
        var imageHeight = 380;
        var imageWidth = Math.floor(h*(380/w))
      }
    
    var resizeRes = 72
    var resizeMethod = ResampleMethod.BICUBIC;
    
    //resize
    srcDoc.resizeImage(imageWidth, imageHeight, resizeRes, resizeMethod)
    
    // adjust canvas size
    srcDoc.resizeCanvas(400, 400, AnchorPosition.MIDDLECENTER);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算