php GD为ean13条形码创建一个透明的png图像

时间:2014-02-19 13:44:37

标签: php

我正在尝试创建一个透明的png图像。这是一个ean-13条形码图像,我希望透明的png格式。我在创建透明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 个答案:

答案 0 :(得分:1)

使用imagecreate代替imagecreatetruecolor,然后在分配颜色后添加imagecolortransparent($im,$white)