官网地址http://www.barcodephp.com/en/userguide我现在想将这个方法打包成函数,然后传数值进去,返回的是html代码,例如<img src="#"/>这种字符串
function barcode($listing)
{
// Including all required classes
require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');
// Including the barcode technology
require_once('class/BCGcode39.barcode.php');
// Loading Font
$font = new BCGFontFile('./class/font/Arial.ttf', 18);
// The arguments are R, G, B for color.
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255);
$drawException = null;
try {
$code = new BCGcode39();
$code->setScale(2); // Resolution
$code->setThickness(30); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0)
$code->parse($listing); // Text
} catch(Exception $exception) {
$drawException = $exception;
}
/* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - Background color */
$drawing = new BCGDrawing('', $color_white);
if($drawException) {
$drawing->drawException($drawException);
} else {
$drawing->setBarcode($code);
$drawing->draw();
}
// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
}
function barcode($listing)
{
// Including all required classes
require_once('class/BCGFontFile.php');
require_once('class/BCGColor.php');
require_once('class/BCGDrawing.php');
// Including the barcode technology
require_once('class/BCGcode39.barcode.php');
// Loading Font
$font = new BCGFontFile('./class/font/Arial.ttf', 18);
// The arguments are R, G, B for color.
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255);
$drawException = null;
try {
$code = new BCGcode39();
$code->setScale(2); // Resolution
$code->setThickness(30); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0)
$code->parse($listing); // Text
} catch(Exception $exception) {
$drawException = $exception;
}
/* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - Background color */
$drawing = new BCGDrawing('', $color_white);
if($drawException) {
$drawing->drawException($drawException);
} else {
$drawing->setBarcode($code);
$drawing->draw();
}
// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
}
2既然有
header('Content-Type: image/png');
// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
那么差生的是图片数据流所以产生 <img src="您的程序名"/> 的工作是你自己的事情
链接应该没有坏的。http://www.barcodephp.com/如果直接放在一个php文件中请求的话,页面会自动生成png格式的图片。但是在某个php页面中引用了这个方法后,页面上所有的样式都失效,即header部分没了不知道应该怎样修改好呢?
/**
*--------------------------------------------------------------------
*
* Holds the drawing $im
* You can use get_im() to add other kind of form not held into these classes.
*
*--------------------------------------------------------------------
* Copyright (C) Jean-Sebastien Goupil
* http://www.barcodephp.com
*/
include_once('BCGBarcode.php');
include_once('drawer/BCGDrawJPG.php');
include_once('drawer/BCGDrawPNG.php');class BCGDrawing {
const IMG_FORMAT_PNG = 1;
const IMG_FORMAT_JPEG = 2;
const IMG_FORMAT_GIF = 3;
const IMG_FORMAT_WBMP = 4; private $w, $h; // int
private $color; // BCGColor
private $filename; // char *
private $im; // {object}
private $barcode; // BCGBarcode
private $dpi; // int
private $rotateDegree; // float /**
* Constructor.
*
* @param int $w
* @param int $h
* @param string filename
* @param BCGColor $color
*/
public function __construct($filename = null, BCGColor $color) {
$this->im = null;
$this->setFilename($filename);
$this->color = $color;
$this->dpi = null;
$this->rotateDegree = 0.0;
} /**
* Destructor.
*/
public function __destruct() {
$this->destroy();
} /**
* Gets the filename.
*
* @return string
*/
public function getFilename() {
return $this->filename;
} /**
* Sets the filename.
*
* @param string $filaneme
*/
public function setFilename($filename) {
$this->filename = $filename;
} /**
* @return resource.
*/
public function get_im() {
return $this->im;
} /**
* Sets the image.
*
* @param resource $im
*/
public function set_im($im) {
$this->im = $im;
} /**
* Gets barcode for drawing.
*
* @return BCGBarcode
*/
public function getBarcode() {
return $this->barcode;
} /**
* Sets barcode for drawing.
*
* @param BCGBarcode $barcode
*/
public function setBarcode(BCGBarcode $barcode) {
$this->barcode = $barcode;
} /**
* Gets the DPI for supported filetype.
*
* @return int
*/
public function getDPI() {
return $this->dpi;
} /**
* Sets the DPI for supported filetype.
*
* @param float $dpi
*/
public function setDPI($dpi) {
$this->dpi = $dpi;
} /**
* Gets the rotation angle in degree.
*
* @return float
*/
public function getRotationAngle() {
return $this->rotateDegree;
} /**
* Sets the rotation angle in degree.
*
* @param float $degree
*/
public function setRotationAngle($degree) {
$this->rotateDegree = (float)$degree;
} /**
* Draws the barcode on the image $im.
*/
public function draw() {
$size = $this->barcode->getDimension(0, 0);
$this->w = max(1, $size[0]);
$this->h = max(1, $size[1]);
$this->init();
$this->barcode->draw($this->im);
} /**
* Saves $im into the file (many format available).
*
* @param int $image_style
* @param int $quality
*/
public function finish($image_style = self::IMG_FORMAT_PNG, $quality = 100) {
$drawer = null; $im = $this->im;
if ($this->rotateDegree > 0.0) {
if (function_exists('imagerotate')) {
$im = imagerotate($this->im, $this->rotateDegree, $this->color->allocate($this->im));
} else {
throw new BCGDrawException('The method imagerotate doesn\'t exist on your server. Do not use any rotation.');
}
} if ($image_style === self::IMG_FORMAT_PNG) {
$drawer = new BCGDrawPNG($im);
$drawer->setFilename($this->filename);
$drawer->setDPI($this->dpi);
} elseif ($image_style === self::IMG_FORMAT_JPEG) {
$drawer = new BCGDrawJPG($im);
$drawer->setFilename($this->filename);
$drawer->setDPI($this->dpi);
$drawer->setQuality($quality);
} elseif ($image_style === self::IMG_FORMAT_GIF) {
// Some PHP versions have a bug if passing 2nd argument as null.
if ($this->filename === null || $this->filename === '') {
imagegif($im);
} else {
imagegif($im, $this->filename);
}
} elseif ($image_style === self::IMG_FORMAT_WBMP) {
imagewbmp($im, $this->filename);
} if ($drawer !== null) {
$drawer->draw();
}
} /**
* Writes the Error on the picture.
*
* @param Exception $exception
*/
public function drawException($exception) {
$this->w = 1;
$this->h = 1;
$this->init(); // Is the image big enough?
$w = imagesx($this->im);
$h = imagesy($this->im); $text = 'Error: ' . $exception->getMessage(); $width = imagefontwidth(2) * strlen($text);
$height = imagefontheight(2);
if ($width > $w || $height > $h) {
$width = max($w, $width);
$height = max($h, $height); // We change the size of the image
$newimg = imagecreatetruecolor($width, $height);
imagefill($newimg, 0, 0, imagecolorat($this->im, 0, 0));
imagecopy($newimg, $this->im, 0, 0, 0, 0, $w, $h);
$this->im = $newimg;
} $black = new BCGColor('black');
imagestring($this->im, 2, 0, 0, $text, $black->allocate($this->im));
} /**
* Free the memory of PHP (called also by destructor).
*/
public function destroy() {
@imagedestroy($this->im);
} /**
* Init Image and color background.
*/
private function init() {
if ($this->im === null) {
$this->im = imagecreatetruecolor($this->w, $this->h)
or die('Can\'t Initialize the GD Libraty');
imagefilledrectangle($this->im, 0, 0, $this->w - 1, $this->h - 1, $this->color->allocate($this->im));
}
}
}
?>
* Draws the PNG on the screen or in a file.
*/
public function draw() {
ob_start();
imagepng($this->im);
$bin = ob_get_contents();
ob_end_clean(); $this->setInternalProperties($bin); if (empty($this->filename)) {
echo $bin;
} else {
file_put_contents($this->filename, $bin);
}
}
print.php<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
</head><body><img src="test.php?text=Y7034988">/*此处换你的其他变量即可*/
</body>
</html>test.php
<?php
// Including all required classes
require('class/BCGFont.php');
require('class/BCGColor.php');
require('class/BCGDrawing.php'); /*'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93',
'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi',
'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'*/
$codebar = BCGcode39; //该软件支持的所有编码,只需调整$codebar参数即可。// Including the barcode technology
include('class/'.$codebar.'.barcode.php'); // Loading Font
$font = new BCGFont('./class/font/Arial.ttf', 30);// The arguments are R, G, B for color.
$color_black = new BCGColor(0, 0, 0);
$color_white = new BCGColor(255, 255, 255); $code = new $codebar();
$code->setScale(2); // Resolution
$code->setThickness(40); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0)
$text = $_REQUEST['text']; //条形码将要数据的内容
$code->parse($text); /* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - Background color */
$drawing = new BCGDrawing('', $color_white);
$drawing->setBarcode($code);
$drawing->draw();// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');// Draw (or save) the image into PNG format.
$drawing->finish(BCGDrawing::IMG_FORMAT_PNG);
?>
THX~问题解决了!!加分加分!^_^