首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不使用TrueType字体显示条形码的PHP函数

不使用TrueType字体显示条形码的PHP函数
EN

Stack Overflow用户
提问于 2014-07-19 03:27:39
回答 1查看 667关注 0票数 1

你好,我正在使用我在互联网上找到的一个函数来显示一个使用TrueType字体的barCode,代码如下:

代码语言:javascript
复制
//For displaying barcodes

//Arguments are:
//  code    Number you want outputted as a barcode

//You can use this script in two ways:
//  From a webpage/PHP script   <img src='/images/barcode.php?code=12345'/>
//  Directly in your web browser    http://www.example.com/images/barcode.php?code=12345

//Outputs the code as a barcode, surrounded by an asterisk (as per standard)
//Will only output numbers, text will appear as gaps
//Image width is dynamic, depending on how much data there is


header("Content-type: image/png");
$file = "barcode.png"; // path to base png image
$im = imagecreatefrompng($file); // open the blank image
$string = "123123123"; // get the code from URL
imagealphablending($im, true); // set alpha blending on
imagesavealpha($im, true); // save alphablending setting (important)

$black = imagecolorallocate($im, 0, 0, 0); // colour of barcode

$font_height=40; // barcode font size. anything smaller and it will appear jumbled and will not be able to be read by scanners

$newwidth=((strlen($string)*20)+41); // allocate width of barcode. each character is 20px across, plus add in the asterisk's
$thumb = imagecreatetruecolor($newwidth, 40); // generate a new image with correct dimensions

imagecopyresized($thumb, $im, 0, 0, 0, 0, $newwidth, 40, 10, 10); // copy image to thumb
imagettftext($thumb, $font_height, 0, 1, 40, $black, 'B2FI25HRc.ttf', '*'.$string.'*'); // add text to image

//show the image
imagepng($thumb);
imagedestroy($thumb);

我找不到为什么函数不能显示图像的错误。有什么想法吗?字体与php函数在同一目录中,我尝试了字体的相对路径和绝对路径,但没有结果。有什么建议吗?

非常感谢

EN

回答 1

Stack Overflow用户

发布于 2014-07-19 03:42:34

您需要检查错误消息。为了调试,注释掉标题行,并在顶部添加以下行,以显示所有错误:

代码语言:javascript
复制
ini_set('display_errors',true);
error_reporting(E_ALL);

在许多情况下,错误消息会很清楚地告诉您出了什么问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24832389

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档