テクスチャを散りばめることができるクラス

 
を、作っています。
ニワンゴ宛に「ハート ★」と、添付画像を送ると、それで散りばめられた画像が出てくるようなものを作りたいですが、今日はここまでです。
 
ちなみに、くそみそジェネレーターとゆっくりジェネレーターは
フォント(ttfファイル)を作って、サーバに置いています。

<?php
/* TextureGenerator.class.php */
class TextureGenerator{
	private $beforeimg;
	private $afterimg;
	private $textures;
	private $palette;
	private $fontpath;
	private $width;
	private $height;
	private $max;
	private $min;
	private $loop;
	public function __construct(){
		$this->textures = array();
		$this->palette = imagecreatetruecolor(1,1);
	}
	public function setBeforeImg($imgsrc){
		$this->beforeimg = $imgsrc;
		$this->afterimg = $imgsrc;
	}
	public function addTexture($texture){
		$this->textures[] = $texture;
	}
	public function setFont($fontpath){
		$this->fontpath = $fontpath;
	}
	public function setSize($w,$h){
		$this->width = $w;
		$this->height = $h;
	}
	/** サイズを指定
	 * @param $max,$min:最大/最小テクスチャサイズ(画面短辺に対する%)
	 */
	public function setTextureSize($max_per,$min_per){
		if($this->width < $this->height){
			$small_side = $this->width;
		} else {
			$small_side = $this->height;			
		}
		$this->max = intval(($small_side * $max_per) / 100);
		$this->min = intval(($small_side * $min_per) / 100);
	}
	public function make(){
		$this->setLoopCnt(3);
		$size_array = $this->getSizeRandomArray($this->loop, $this->max, $this->min);
		$textures = $this->textures;
		$tex_cnt = 0;

		foreach($size_array as $size){
			$x = rand(0 , $this->width);
			$y = rand(0 , $this->height);
			$p = imagecolorat($this->beforeimg , $x , $y);
			$color = imagecolorallocate($this->palette , ($p>> 16) & 0xff , ($p>> 8) & 0xff , $p & 0xff);
			if($color){
				ImageTTFText($this->afterimg, $size."px", 0, $x, $y, $color, $this->fontpath,$textures[$tex_cnt]);
			}
			$tex_cnt++;
			if($tex_cnt > count($textures) - 1){
				$tex_cnt = 0;
			}
		}
	}
	public function outputpng($filepath = NULL){
		imagepng($this->afterimg,$filepath,0);

		imagedestroy($this->beforeimg);
		imagedestroy($this->afterimg);
		imagedestroy($this->palette);
	}
	private function setLoopCnt($loop){
		if($loop <= 0){
			$loop = 1;
		}
		$this->loop = $this->width * $this->height / $this->max / $loop;
	}
	private function getSizeRandomArray($loop,$max,$min){
		$ret = array();
		for($i=0; $i<$loop; $i++){
			$ret[] = rand($max,$min);
		}
		return $ret;
	}
}
?>
<?php
/* Texture.class.php */
class Texture{
	public function Convert($string){
		$array = self::getArray();
		if(isset($array[$string])){
			return $array[$string];
		} else {
			return $string;
		}
	}
	public function getArray(){
		return array(
			"太陽"=>"&#9728;",
			""=>"&#9728;",
			""=>"&#9728;",
			""=>"&#9729;",
			""=>"&#9729;",
			""=>"&#9730;",
			""=>"&#9730;",
			""=>"&#9731;",
			"電話"=>"&#9742;",
			"髑髏"=>"&#9760;",
			"骸骨"=>"&#9760;",
			""=>"&#9760;",
			"どくろ"=>"&#9760;",
			"ドクロ"=>"&#9760;",
			""=>"&#9770;",
			"陰陽"=>"&#9775;",
			"ニコ"=>"&#9787;",
			"SMILE"=>"&#9787;",
			"smile"=>"&#9787;",
			"ピース"=>"&#9996;",
			"チョキ"=>"&#9996;",
			"ハート"=>"&#9829;",
			"温泉"=>"&#9832;",
			""=>"&#9832;",
			"クローバー"=>"&#9827;",
			"若葉"=>"&#9827;",
			"わかば"=>"&#9827;",
			"ワカバ"=>"&#9827;",
			"クローバー"=>"&#9827;",
			"スペード"=>"&#9824;",
		);
	}
}

?>

こうやって使います。

<!--form.html-->
<html><body>
<form enctype="multipart/form-data" method="POST" action="genmain.php">
<input type="file" name="userfile" value="" class="fle">
<input type="text" name="tex[]">
<input type="text" name="tex[]">
<input type="text" name="tex[]">
<input type="text" name="tex[]">
<input type="text" name="tex[]">
<input type="text" name="tex[]">
<input type="text" name="tex[]">
<input type="text" name="tex[]">
<input type="submit">
</form>
</body></html>
<?php
/* genmain.php */
require_once("TextureGenerator.class.php");

list($w,$h) = getimagesize($_FILES['userfile']['tmp_name']);
if($w> 1280 || $h> 1024){
	die("画像が大きいです。画像を縮めてやり直してください。。");
}

if(isset($_FILES['userfile'])){
	if($_FILES['userfile']['type'] == "image/pjpeg" ||
	 $_FILES['userfile']['type'] == "image/jpeg"){
		$image = imagecreatefromjpeg($_FILES['userfile']['tmp_name']);
	}else if($_FILES['userfile']['type'] == "image/png" ||
	  $_FILES['userfile']['type'] == "image/x-png"){
		$image = imagecreatefrompng($_FILES['userfile']['tmp_name']);
	}else if($_FILES['userfile']['type'] == "image/gif"){
		$image = imagecreatefromgif($_FILES['userfile']['tmp_name']);
	}else{
		die("対応していないファイルです。");
	}
} else {
	die("画像がありません。");
}

$gen_class = new TextureGenerator();
$gen_class->setBeforeImg($image);
foreach($_POST["tex"] as $tex){
  if(strlen($tex) > 0){
    $gen_class->addTexture("$tex");
  }
}
$gen_class->setFont("msgothic.ttc");
$gen_class->setSize($w,$h);
$gen_class->setTextureSize(10,1);


$gen_class->make();
header("Content-type:image/png");
$gen_class->outputpng();
//$gen_class->outputpng("afterimg.png");

?>

こんな感じの画像ができます。ゆるふわ!!