Php 返回图像的base64表示形式
以下代码取自David S.Tufts条形码PHP脚本:Php 返回图像的base64表示形式,php,base64,barcode,Php,Base64,Barcode,以下代码取自David S.Tufts条形码PHP脚本: function barcode( $filepath = "", $text = "0", $size = "20", $orientation = "horizontal", $code_type = "code128", $print = false, $SizeFactor = 1 ) { $code_string = ""; $chksum = 104; // Must not change o
function barcode( $filepath = "", $text = "0", $size = "20", $orientation = "horizontal", $code_type = "code128", $print = false, $SizeFactor = 1 ) {
$code_string = "";
$chksum = 104;
// Must not change order of array elements as the checksum depends on the array's key to validate final code
$code_array = [
" " => "212222",
"!" => "222122",
"\"" => "222221",
"#" => "121223",
"$" => "121322",
"%" => "131222",
"&" => "122213",
"'" => "122312",
"(" => "132212",
")" => "221213",
"*" => "221312",
"+" => "231212",
"," => "112232",
"-" => "122132",
"." => "122231",
"/" => "113222",
"0" => "123122",
"1" => "123221",
"2" => "223211",
"3" => "221132",
"4" => "221231",
"5" => "213212",
"6" => "223112",
"7" => "312131",
"8" => "311222",
"9" => "321122",
":" => "321221",
";" => "312212",
"<" => "322112",
"=" => "322211",
">" => "212123",
"?" => "212321",
"@" => "232121",
"A" => "111323",
"B" => "131123",
"C" => "131321",
"D" => "112313",
"E" => "132113",
"F" => "132311",
"G" => "211313",
"H" => "231113",
"I" => "231311",
"J" => "112133",
"K" => "112331",
"L" => "132131",
"M" => "113123",
"N" => "113321",
"O" => "133121",
"P" => "313121",
"Q" => "211331",
"R" => "231131",
"S" => "213113",
"T" => "213311",
"U" => "213131",
"V" => "311123",
"W" => "311321",
"X" => "331121",
"Y" => "312113",
"Z" => "312311",
"[" => "332111",
"\\" => "314111",
"]" => "221411",
"^" => "431111",
"_" => "111224",
"\`" => "111422",
"a" => "121124",
"b" => "121421",
"c" => "141122",
"d" => "141221",
"e" => "112214",
"f" => "112412",
"g" => "122114",
"h" => "122411",
"i" => "142112",
"j" => "142211",
"k" => "241211",
"l" => "221114",
"m" => "413111",
"n" => "241112",
"o" => "134111",
"p" => "111242",
"q" => "121142",
"r" => "121241",
"s" => "114212",
"t" => "124112",
"u" => "124211",
"v" => "411212",
"w" => "421112",
"x" => "421211",
"y" => "212141",
"z" => "214121",
"{" => "412121",
"|" => "111143",
"}" => "111341",
"~" => "131141",
"DEL" => "114113",
"FNC 3" => "114311",
"FNC 2" => "411113",
"SHIFT" => "411311",
"CODE C" => "113141",
"FNC 4" => "114131",
"CODE A" => "311141",
"FNC 1" => "411131",
"Start A" => "211412",
"Start B" => "211214",
"Start C" => "211232",
"Stop" => "2331112",
];
$code_keys = array_keys( $code_array );
$code_values = array_flip( $code_keys );
for ( $X = 1; $X <= strlen( $text ); $X ++ ) {
$activeKey = substr( $text, ( $X - 1 ), 1 );
$code_string .= $code_array[ $activeKey ];
$chksum = ( $chksum + ( $code_values[ $activeKey ] * $X ) );
}
$code_string .= $code_array[ $code_keys[ ( $chksum - ( intval( $chksum / 103 ) * 103 ) ) ] ];
$code_string = "211214" . $code_string . "2331112";
// Pad the edges of the barcode
$code_length = 20;
if ( $print ) {
$text_height = 30;
} else {
$text_height = 0;
}
for ( $i = 1; $i <= strlen( $code_string ); $i ++ ) {
$code_length = $code_length + (integer) ( substr( $code_string, ( $i - 1 ), 1 ) );
}
if ( strtolower( $orientation ) == "horizontal" ) {
$img_width = $code_length * $SizeFactor;
$img_height = $size;
} else {
$img_width = $size;
$img_height = $code_length * $SizeFactor;
}
$image = imagecreate( $img_width, $img_height + $text_height );
$black = imagecolorallocate( $image, 0, 0, 0 );
$white = imagecolorallocate( $image, 255, 255, 255 );
imagefill( $image, 0, 0, $white );
if ( $print ) {
imagestring( $image, 5, 31, $img_height, $text, $black );
}
$location = 10;
for ( $position = 1; $position <= strlen( $code_string ); $position ++ ) {
$cur_size = $location + ( substr( $code_string, ( $position - 1 ), 1 ) );
if ( strtolower( $orientation ) == "horizontal" ) {
imagefilledrectangle( $image, $location * $SizeFactor, 0, $cur_size * $SizeFactor, $img_height,
( $position % 2 == 0 ? $white : $black ) );
} else {
imagefilledrectangle( $image, 0, $location * $SizeFactor, $img_width, $cur_size * $SizeFactor, ( $position % 2 == 0 ? $white : $black ) );
}
$location = $cur_size;
}
// Draw barcode to the screen or save in a file
if ( $filepath == "" ) {
header( 'Content-type: image/png' );
imagepng( $image );
imagedestroy( $image );
} else {
imagepng( $image, $filepath );
imagedestroy( $image );
}
}
barcode( "", "foo", "20", "horizontal", "code128", true, "1" );
而不是图像
我已经试过换线了:
imagepng$image
到
返回数据:image/png;base64、.base64_encodeimagepng$image;
但它似乎不起作用。您所需要做的就是在进行调用之前打开输出缓冲,然后获取缓冲区内容
$contents = null;
ob_start(); // Start output buffering
imagealphablending($resource, true);
imagesavealpha($resource, true);
imagepng($resource);
$contents = ob_get_flush(); // Stop output buffering
这个问题与字节码无关。你错误的称呼实际上是。此外,请描述似乎不起作用的具体情况:如何不发挥作用?字符串的结尾是什么?还是有错误?你觉得没问题,但浏览器拒绝了吗?发生了什么?是的,阿玛丹,你是对的。谢谢你纠正它。我的意思是数据uri。我的意思是,它返回一个错误,而不是显示渲染图像,这似乎不起作用。我试图编辑我的问题,但编辑不允许我进行编辑。我很抱歉我的问题含糊不清谢谢你@delboy1978uk花时间回答,但我已经通过执行codeob_start解决了这个问题;imagepng$image$image\u data=ob\u get\u内容;清洁卫生;返回base64_encode$image_数据;代码是的,这正是我的意思,很高兴你能成功。你能把这个标记为正确答案吗-
$contents = null;
ob_start(); // Start output buffering
imagealphablending($resource, true);
imagesavealpha($resource, true);
imagepng($resource);
$contents = ob_get_flush(); // Stop output buffering