2017-11-03 20 views
1

Ich habe ein transparentes Bild und einen Text breiter als das Bild, ich versuche, den Text an der Spitze des transparenten Bildes zentriert einzubetten und ein endgültiges transparentes PNG-Bild zu bekommen, habe ich erzeugt, um den transparenten Text als Behälter mit der Breite und Höhe, aber wenn sie mit dem Bild verbinden kommt es mit einem schwarzen HintergrundKombiniert transparente Bilder in einem GD transparent halten

function createImageServer($name, $state) 
{ 
    $path = 'js/FusionCharts/FusionCharts_XT_Website/Charts/Resources/'; 
    $server = $path . 'server' . $state . '.png'; 

    list($width, $height, $type, $attr) = getimagesize($server); 

    $font_path = 'images_machines/FreeSans.ttf'; 

    $bbox = imagettfbbox(10,0,$font_path,$name); 

    $diffHeight = 5; 

    $fontHeight = ($bbox[1] - $bbox[7]) + $diffHeight; 
    $textWidth = $bbox[2] + $bbox[0]; 

    $dstimage = imagecreatetruecolor($textWidth,$height + $fontHeight); 
    imagealphablending($dstimage, false); 

    //Create alpha channel for transparent layer 
    $col=imagecolorallocatealpha($dstimage,255,255,255,127); 
    imagefilledrectangle($dstimage,0,0,$textWidth, $height+$fontHeight,$col); 
    imagealphablending($dstimage,true); 
    imagefttext($dstimage,10,0,0,$fontHeight-$diffHeight,0,$font_path,$name); 


    $srcimage = imagecreatefrompng($server); 
    imagealphablending($srcimage, false); 

    imagecopymerge($dstimage,$srcimage,($textWidth/2)-($width/2),$fontHeight,0,0,$width,$height,100); 
    imagealphablending($dstimage, true); 
    imagesavealpha($dstimage, true); 

    $pathImage = 'images_machines/' . $name . '.png'; 


    imagepng($dstimage,$pathImage); 

    imagedestroy($dstimage); 
    imagedestroy($srcimage); 

    return $pathImage; 
} 

Originalbild:

enter image description here

resultierendes Bild:

enter image description here

Antwort

0

Ersetzen Sie Ihren Anruf imagecopymerge mit imagecopy (den Funktionsnamen ändern und den letzten Parameter entfernen):

//imagecopymerge($dstimage,$srcimage,($textWidth/2)-($width/2),$fontHeight,0,0,$width,$height,100); 

imagecopy($dstimage,$srcimage,($textWidth/2)-($width/2),$fontHeight,0,0,$width,$height); 

Ergebnis:

enter image description here

+1

Vielen Dank @ Timclutter –