【问题标题】:Merging two images via gradient通过渐变合并两个图像
【发布时间】:2014-01-27 13:39:57
【问题描述】:

我正在尝试合并PHP 中的两个图像,在中间相互重叠,就像这里一样:http://i.stack.imgur.com/ejdDQ.jpg

但是,我没有运气。 在 ImageMagick 页面上,他们使用这种方法在命令行中执行此操作: http://www.imagemagick.org/Usage/photos/#overlap 但由于我不能在我的服务器上使用 exec,我必须借助安装在服务器上的 imagick 扩展程序 (http://us1.php.net/manual/en/book.imagick.php) 以某种方式做到这一点。

有什么办法可以做到吗?

【问题讨论】:

    标签: php image imagemagick imagick


    【解决方案1】:

    使用链接中的源文件和下面的代码生成图像:

    //Load the images
    $left = new Imagick(realpath('../images/im/holocaust_tn.gif'));
    $right = new Imagick(realpath('../images/im/spiral_stairs_tn.gif'));
    $gradient = new Imagick(realpath('../images/im/overlap_mask.png'));
    
    //The right bit will be offset by a certain amount - avoid recalculating.
    $offsetX = $gradient->getImageWidth() - $right->getImageWidth();
    
    
    //When doing the fading of the images, ImageMagick uses "White == show image".
    //The gradient is setup with black on the left, white on the right. So the for
    //the left image we need to reverse the gradient to make it white on the left.
    $negativeGradient = clone $gradient;
    $negativeGradient->negateimage(false);
    
    //Fade out the left part
    $left->compositeimage(
        $negativeGradient,
        Imagick::COMPOSITE_COPYOPACITY,
        0, 0
    );
    
    //Fade out the right part - offset the gradient
    //to make it align in the final image
    $right->compositeimage(
        $gradient,
        Imagick::COMPOSITE_COPYOPACITY,
        -$offsetX, 0
    );
    
    //Create a new canvas to render everything in to.
    $canvas = new Imagick();
    $canvas->newImage($gradient->getImageWidth(), $gradient->getImageHeight(), new ImagickPixel('black'));
    
    //Blend left half into final image
    $canvas->compositeimage(
        $left,
        Imagick::COMPOSITE_BLEND,
        0, 0
    );
    
    //Blend Right half into final image
    $canvas->compositeimage(
        $right,
        Imagick::COMPOSITE_BLEND,
        $offsetX, 0
    );
    
    
    //Output the final image
    $canvas->setImageFormat('png');
    
    header("Content-Type: image/png");
    echo $canvas->getImageBlob();
    // And Robert is your father's brother.
    

    【讨论】:

    • 所以你基本上将透明度从渐变复制到图像绑定?但你为什么要这样做? " 需要否定掩码 // 使数学正确" $gradient2->negateimage(false);
    • @user3240613 添加了解释 - 老实说,您可能会发现在渐变褪色后显示左侧图像更容易,看看当您不否定它时会发生什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 2012-09-23
    • 2023-03-19
    • 1970-01-01
    相关资源
    最近更新 更多