php 生成占位图片

<?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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值