2015-07-13 110 views
1

我需要將兩個圖像合併在一起並向其中添加文本。但是,當代碼輸出我得到黑色背景。當我刪除「imagecopymerge」功能,我看到白色背景。imagecopymerge獲取白色背景而不是黑色

$firstUrl = '1.jpg'; 
$secondUrl = "2.jpg"; 
$a = getimagesize ($firstUrl);  

$x = 320; 
$y = $a[1]+88; 

$image = imagecreatetruecolor($x, $y); 
$white = imagecolorallocate($image, 255, 255, 255); 

imagefilledrectangle($image, 0, 0, $x, $y, $white);  

$first = imagecreatefromjpeg($firstUrl); 
$second = imagecreatefromjpeg($secondUrl); 

imagecopymerge($image,$first,0,0,0,0, $x, $y,100); 
imagecopymerge($image,$second,$a[0]-70,$a[1]+10,0,0, 60, 60,100);  

$text_color = imagecolorallocate($image, 51, 51, 51); 
$font = 'a.ttf'; 
imagettftext($image, 12, 0, 10, $a[1]+30, $text_color, $font, "text"); 

header("Content-Type: image/png"); 
imagepng($image); 

回答

1

使用要插入圖像的寬度和高度:

imagecopymerge($image,$first,0,0,0,0,imagesx($first),imagesy($first),100); 

$x$y是最終圖像的尺寸。如果$first小於此值,則imagecopymerge()用黑色填充缺失區域。

通過使用imagesx()imagesy(),您複製圖像的確切區域。

0

更改行,imagecopymerge($ image,$ first,0,0,0,0,$ x,$ y,100);成像像imagecopymerge($ image,$ first,100,100,0,0,$ x,$ y,100); 然後它應該工作。