<?php
/**
* 生成关键字墙图片
* 作者:勇者向前衝
* 日期:2012年8月2日
* 生成速度有点慢,需要10秒,和关键词数量关系不大。
* 仓促写着玩的,有需要的朋友就拿去用吧,如果有更好方法可以告诉我。
* 算法流程:
* 定义一些关键词;
* 计算每个关键词的字体;
* 生成一个原图1/9大小的矩阵,用于存储原图上是否画过的信息,这样搜索的时候能少搜一些;
* 初始化图片;
* 遍历关键词;
* 按找方螺旋顺序从中间搜索;
* 如果可以横放或者竖放,就把关键词放入图片,然后在矩阵上标注画过的部分;
* 最后把图片输出出来。
* 推荐网站:www.quqiaoqiao.com
*/
//测试数据,51个关键词
$textArray = array('勇者', '向前衝', '宅', '天使', 'PHP100', '博客', 'PHONE100', 'IT', '资讯', '哈哈', 'PHP', 'CSS', 'Javascript', '可用性', '设计', 'HTML', '产品设计', '交互设计', 'web', '交流会', '表单', 'css3', 'Firefox', '升级拖拉机', '用户体验', '用户研究', 'Canvas', '细节', '插件', '界面设计', 'ControlJS', '兼容', '社会化', 'js', 'iframe', '渲染引擎', '友情提示', '校验', '前端开发', 'Web标准化', 'chrome', 'iphone', '杂谈', 'iPad', 'jQuery', '小鸟微博', '交互', '团队生活', '浏览器', '游记', 'IE6');
// 生成每个关键词的字体大小,最大60,最小10,每一级数量之间相差1,根据关键词数量计算每级减少的大小。
$textWeightArray = array ();
$size = 60;
$count = 1;
$index = 0;
$step = ceil ( count ( $textArray ) / 10 );
$sizeStep = ($step < 5) ? 5 : $step;
$sizeStep = ($step > 10) ? 10 : $step;
foreach ( $textArray as $key => $v ) {
if ($size <= 10) {
$textWeightArray [$key] = 10;
} else {
$textWeightArray [$key] = $size;
$index ++;
if ($index >= $count) {
$size -= $sizeStep;
$index = 0;
$count ++;
}
}
}
// 生成原图的1/9的矩阵,用于记录已经画过的地方
$blockMatrix = array ();
// 创建一个213*213的方形,里面有个长106,高80的椭圆
for($y = 0; $y < 213; $y ++) {
for($x = 0; $x < 213; $x ++) {
$xx = ($x - 106) / 106;
$yy = ($y - 106) / 80;
if ($xx * $xx + $yy * $yy >= 1) {
$blockMatrix [$x] [$y] = 1;
} else {
$blockMatrix [$x] [$y] = 0;
}
}
}
// 生成图片,设置颜色和字体
header ( 'Content-Type: image/png' );
$im = imagecreatetruecolor ( 639, 480 );
$black = imagecolorallocate ( $im, 0, 0, 0 );
$white = imagecolorallocate ( $im, 255, 255, 255 );
$red = imagecolorallocate ( $im, 255, 0, 0 );
$green = imagecolorallocate ( $im, 0, 255, 0 );
$blue = imagecolorallocate ( $im, 0, 0, 255 );
$backGroundColor = imagecolorallocate ( $im, 250, 250, 250 );
imagefilledrectangle ( $im, 0, 0, 638, 479, $backGroundColor );
$font = 'MSYHBD.TTF';
// 循环输出文字
foreach ( $textArray as $key => $text ) {
$rect = imagettfbbox ( $textWeightArray [$key], 0, $font, $text ); //横着的
$rect2 = imagettfbbox ( $textWeightArray [$key], 90, $font, $text ); //竖着的
$rectSmall = $rect;
$rectSmall2 = $rect2;
for($i = 0; $i < 8; $i ++) {
$rectSmall[$i] /= 3;
$rectSmall2[$i] /= 3;
}
$rectSmall[0] = floor($rectSmall[0]);
$rectSmall[1] = ceil($rectSmall[1]);
$rectSmall[2] = ceil($rectSmall[2]);
$rectSmall[3] = $rectSmall[1];
$rectSmall[4] = $rectSmall[2];
$rectSmall[5] = floor($rectSmall[5]);
$rectSmall[6] = $rectSmall[0];
$rectSmall[7] = $rectSmall[5];
$rectSmall2[0] = ceil($rectSmall2[0]);
$rectSmall2[1] = ceil($rectSmall2[1]);
$rectSmall2[2] = $rectSmall2[0];
$rectSmall2[3] = floor($rectSmall2[3]);
$rectSmall2[4] = floor($rectSmall2[4]);
$rectSmall2[5] = $rectSmall2[3];
$rectSmall2[6] = $rectSmall2[4];
$rectSmall2[7] = $rectSmall2[1];
// 方螺旋状遍历,四种状态,0向右,1向上,2向左,3向下
$x = 107;
$y = 107;
$status = 0;
$length = 1;
$currentLength = 0;
while ( $x > 0 && $x < 213 && $y > 0 && $y < 213 ) {
$xx = $x * 3 - 1;
$yy = $y * 3 - 81;
$rs [0] = $rectSmall [0] + $x;
$rs [1] = $rectSmall [1] + $y;
$rs [2] = $rectSmall [2] + $x;
$rs [3] = $rectSmall [3] + $y;
$rs [4] = $rectSmall [4] + $x;
$rs [5] = $rectSmall [5] + $y;
$rs [6] = $rectSmall [6] + $x;
$rs [7] = $rectSmall [7] + $y;
$rs2 [0] = $rectSmall2 [0] + $x;
$rs2 [1] = $rectSmall2 [1] + $y;
$rs2 [2] = $rectSmall2 [2] + $x;
$rs2 [3] = $rectSmall2 [3] + $y;
$rs2 [4] = $rectSmall2 [4] + $x;
$rs2 [5] = $rectSmall2 [5] + $y;
$rs2 [6] = $rectSmall2 [6] + $x;
$rs2 [7] = $rectSmall2 [7] + $y;
// 试试横着放或者竖着放
$full = 0;
for($j = $rs [7]; $j <= $rs [3]; $j ++) {
for($i = $rs [6]; $i <= $rs [4]; $i ++) {
if ($i <= 0 || $i >= 213 || $j <= 0 || $j >= 213 || $blockMatrix [$i] [$j] == 1) {
$full = 1;
break 2;
}
}
}
if ($full == 0) {
for($j = $rs [7]; $j <= $rs [3]; $j ++) {
for($i = $rs [6]; $i <= $rs [4]; $i ++) {
$blockMatrix [$i] [$j] = 1;
}
}
$randomColor = imagecolorallocate ( $im, mt_rand ( 5, 250 ), mt_rand ( 5, 250 ), mt_rand ( 5, 250 ) );
imagettftext ( $im, $textWeightArray [$key], 0, $xx, $yy, $randomColor, $font, $text );
break;
} else {
$full = 0;
for($j = $rs2 [3]; $j <= $rs2 [1]; $j ++) {
for($i = $rs2 [4]; $i <= $rs2 [2]; $i ++) {
if ($i <= 0 || $i >= 213 || $j <= 0 || $j >= 213 || $blockMatrix [$i] [$j] == 1) {
$full = 1;
break 2;
}
}
}
if ($full == 0) {
for($j = $rs2 [3]; $j <= $rs2 [1]; $j ++) {
for($i = $rs2 [4]; $i <= $rs2 [2]; $i ++) {
$blockMatrix [$i] [$j] = 1;
}
}
$randomColor = imagecolorallocate ( $im, mt_rand ( 5, 250 ), mt_rand ( 5, 250 ), mt_rand ( 5, 250 ) );
imagettftext ( $im, $textWeightArray [$key], 90, $xx, $yy, $randomColor, $font, $text );
break;
}
}
if ($status == 0) {
$x ++;
$currentLength ++;
if ($currentLength == $length) {
$currentLength = 0;
$status = 1;
}
} else if ($status == 1) {
$y --;
$currentLength ++;
if ($currentLength == $length) {
$currentLength = 0;
$status = 2;
$length ++;
}
} else if ($status == 2) {
$x --;
$currentLength ++;
if ($currentLength == $length) {
$currentLength = 0;
$status = 3;
}
} else if ($status == 3) {
$y ++;
$currentLength ++;
if ($currentLength == $length) {
$currentLength = 0;
$status = 0;
$length ++;
}
}
}
}
imagepng ( $im );
imagedestroy ( $im );
?>
- 1
- 2
- 3
- 4
- 5
- 6
前往页