2013-07-31 153 views
1

我使用下面的代碼來掩蓋另一個圖像上的一個圖像。在輸出時,它給了我一個黑色背景的圖像。Imagick - 使黑色背景變成白色

但我需要一個白色背景或透明背景。

以下是我用來掩蓋另一個圖像的代碼。

<?PHP 
$destination_path = getcwd().DIRECTORY_SEPARATOR; 
$im1="image1.png"; 
$im2="image2.png"; 

$i1="$destination_path$im1"; 
$i2="$destination_path$im2"; 

$base = new Imagick($i1); 
$mask = new Imagick($i2); 

// Setting same size for all images 
$base->resizeImage(274, 275, Imagick::FILTER_LANCZOS, 1); 

// Copy opacity mask 
$base->compositeImage($mask, Imagick::COMPOSITE_DSTIN, 0, 0, Imagick::CHANNEL_ALPHA); 

$base->writeImage('output.png'); 
header("Content-Type: image/png"); 

echo $base; 
?> 

回答

0

嘗試了這一點:Background Color 白:

<?php 
$image = new imagick("opossum.jpg"); 
$image->setimagebackgroundcolor("#fad888"); //Here you can mention the color 
$image->waveImage(20, 176); 
header("Content-Type: image/jpeg"); 
echo $image; 
?> 

透明背景:

<?php 
$im = new Imagick(); 
$im->setBackgroundColor(new ImagickPixel('transparent')); 

$im->readImage('carte_Alain2.svg'); 

$im->setImageFormat("png32"); 

header('Content-type: image/png'); 
echo $im; 
?> 

還要檢查該鏈接:

http://php.net/manual/en/imagick.setbackgroundcolor.php

+0

感謝helping.But有代碼的arent我想要什麼。 – user2360906

+0

他們不給我白色背景也沒有創建一個具有透明背景圖像 – user2360906

+0

,但它對我來說工作正常..如果我在我的系統中運行上述代碼! :( – TomPHP

6

訣竅是使用:$im = $im->flattenImages();

<?php 
$im = new Imagick($filename); 

$im->setImageBackgroundColor('#ffffff'); 
$im = $im->flattenImages(); 

$im->setImageFormat("jpeg"); 
$im->setImageCompressionQuality(95); 
$im->writeImage($filename); 
+0

它不能與pdf文件一起工作如果你用pdf,它會把所有的頁面合併爲一個 – Isfettcom

+0

對我很好用 但是我一個一個地打開頁面:$ im-> readImage ($ file。「[$ page]」); – Laurent