2016-10-07 44 views
2

我已成功地將一個PNG圖像合併到使用PHP GD庫的JPEG背景,但是我希望PNG圖像具有透明背景,但白色背景不斷顯示爲如下圖所示:php將透明PNG圖像合併到jpeg防止白色背景

enter image description here

我已經看了幾個帖子,以解決此問題,並試圖一切都是徒勞的實現方法的不同,任何建議,我應該添加或做什麼?

這裏是我的代碼:

<?php 
     $background = imagecreatefromjpeg('img3.jpg'); 
     $bird = imagecreatefrompng('img4.png'); 
     $bird_x = imagesx($bird); 
     $bird_y = imagesy($bird); 

     imagesavealpha($bird, true); 
     $color = imagecolorallocatealpha($bird, 0, 0, 0, 127); 
     imagefill($bird, 0, 0, $color); 
    if (imagecopymerge($background, $bird, 0, 0, 0, 0, $bird_x, $bird_y, 100)) 
     { 
      header('Content-Type: image/jpeg'); 
      imagejpeg($background); 
      imagedestroy($bird); 
     } 
     else 
      { 
       header('Content-Type: text/html'); 
       echo "Failed to Merge images!"; 
      } 
    ?> 
+1

添加一些代碼樣本這裏 – Elby

回答

1

使用imagecopy的

imagecopy($dest_image, $src, ($offset + 250), $offset, 0, 0, imagesx($src),imagesy($src)); 
+0

我這是怎麼實現的imagecopy的'imagecopy的($背景,$鳥,0,0,0,0,imagesx($鳥),imagesy( $鳥))'而且它可以工作,但是當我改變鳥的座標時會出現一個黑框......關於如何防止這種情況的建議? – Murkdid

+0

@Murkdid可能是鳥圖像小於按照新座標 – Elby

0

我想指定的行爲是由設計。

imagecopymerge合併源圖像的N%像素與目標圖像的(100-N)%像素。根據這個定義,假定在複製鳥的區域(這是完整的矩形)中不會看到任何背景圖像像素是合乎邏輯的。因此,白色像素,而不是阿爾法。

嘗試imagecopyimagecopyresampledexample)而不調整大小。