Also apply the __toString fix to UserInfo
[cacert-mgr.git] / external / ZendFramework-1.9.5 / externals / dojo / demos / faces / resources / imageMaker.php
1 <?php
2
3 // fake a delay until actual backend processing is going on
4 // sleep(3);
5 require_once "lib.php";
6
7 // PHP4 and low 5 compatibility?:
8 if(!function_exists('json_encode')){
9 require("../../dojo/tests/resources/JSON.php");
10 $jsonMaker = new Services_JSON;
11 function json_encode($obj){
12 return $jsonMaker->encode($obj);
13 }
14 function json_decode($str){
15 return $jsonMaker->decode($str);
16 }
17 }
18
19 // create $hair, $eyes, and $mouth variables
20 foreach(array("hair","eyes","mouth") as $part){
21 if(!empty($_POST[$part])){
22 // FIXME: add [a-zA-Z] matching here:
23 $$part = $_POST[$part];
24 }
25 }
26
27 if(isset($hair) && isset($eyes) && isset($mouth)){
28
29 $result = array();
30
31 $nick = createName($hair, $eyes, $mouth);
32 $outfile = "cache/" . $nick . ".jpg";
33 $thumbfile = "cache/." . $nick . "_thumb.jpg";
34
35 if(!file_exists("../" . $outfile)){
36 // TODO: create and cache this image in images/shame/
37
38 $width = 230; $height = 326;
39
40 $im = imageCreateTrueColor($width, $height);
41 $bgColor = imagecolorallocate($img, 255,255,255);
42 imagefill($im, 0, 0, $bgColor);
43
44 $parts = array(
45 "hair" => array(0, 0, 230, 120),
46 "eyes" => array(0, 120, 230, 94),
47 "mouth" => array(0, 214, 230, 113)
48 );
49
50 foreach($parts as $part => $dst){
51 // copy each slice into the new image
52 $tmpfile = "../images/" . $$part . ".jpg";
53 if(file_exists($tmpfile)){
54 $tmp = imageCreateFromJpeg($tmpfile);
55 imagecopy($im, $tmp, $dst[0], $dst[1], $dst[0], $dst[1], $dst[2], $dst[3]);
56 imagedestroy($tmp);
57 }
58 }
59
60 imagejpeg($im, "../" . $outfile);
61
62 if(!file_exists("../" . $thumbfile)){
63 $thumb = createThumb($im, 55);
64 imagejpeg($thumb, "../" . $thumbfile);
65 }
66
67 $result['addToList'] = true;
68 $result['thumb'] = $thumbfile;
69
70 }else{
71 // duplicate!
72 $result["duplicate"] = true;
73 }
74
75 $result["name"] = $nick;
76 $result["clan"] = getClanName($nick);
77 $result["file"] = $outfile;
78
79 // send the final data back as a success!
80 print json_encode($result);
81 }else{
82 print json_encode(array("error" => "error, someone didn't flip enough!"));
83 }
84
85 function createName($first, $middle, $last){
86
87 if($first == $middle && $middle == $last){
88 // edge case where longer names are more interesting, but
89 // we want to show the real name when all three pieces are
90 // one person:
91 $str = $first;
92 }else{
93 // break each name into thirds, then return the mashup name
94 // from the appropriate slice:
95 $first_len = ceil(strlen($first) / 3);
96 $mid_len = ceil(strlen($middle) / 3);
97 $last_len = ceil(strlen($last) / 3);
98
99 $first_seg = substr($first, 0, $first_len);
100 $mid_seg = substr($middle, $mid_len, ($mid_len * 2) - 1);
101 $last_seg = substr($last, $last_len * -1, $last_len);
102 $str = $first_seg . $mid_seg . $last_seg;
103 }
104 return $str;
105
106 }
107
108 function createThumb($src, $maxd){
109
110 $srcw = imagesx($src);
111 $srch = imagesy($src);
112 if($srcw<$srch){
113 $height = $maxd;
114 $width=floor($srcw * $height / $srch);
115 }else{
116 $width = $maxd;
117 $height = floor($srch * $width / $srcw);
118 }
119 if($width > $srcw && $height > $srch){
120 $width = $srcw;
121 $height=$srch;
122 } //if image is actually smaller than you want, leave small (remove this line to resize anyway)
123
124 $dest = imagecreatetruecolor($width, $height);
125 if($height < 100){
126 imagecopyresized($dest, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));
127 }else{
128 imagecopyresampled($dest, $src, 0, 0, 0, 0, $width, $height, imagesx($src), imagesy($src));
129 }
130
131 return $dest;
132
133 }
134
135 ?>