2014-02-19 140 views
-1

我想创建一个透明png image.this是一个EAN-13条码图像,我想在png格式中透明。我在创建透明png时遇到了麻烦。它目前有一个白色的背景。php GD为ean13条形码创建一个透明PNG图像

任何人都可以指向正确的方向。这是到目前为止的代码...

include('./php-barcode.php'); 

    $font  = './NOTTB___.TTF'; 

    $fontSize = 16; // GD1 in px ; GD2 in point 
    $marge = 10; // between barcode and hri in pixel 
    $x  = 80; // barcode center 
    $y  = 80; // barcode center 
    $height = 50; // barcode height in 1D ; module size in 2 
    $width = 2; // barcode height in 1D ; not use in 2D 
    $angle = 0; // rotation in degrees : nb : non horizontable barcode might not be usable because of pixelisation 

    $code  = $codevalue; // barcode, of course ;) 
    $type  = $typevalue; // 

    function drawCross($im, $color, $x, $y){ 
    imageline($im, $x - 10, $y, $x + 10, $y, $color); 
    imageline($im, $x, $y- 10, $x, $y + 10, $color); 
    } 

    $im  = imagecreatetruecolor(150, 150); 
    $black = ImageColorAllocate($im,0x00,0x00,0x00); 
    $white = ImageColorAllocate($im,0xff,0xff,0xff); 
    imagefilledrectangle($im, 0, 0, 150, 150, $white); 

    $data = Barcode::gd($im, $black, $x, $y, $angle, $type, array('code'=>$code), $width, $height); 

    if (isset($font)){ 
    $box = imagettfbbox($fontSize, 0, $font, $data['hri']); 
    $len = $box[2] - $box[0]; 
    Barcode::rotate(-$len/2, ($data['height']/2) + $fontSize + $marge, $angle, $xt, $yt); 
    imagettftext($im, $fontSize, $angle, $x + $xt, $y + $yt, $black, $font, $data['hri']); 
    } 

    header('Content-type: image/png'); 
    imagepng($im); 
    imagedestroy($im); 
+1

“这是我的代码到目前为止...” - **我的**代码? https://github.com/jbdemonte/barcode/blob/master/php/sample%20php/sample-gd.php。应该是“我发现有人在线代码,根据我的需要调整它” –

回答

1

使用imagecreate代替imagecreatetruecolor,然后分配颜色之后添加。

+0

感谢它的工作,但我需要你的一点帮助,请让我正确我怎样才能使透明文本和条形码白色 – Anni

+0

我是不知道你的意思,@ user3328337 ...但你应该能够将我给你的东西应用到你的需求 - 如果有必要,分配另一种颜色。 –