PHP合并图像(白色=透明度) [英] PHP Merge images (white color = transparency)

查看:154
本文介绍了PHP合并图像(白色=透明度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果图像颜色为白色,而透明状态为白色,如何合并图像?

我有这种情况:

我需要以下结果:

解决方案

这不是最好的结果,因为我不知道实现此目的的更好方法.这是我的结果:

这是PHP:

<?php

/** Set source image location **/
$baseImageSource    = 'http://q/stock-photos/tux.png';
$overlayImageSource = 'http://q/stock-photos/firefox-logo-small.jpg';

/** Overlay image configuration **/
// Set this value between 0 and 100. 10 will doing great
$fuzz = 10;

// Set position of overlay image, from top and left;
$overlayTop  = 240;
$overlayLeft = 200;


/** Core program **/

// Create Imagick object for source image
$overlayImage = new Imagick( $overlayImageSource );
$finalImage = new Imagick( $baseImageSource );

// Remove overlay image background
$overlayImage->paintTransparentImage(
    $overlayImage->getImagePixelColor( 0, 0 ),
    0, round( $fuzz * 655.35 )
);

// Set image overlay format
$overlayImage->setImageFormat('png');

// Put overlay image to base image
$finalImage->compositeImage(
    $overlayImage, Imagick::COMPOSITE_DEFAULT,
    $overlayLeft,
    $overlayTop
);

// Set output image format
$finalImage->setImageFormat('png');

// Prepare image and publish!
header('Content-type: image/png');
echo $finalImage;

基本上,这只是 (以实现图像合并)和此答案(以实现背景移除).用于删除背景的方法是 Imagick::paintTransparentImage() ,其中 Imagick::getImagePixelColor() 用于检测背景颜色.然后,我们只需要将两个图像与 Imagick::compositeImage() 合并. /p>

但是,这个结果仍然远非完美,尤其是将其与GIMP或Photoshop等图像处理应用程序进行比较时.但是您应该尝试一下.希望对您有所帮助:)

How to merge images if in image color white get transparency status white color?

I have this situation:

I need this result:

解决方案

This is not a best result, since I didn't know better method to achieve this. Here is my result:

Here is the PHP:

<?php

/** Set source image location **/
$baseImageSource    = 'http://q/stock-photos/tux.png';
$overlayImageSource = 'http://q/stock-photos/firefox-logo-small.jpg';

/** Overlay image configuration **/
// Set this value between 0 and 100. 10 will doing great
$fuzz = 10;

// Set position of overlay image, from top and left;
$overlayTop  = 240;
$overlayLeft = 200;


/** Core program **/

// Create Imagick object for source image
$overlayImage = new Imagick( $overlayImageSource );
$finalImage = new Imagick( $baseImageSource );

// Remove overlay image background
$overlayImage->paintTransparentImage(
    $overlayImage->getImagePixelColor( 0, 0 ),
    0, round( $fuzz * 655.35 )
);

// Set image overlay format
$overlayImage->setImageFormat('png');

// Put overlay image to base image
$finalImage->compositeImage(
    $overlayImage, Imagick::COMPOSITE_DEFAULT,
    $overlayLeft,
    $overlayTop
);

// Set output image format
$finalImage->setImageFormat('png');

// Prepare image and publish!
header('Content-type: image/png');
echo $finalImage;

Basically this is just a modification of this answer (to achieve image merger) and this answer (to achieve background removal). The method used to remove background is Imagick::paintTransparentImage(), with Imagick::getImagePixelColor() is used to detect background color. Then we just need to merge both image with Imagick::compositeImage().

But still, this result is far from perfect, especially if you compare it with image processing app like GIMP or Photoshop. But you should give it a try. Hope it helps :)

这篇关于PHP合并图像(白色=透明度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆