Vous pouvez télécharger le fichier original ici : generate.php
« generate.php » colorisé
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 | <?php /* (c) Serge Emond, http://greyworld.net/ * * Simple test script for my patch to GD & PHP allowing * access to more of freetype2's properties * * see http://greyworld.net/software/php-gd-freetype2/ * * $Id: generate.php,v 1.3 2008/01/25 07:06:51 greyl Exp $ */ if (!extension_loaded('gd')) dl('gd.so'); /** Simple object to render text in an image using GD. */ class Generate { var $o = array( /** Output filename (always PNG) */ 'out' => './out.png', /** Text to render (UTF-8) */ 'text' => false, /** Font file */ 'font' => false, /** Color (R, G, B): int, 0-255 */ 'color' => array(0,0,0), /** Background color (R, G, B, Alpha) * R/G/B: int = 0-255 * Alpha: int = 0-127 (0: opaque, 127: transparent) */ 'bg' => array(255,255,255,127), /** Size in points */ 'size' => 10, /** Dots per inch (DPI) */ 'res' => 96, 'angle' => 0, /**< Angle in degrees */ 'slant' => 0.0, /**< Slant in degrees */ 'wm' => 1.0, /**< Width Multiplier */ 'hm' => 1.0, /**< Height Multiplier */ 'am' => 1.0, /**< Advance Multiplier */ 'uline' => 0, /**< Bool: show underline or not */ ); /** getopt() */ var $shortOpts = "t:f:s:r:o:x:SHBaImlAh"; /** extrainfo to getfttext */ var $ft_opts = array('hdpi' => 96, 'vdpi' => 96,); /** Bounding box width */ var $boxW = 0; /** Bounding box height */ var $boxH = 0; /** Text X pos */ var $txtX = 0; /** Text Y pos */ var $txtY = 0; /** Entry point */ function main() { // Parse options $this->getOpts(); // Evaluate image dimensions & calculate bounding box $this->evalBitmap(); // Render text and save to file $this->draw(); } // main() /** Show usage information */ function usage() { $a = $_SERVER['argv']; echo "{$a[0]} <options>\n". " Options (* means requires)\n". " -h This help\n". "\n". " -t \"txt\" *Text to generate\n". " -o name Out file (png, default: out.png)\n". " -f fnt.ttf *Font file\n". " -s size Font size (default = 10)\n". " -r dpi DPI to use\n". "\n". " -S load_no_scale\n". " -H load_no_hinting\n". " -B load_no_bitmap\n". " -a load_force_autohint\n". " -I load_load_ignore_transform\n". " -m load_monochrome\n". " -l load_linear_design\n". " -A load_no_autohint\n". " -x slant,wm,hm,am Various transformations:\n". " slant: Slant Angle, degrees (def: 0.0)\n". " wm: Width Multiplier (def: 1.0)\n". " hm: Height Multiplier (def: 1.0)\n". " am: Advance Multiplier (def: 1.0)\n". ""; } // usage() /** Parse options */ function getOpts() { $options = getopt($this->shortOpts); $missing = array('t' => 'Text to render required', 'f' => 'Font file required', ); $fto = array('S' => 'load_no_scale', 'H' => 'load_no_hinting', 'B' => 'load_no_bitmap', 'a' => 'load_force_autohint', 'I' => 'load_load_ignore_transform', 'm' => 'load_monochrome', 'l' => 'load_linear_design', 'A' => 'load_no_autohint', ); foreach ($options as $k => $v) { switch ($k) { case 'h': $this->usage(); exit(0); case 't': $this->o['text'] = $v; unset($missing['t']); break; case 'f': $this->o['font'] = $v; unset($missing['f']); break; case 'o': $this->o['out'] = $v; break; case 's': $this->o['size'] = (int)$v; break; case 'r': $this->o['res'] = (int)$v; $this->ft_opts['hdpi'] = (int)$v; $this->ft_opts['vdpi'] = (int)$v; break; case 'S': case 'H': case 'B': case 'a': case 'I': case 'm': case 'l': case 'A': $this->ft_opts[$fto[$k]] = 1; break; case 'x': $tmp = explode(',', $v); if (isset($tmp[0]) && $tmp[0] != '') { $this->o['slant'] = $tmp[0]; $this->ft_opts['slant_angle'] = $tmp[0] * M_PI / 180.0; } if (isset($tmp[1]) && $tmp[1] != '') { $this->o['wm'] = $tmp[1]; $this->ft_opts['width_mult'] = $tmp[1]; } if (isset($tmp[2]) && $tmp[2] != '') { $this->o['hm'] = $tmp[2]; $this->ft_opts['height_mult'] = $tmp[2]; } if (isset($tmp[3]) && $tmp[3] != '') { $this->o['am'] = $tmp[3]; $this->ft_opts['advance_mult'] = $tmp[3]; } break; } } if (count($missing) > 0) { foreach ($missing as $k => $v) { echo "Missing argument '$k': $v\n"; } $this->usage(); exit(-1); } } // getOpts() /** Calculate bounding boxes and stuff */ function evalBitmap() { /* Notes on bounding boxes * * TL TR * +----------------------------------------------------------+ * | | | * | | | * | | (0,0) | * | |/ | * |-----+----------------------------------------------------| * | | | * | | | * +----------------------------------------------------------+ * BL BR * * a=0 Descrition si a != 0 * 0: xMin cos(a) - yMin sin(a) xMin x du coin BL * 1: xMin sin(a) - yMin cos(a) -yMin y min (y de BL) * 2: xMax cos(a) - yMin sin(a) xMax x max (x de BR) * 3: xMax sin(a) - yMin cos(a) -yMin y de BR * 4: xMax cos(a) - yMax sin(a) xMax x de TR * 5: xMax sin(a) - yMax cos(a) -yMax y max (y de TR) * 6: xMin cos(a) - yMax sin(a) xMin x min (x de TL) * 7: xMin sin(a) - yMax cos(a) -yMax y de TL * * C'est tout fucké.. alors utilisons nos propres calculs. */ $s = Imageftbbox($this->o['size'], 0, $this->o['font'], $this->o['text'], $this->ft_opts); $width = abs($s[2] - $s[6]); $height = abs($s[1] - $s[5]); // Convert size of fonts from points to pixels $pixelSize = (float)($this->o['size'] * $this->ft_opts['vdpi']) / 72.0; // x: vers la droite // y: vers le bas // (0,0) = baseline, coin gauche $dims = array('tl' => array($s[0], $s[5]), 'tr' => array($s[2], $s[5]), 'br' => array($s[2], $s[1]), 'bl' => array($s[0], $s[1]) ); // Underline stuff.. #if ($this->o['uline']) { # $ulineThickness = (int)abs( $this->o['hm'] * $this->fnt['fntUlineThick'] * $pixelSize / (float)$this->fnt['fntEmSize']); # $ulinePosition = (int)(-1 * ( $this->o['hm'] * $this->fnt['fntUlinePos'] * $pixelSize / (float)$this->fnt['fntEmSize'] + $ulineThickness/2) + .5); # if ($ulineThickness < 1) $ulineThickness = 1; # $ulineLeft = $s[0] - 1.5*abs($ulineThickness); # $ulineRight = $s[2] + 1.5*abs($ulineThickness); # $dims['uposl'] = array($ulineLeft, $ulinePosition); # $dims['uposr'] = array($ulineRight, $ulinePosition); #} // On applique une rotation de "a" $a = $this->o['angle'] * M_PI / 180.0; $cosa = cos($a); $sina = sin($a); $dimsp = array(); $xMin = $xMax = $yMin = $yMax = false; $all_x = $all_y = array(); foreach ($dims as $tag => $c) { $dimsp[$tag] = array($c[1]*$sina + $this->o['am']*$this->o['wm']*$c[0]*$cosa, $c[1]*$cosa - $this->o['am']*$this->o['wm']*$c[0]*$sina); $all_x[] = $dimsp[$tag][0]; $all_y[] = $dimsp[$tag][1]; } $bbox = array(min($all_x), max($all_x), min($all_y), max($all_y)); #$bbox = array(min($s[0], $s[2], $s[4], $s[6]), # max($s[0], $s[2], $s[4], $s[6]), # min($s[1], $s[3], $s[5], $s[7]), # max($s[1], $s[3], $s[5], $s[7]) # ); $width = abs($bbox[1] - $bbox[0]); $height = abs($bbox[3] - $bbox[2]); $base = array(-$bbox[0], -$bbox[2]); // Padding #$width += 6; $height += 6; #$base[0] += 3; $base[1] += 3; #echo "bbox:<br><pre>";print_r($bbox);exit; #echo "Orig: <br><pre>"; print_r($dims) ; echo "</pre><br>Rot of $a rads:<br><pre>";print_r($dimsp);echo "</pre>";exit; // Padding: pour l'instant, on fixe à 3 pixels $this->boxW = 10 + $width; $this->boxH = 10 + $height; $this->txtX = 5 + $base[0]; $this->txtY = 5 + $base[1]; #if ($this->o['uline']) { # $this->ulinePos = array($dimsp['uposl'], $dimsp['uposr']); # $this->ulineThick = $ulineThickness; #} #$this->txtX = 3; #$this->txtY = 3 + $height; #echo "({$this->boxW} x {$this->boxH}) :: ({$this->txtX},{$this->txtY})<br>\n"; #exit; return true; } // evalBitmap() /** Render text and save file */ function draw() { $this->img = imagecreatetruecolor($this->boxW, $this->boxH); imagealphablending($this->img, false); $bgcolor = imagecolorallocatealpha($this->img, $this->o['bg'][0], $this->o['bg'][1], $this->o['bg'][2], $this->o['bg'][3]); $fgcolor = imagecolorallocatealpha($this->img, $this->o['color'][0], $this->o['color'][1], $this->o['color'][2], 0); // 100% opaque imagefilledrectangle($this->img, 0, 0, $this->boxW-1, $this->boxH-1, $bgcolor); imagealphablending($this->img, true); // Underline #if ($this->o['uline']) { # // Force 1 px de thickness :| # //imageantialias($this->img, !isset($this->ft_opts['monochrome'])); # imagesetthickness($this->img, $this->ulineThick); # imageline($this->img, # $this->ulinePos[0][0] + $this->txtX, $this->ulinePos[0][1] + $this->txtY, # $this->ulinePos[1][0] + $this->txtX, $this->ulinePos[1][1] + $this->txtY, # $fgcolor); #} #echo "<pre>";print_r($this->ulinePos);echo "\n\n</pre>thick: {$this->ulineThick}<br>\n";exit; ImageFtText($this->img, $this->o['size'], $this->o['angle'], $this->txtX, $this->txtY, $fgcolor, $this->o['font'], $this->o['text'], $this->ft_opts); #$xcolor = imagecolorallocate($this->img, 255, 0, 0); #imagesetpixel($this->img, $this->txtX, $this->txtY, $xcolor); imagesavealpha($this->img, true); imagepng($this->img, $this->o['out']); return true; } // draw() } // Generate $g = new Generate(); $g->main(); |