【问题标题】:Trim whitespace from an image then resize image with 'padding'从图像中修剪空白,然后使用“填充”调整图像大小
【发布时间】:2013-09-19 10:34:17
【问题描述】:

我不介意这是通过 PHP 脚本(最好是 Codeigniter,但任何都可以)或通过 Photoshop 操作来完成。

基本上我想获得一张我可以执行以下操作的图像:

  1. 修剪图像周围所有“无聊”的空白
  2. 最大调整为 380 像素(高度和宽度)
  3. 最后将图像填充到 400 像素 x 400 像素(例如,在修改后的图像周围放置我自己的“白色”填充)

谁能给我任何关于如何使用 GD/ImageMagick 或任何合适的 PHP 脚本/库来做到这一点的提示?

到目前为止,我已经尝试了以下...

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

【问题讨论】:

  • 你在尝试中使用了什么?
  • 我已经添加了我的尝试……但似乎没有做任何事情:(
  • 稍微修改了函数以包含将其输出到文件的选项

标签: php imagemagick gd photoshop image-resizing


【解决方案1】:

这个 Photoshop 脚本会做你想做的事。请记住,修剪功能仅适用于右下角或左上角像素颜色。如果该边界区域“脏”,这将不起作用

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

【讨论】:

猜你喜欢
  • 2018-05-17
  • 2017-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-01
  • 1970-01-01
  • 2014-11-18
  • 2020-01-21
相关资源
最近更新 更多