在Imagick中,如何扩展最后一列像素并将其水平复制到我想要的大小?

时间:2014-03-03 14:00:00

标签: imagick

我不确定我的主题是否足够明确。

我在渐变灰色背景上有一个人体模型的图像。实际上有数千人。由于光线不同等原因,每个背景都有点不同......

我想将这些800像素宽,1500像素高的图像转换为1000x1500,方法是将1像素宽的第一列复制100个额外像素,并将其转换为最后一列。这样,我可以在图像上获得平滑的渐变,并以1000x1500像素图像完成。

但是,我无法弄清楚如何做到这一点。我看到我可以添加其他图像,或选择纯色,但由于背景不同,它必须基于每个图像。

任何帮助都会非常感激,即使这意味着我必须使用另一个php解决方案。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用代码来完成!

<?php
$imagick = new Imagick(realpath("../images/TestImage.jpg"));

$imagick->setImageVirtualPixelMethod(Imagick::VIRTUALPIXELMETHOD_EDGE);

$desiredWidth = 1000;
$desiredHeight = 1500;

$originalWidth = $imagick->getImageWidth();

//Make the image be the desired width.
$imagick->sampleimage(
    $desiredWidth, 
    $imagick->getImageHeight()
);

//Now scale, rotate, translate (aka affine project) it
//to be how you want
$points = array(
    //The x scaling factor is 0.5 when the desired width is double
    //the source width
    ($originalWidth / $desiredWidth), 0,
    //Don't scale vertically
    0, 1,
    //Offset the image so that it's in the centre
    ($desiredWidth - $originalWidth) / 2,  0
);

$imagick->distortImage(Imagick::DISTORTION_AFFINEPROJECTION, $points, false);

header("Content-Type: image/jpg");
echo $imagick->getImageBlob();


//Fyi it may be easier to think of the affine transform by 
//how it works for a rotation:
//$affineRotate = array(
//    "sx" => cos($angle),
//    "sy" => cos($angle),
//    "rx" => sin($angle),
//    "ry" => -sin($angle),
//    "tx" => 0,
//    "ty" => 0,
//);