<?php
error_reporting(E_ALL);
header('Content-type:image/png');
$width=200;
$height=200;
$color="#f00";
$string = "宇宙最强宇宙最强最强";
$im = imagecreate($width, $height);
$color=hex2rgb($color);
// var_dump($color);exit;
$bg = imagecolorallocate($im, $color['red'], $color['green'], $color['blue']); //设置画布的背景为白色
$black = imagecolorallocate($im, 255, 255, 255); //设置一个颜色变量为黑色
//在图像中输出的字符,字体乱码的时候使用
$font_file = './simhei.ttf';
$str ='php'.iconv('gb2312','utf-8','面对对象')." asd师傅的说法www.phpobject.net";
imagefttext($im, 12,0, 0, $height/2, $black, $font_file,$string);
//imagestring($im, 5, 28, 70, $string, $black); //水平的将字符串输出到图像中
imagepng($im);
//一个很使用的php颜色转换函数,用于将hex表示的颜色值转换为rgb格式。例如:#dfdfdf转换成(239,239,239)
function hex2rgb( $colour ) {
if ( $colour[0] == '#' ) {
$colour = substr( $colour, 1 );
}
if ( strlen( $colour ) == 6 ) {
list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );
} elseif ( strlen( $colour ) == 3 ) {
list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );
} else {
return false;
}
$r = hexdec( $r );
$g = hexdec( $g );
$b = hexdec( $b );
return array( 'red' => $r, 'green' => $g, 'blue' => $b );
}
?>
转载于:https://my.oschina.net/u/2479599/blog/513783