isset($options['font'])) {
$font = 5;
} else if (is_int($options['font'])) {
$font = (int)$options['font'];
if ($font < 0 || $font > 5) { $font = 5; }
} else {
$font = imageloadfont($options['font']);
}
// 确定字体宽度和高度
$fontWidth = imagefontwidth($font);
$fontHeight = imagefontheight($font);
// 确定图像的宽度和高度
$width = $fontWidth * strlen($code) + $paddingLeft + $paddingRight +
$border * 2 + 1;
$height = $fontHeight + $paddingTop + $paddingBottom + $border * 2 + 1;
// 创建图像
$img = imagecreate($width, $height);
// 绘制边框
if ($border) {
$rgb = $this->_hex2rgb($bdColor);
$borderColor = imagecolorallocate($img, $rgb['red'],
$rgb['green'], $rgb['blue']);
imagefilledrectangle($img, 0, 0, $width, $height, $borderColor);
}
// 绘制背景
$rgb = $this->_hex2rgb($bgcolor);
$backgroundColor = imagecolorallocate($img,
$rgb['red'], $rgb['green'], $rgb['blue']);
imagefilledrectangle($img, $border, $border,
$width - $border - 1, $height - $border - 1, $backgroundColor);
// 绘制文字
$rgb = $this->_hex2rgb($color);
$textColor = imagecolorallocate($img,
$rgb['red'], $rgb['green'], $rgb['blue']);
imagestring($img, $font, $paddingLeft + $border, $paddingTop + $border,
$code, $textColor);
// 输出图像
header("Content-type: " . image_type_to_mime_type(IMAGETYPE_JPEG));
imagejpeg($img);
imagedestroy($img);
}
/**
* 将 16 进制颜色值转换为 rgb 值
*
* @param string $hex
*
* @return array
*/
function _hex2rgb($hex) {
$rgb = array();
if (strtolower(substr($hex, 0, 2)) == '0x') {
$offset = 2;
} else {
$offset = 0;
}
$rgb['red'] = hexdec(substr($hex, $offset, 2));
$offset += 2;
$rgb['green'] = hexdec(substr($hex, $offset, 2));
$offset += 2;
$rgb['blue'] = hexdec(substr($hex, $offset, 2));
return $rgb;
}
}