dongshi1868 2014-05-13 20:17
浏览 15
已采纳

COMPOSITE_DISSOLVE无法在PHP中运行

I want to make the mixed image with fade effect from two images.

Here is my PHP code.

$img1 = new Imagick();
$img1->readImage("images/1.jpg");

$img2 = new Imagick();
$img2->setOption('compose:args', '50');
$img2->readImage("images/2.jpg");

$img1->compositeImage($img2, imagick::COMPOSITE_DISSOLVE, 0, 0);
$img1->writeImage("images/3.jpg");

This code works correctly on my Mac OS.

But it doesn't work on remote server (Cent OS 6.5), the result image is same with 2.jpg.

I tried with COMPOSITE_BLEND constant, it works well.

And I tried the command in terminal.

# composite -dissolve 50 2.jpg 1.jpg 3.jpg

It also works well.

Why doesn't COMPOSITE_DISSOLVE constant work in PHP?

Please help me.

  • 写回答

1条回答 默认 最新

  • duan0065626385 2014-05-14 01:14
    关注

    You really ought to just figure out what the difference is between your dev environment and your server. It's very likely to be the case that the version of ImageMagick on the dev server is very out of date, unless you are compiling it yourself, as the Centos packages management system seems to take a long time to get new releases.

    However....the way you are blending the images is not fantastic. Although it may work for you, it requires the slightly poorly defined $img2->setOption('compose:args', '50'); line.

    A much more powerful technique is to control the alpha channel yourself through the COMPOSITE_COPYOPACITY. This gives you full control for how images are blended. e.g.

    $img1 = new \Imagick();
    $img1->readImage(realpath("../images/Biter_500.jpg"));
    
    $img2 = new \Imagick();
    $img2->readImage(realpath("../images/Skyline_400.jpg"));
    
    //Resize images to the same size, to look pretty.
    $img1->resizeimage(
     $img2->getImageWidth(), 
     $img2->getImageHeight(),
     \Imagick::FILTER_LANCZOS, 
     1
    );
    
    //Create an image that the alpha will be created in.
    $opacity = new \Imagick();
    
    if (true) {
        //Create a 50% grey image
        $opacity->newPseudoImage($img1->getImageWidth(), $img1->getImageHeight(), "CANVAS:gray(50%)");
    }
    else {
        //Create a far more interesting blend.
        //Gradients are created top to bottom, so we need to rotate the image  
        $opacity->newPseudoImage($img1->getImageHeight(), $img1->getImageWidth(), "gradient:gray(10%)-gray(90%)");
        $opacity->rotateimage('black', 90);
    }
    
    $img2->compositeImage($opacity, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
    $img1->compositeImage($img2, \Imagick::COMPOSITE_ATOP, 0, 0);
    
    header("Content-Type: image/jpg");
    echo $img1->getImageBlob();
    

    The first way of creating the opacity image with "CANVAS:gray(50%)" does what your existing image code does. The second way of blending creates a image that is blended from 10% to 90% across the width of the image.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集
  • ¥15 在启动roslaunch时出现如下问题