用php生成动态gif图片,Matrix代码雨效果,Matrix Digital Rain。这里需要GIFEncoder class的支持。
你能看到Matrix代码雨效果Matrix Digital Rain中的那句中文么?
生成图片的效果:
生成代码:
<pre class="brush:php;"> include('GIFEncoder.class.php');
ob_start();
$rain_arr = array('惠', '邻', '网', '致', '力', '于', '高', '质',
'量', '程', '序', '代', '码', '的', '定', '制', '开', '发');
$str = '~!@#$%^&*()-_=+`[]{};:?/.,<>\\|"';
for($i = 0; $i < strlen($str); $i++) {
$rain_arr[] = $str[$i];
}
$length = count($rain_arr);
$max_x = 300; //图片大小
$max_y = 300;
$font_file = 'simsun.ttc'; //字体
$font_size = 10; //字体大小
$grid = 10;
$measuer = imagettfbbox(10,0,$font_file,'1');
$space = 5;
$char_height = $measuer[1] - $measuer[7] + $space;
$char_width = $measuer[2] - $measuer[0] + $space;
$i = 1;
$colorimage = imagecreatetruecolor(1,1);
$color[1] = imagecolorallocate($colorimage, 255, 255, 255);
while($i<$grid){
$color[] = imagecolorallocate($colorimage, 0, 255-20*$i, 0);
++$i;
}
imagedestroy($colorimage);
//ENOUGH TEXT TO FILL THE PAGE.
$width = ceil($max_x/$char_width);
$height = ceil($max_y/$char_height);
$x = 0;
while($x < $width){
$y = 0;
while($y < $height){
$text[$x][$y] = $msg[rand(0, $length)];// echo $text[$x][$y]; exit();
++$y;
}
$drop[$x] = rand(0, $width);
++$x;
}
$i = 0;
while($i<$height+$grid){
$image = imagecreatetruecolor($max_x, $max_y);
$x = 0;
while($x < $width){
$y = 0;
while($y < $drop[$x]){
if($drop[$x]-$y<=$grid){
imagettftext($image, $font_size, 0, $x*$char_width, ($y+1)*$char_height-$space, $color[$drop[$x]-$y], $font_file, $text[$x][$y]);
}
++$y;
}
if($drop[$x]<$height+$grid){
++$drop[$x];
}else{
$drop[$x] = 0;
}
++$x;
}
imagegif($image);
$imagedata[$i] = ob_get_contents();
imagedestroy($image);
ob_clean();
++$i;
}
$gif = new GIFEncoder(
$imagedata,
100,
0,
2,
0, 0, 0,
0,
"bin"
);
Header ( 'Content-type:image/gif' );
echo $gif->GetAnimation ();
```
Matrix Digital Rain php gif 生成 动态