Changeset 3310 for libcaca/trunk/caca-php/examples/www
- Timestamp:
- Nov 7, 2008, 8:56:36 AM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/caca-php/examples/www/img2txt.php
- Property svn:executable deleted
r3306 r3310 1 #!/usr/bin/php52 1 <?php 3 2 /* 4 3 * img2txt image to text converter 4 * Copyright (c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com> 5 * 6 * This file is a Php port of "src/img2txt.c" 7 * which is: 5 8 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> 6 9 * 2007 Jean-Yves Lamoureux <jylam@lnxscene.org> … … 16 19 */ 17 20 18 if (php_sapi_name() != "cli") { 19 die("You have to run this program with php-cli!\n"); 20 } 21 $img2txt_php = isset($_SERVER['SCRIPT_NAME']) 22 ? 23 $_SERVER['SCRIPT_NAME'] 24 : 25 'img2txt.php'; 26 27 $argv = array(basename($img2txt_php)); 28 $args = ''; 29 if(isset($_REQUEST['args'])) 30 { 31 $args = $_REQUEST['args']; 32 } 33 $args=trim($args); 34 if(strlen($args)) 35 { 36 foreach(explode(' ', $args) as $arg) 37 { 38 $argv[] = $arg; 39 } 40 } 41 $argc = count($argv); 42 $stderr = ''; 43 $stdout = ''; 21 44 22 45 class MygetoptException extends Exception … … 198 221 function usage($argc, $argv) 199 222 { 200 fprintf(STDERR, "Usage: %s [OPTIONS]... <IMAGE>\n", $argv[0]); 201 fprintf(STDERR, "Convert IMAGE to any text based available format.\n"); 202 fprintf(STDERR, "Example : %s -w 80 -f ansi ./caca.png\n\n", $argv[0]); 203 fprintf(STDERR, "Options:\n"); 204 fprintf(STDERR, " -h, --help\t\t\tThis help\n"); 205 fprintf(STDERR, " -v, --version\t\t\tVersion of the program\n"); 206 fprintf(STDERR, " -W, --width=WIDTH\t\tWidth of resulting image\n"); 207 fprintf(STDERR, " -H, --height=HEIGHT\t\tHeight of resulting image\n"); 208 fprintf(STDERR, " -x, --font-width=WIDTH\t\tWidth of output font\n"); 209 fprintf(STDERR, " -y, --font-height=HEIGHT\t\tHeight of output font\n"); 210 fprintf(STDERR, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n"); 211 fprintf(STDERR, " -c, --contrast=CONTRAST\tContrast of resulting image\n"); 212 fprintf(STDERR, " -g, --gamma=GAMMA\t\tGamma of resulting image\n"); 213 fprintf(STDERR, " -d, --dither=DITHER\t\tDithering algorithm to use :\n"); 223 global $stderr; 224 $stderr .= sprintf("Usage: %s [OPTIONS]... <IMAGE>\n", $argv[0]); 225 $stderr .= sprintf("Convert IMAGE to any text based available format.\n"); 226 $stderr .= sprintf("Example : %s -w 80 -f ansi ./caca.png\n\n", $argv[0]); 227 $stderr .= sprintf("Options:\n"); 228 $stderr .= sprintf(" -h, --help\t\t\tThis help\n"); 229 $stderr .= sprintf(" -v, --version\t\t\tVersion of the program\n"); 230 $stderr .= sprintf(" -W, --width=WIDTH\t\tWidth of resulting image\n"); 231 $stderr .= sprintf(" -H, --height=HEIGHT\t\tHeight of resulting image\n"); 232 $stderr .= sprintf(" -x, --font-width=WIDTH\t\tWidth of output font\n"); 233 $stderr .= sprintf(" -y, --font-height=HEIGHT\t\tHeight of output font\n"); 234 $stderr .= sprintf(" -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n"); 235 $stderr .= sprintf(" -c, --contrast=CONTRAST\tContrast of resulting image\n"); 236 $stderr .= sprintf(" -g, --gamma=GAMMA\t\tGamma of resulting image\n"); 237 $stderr .= sprintf(" -d, --dither=DITHER\t\tDithering algorithm to use :\n"); 214 238 $list = caca_get_dither_algorithm_list(caca_create_dither(imagecreate(1, 1))); 215 239 foreach($list as $type => $name) 216 240 { 217 fprintf(STDERR,"\t\t\t%s: %s\n", $type, $name);218 } 219 220 fprintf(STDERR," -f, --format=FORMAT\t\tFormat of the resulting image :\n");241 $stderr .= sprintf("\t\t\t%s: %s\n", $type, $name); 242 } 243 244 $stderr .= sprintf(" -f, --format=FORMAT\t\tFormat of the resulting image :\n"); 221 245 $list = caca_get_export_list(); 222 246 foreach($list as $type => $name) 223 247 { 224 fprintf(STDERR,"\t\t\t%s: %s\n", $type, $name);248 $stderr .= sprintf("\t\t\t%s: %s\n", $type, $name); 225 249 } 226 250 } … … 228 252 function version() 229 253 { 230 printf( 254 global $stdout; 255 $stdout .= sprintf( 231 256 "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n" . 232 257 "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s\n" . … … 242 267 { 243 268 global $argc, $argv; 269 global $stderr; 244 270 $cols = 0; 245 271 $lines = 0; … … 264 290 ); 265 291 266 while($opt_and_arg = mygetopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options))) 267 { 268 $opt = $opt_and_arg[0]; 269 $arg = $opt_and_arg[1]; 270 if((substr($opt, 0, 2) == '--') 271 && 272 array_key_exists(substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : ''), $long_options)) 292 try { 293 while($opt_and_arg = mygetopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options))) 273 294 { 274 $opt = '-' . $long_options[substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : '')]; 295 $opt = $opt_and_arg[0]; 296 $arg = $opt_and_arg[1]; 297 if((substr($opt, 0, 2) == '--') 298 && 299 array_key_exists(substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : ''), $long_options)) 300 { 301 $opt = '-' . $long_options[substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : '')]; 302 } 303 switch($opt) 304 { 305 case '-W': /* --width */ 306 $cols = intval($arg); 307 break; 308 case '-H': /* --height */ 309 $lines = intval($arg); 310 break; 311 case '-x': /* --width */ 312 $font_width = intval($arg); 313 break; 314 case '-y': /* --height */ 315 $font_height = intval($arg); 316 break; 317 case '-f': /* --format */ 318 $format = $arg; 319 break; 320 case '-d': /* --dither */ 321 $dither = $arg; 322 break; 323 case '-g': /* --gamma */ 324 $gamma = floatval($arg); 325 break; 326 case '-b': /* --brightness */ 327 $brightness = floatval($arg); 328 break; 329 case '-c': /* --contrast */ 330 $contrast = floatval($arg); 331 break; 332 case '-h': /* --help */ 333 usage($argc, $argv); 334 return 0; 335 case '-v': /* --version */ 336 version(); 337 return 0; 338 default: 339 return 1; 340 } 275 341 } 276 switch($opt) 277 { 278 case '-W': /* --width */ 279 $cols = intval($arg); 280 break; 281 case '-H': /* --height */ 282 $lines = intval($arg); 283 break; 284 case '-x': /* --width */ 285 $font_width = intval($arg); 286 break; 287 case '-y': /* --height */ 288 $font_height = intval($arg); 289 break; 290 case '-f': /* --format */ 291 $format = $arg; 292 break; 293 case '-d': /* --dither */ 294 $dither = $arg; 295 break; 296 case '-g': /* --gamma */ 297 $gamma = floatval($arg); 298 break; 299 case '-b': /* --brightness */ 300 $brightness = floatval($arg); 301 break; 302 case '-c': /* --contrast */ 303 $contrast = floatval($arg); 304 break; 305 case '-h': /* --help */ 306 usage($argc, $argv); 307 return 0; 308 case '-v': /* --version */ 309 version(); 310 return 0; 311 default: 312 return 1; 313 } 314 } 315 316 if($argc != 2) 317 { 318 fprintf(STDERR, "%s: wrong argument count\n", $argv[0]); 342 } 343 catch (MygetoptException $e) 344 { 345 $stderr .= $argv[0] . ": " . $e->getMessage() . "\n"; 346 usage($argc, $argv); 347 return 2; 348 } 349 350 if($argc > 1) 351 { 352 $stderr .= sprintf("%s: too many arguments\n", $argv[0]); 319 353 usage($argc, $argv); 320 354 return 1; 321 355 } 322 356 357 $file = isset($_FILES['file']) ? $_FILES['file']['tmp_name'] : NULL; 358 $filename = isset($_FILES['file']) ? $_FILES['file']['name'] : NULL; 359 360 if(! $file) 361 { 362 $stderr .= sprintf("%s: no image was provided\n", $argv[0]); 363 usage($argc, $argv); 364 return 1; 365 } 366 323 367 $cv = caca_create_canvas(0, 0); 324 368 if(!$cv) 325 369 { 326 fprintf(STDERR,"%s: unable to initialise libcaca\n", $argv[0]);370 $stderr .= sprintf("%s: unable to initialise libcaca\n", $argv[0]); 327 371 return 1; 328 372 } 329 373 330 $i_str = file_get_contents($argv[$argc-1]);374 $i_str = $file ? file_get_contents($file) : NULL; 331 375 $i = $i_str ? imagecreatefromstring($i_str) : NULL; 332 376 if(!$i) 333 377 { 334 fprintf(STDERR, "%s: unable to load %s\n", $argv[0], $argv[$argc-1]);378 $stderr .= sprintf("%s: unable to load %s\n", $argv[0], $filename); 335 379 return 1; 336 380 } … … 358 402 if(! caca_set_dither_algorithm($i_dither, $dither?$dither:"fstein")) 359 403 { 360 fprintf(STDERR,"%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither?$dither:"fstein");404 $stderr .= sprintf("%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither?$dither:"fstein"); 361 405 return -1; 362 406 } … … 368 412 caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i); 369 413 370 $export = caca_export_string($cv, $format?$format:"ansi"); 414 $format = $format ? $format : 'html'; 415 416 $export = caca_export_string($cv, $format); 371 417 if(!$export) 372 418 { 373 fprintf(STDERR, "%s: Can't export to format '%s'\n", $argv[0], $format); 419 $stderr .= sprintf("%s: Can't export to format '%s'\n", $argv[0], $format); 420 return -1; 374 421 } 375 422 else 376 423 { 424 $content_type_map = array( 425 'ansi' => 'text/plain; charset=CP437', 426 'utf8' => 'text/plain; charset=UTF-8', 427 'utf8cr' => 'text/plain; charset=UTF-8', 428 'html' => 'text/html; charset=UTF-8', 429 'html3' => 'text/html; charset=UTF-8', 430 'bbfr' => 'text/plain; charset=UTF-8', 431 'irc' => 'text/plain; charset=UTF-8', 432 'ps' => 'application/postscript', 433 'svg' => 'image/svg+xml', 434 'tga' => 'image/x-targa' 435 ); 436 437 $download_extension_map = array( 438 'caca' => 'caca', 439 'ansi' => 'txt', 440 'utf8' => 'txt', 441 'utf8cr' => 'txt', 442 'irc' => 'txt', 443 'tga' => 'tga' 444 ); 445 446 $inline_extension_map = array( 447 'bbfr' => 'txt', 448 'ps' => 'ps', 449 'svg' => 'svg' 450 ); 451 452 if (! array_key_exists($format, $content_type_map)) 453 $content_type = 'application/octet-stream'; 454 else 455 $content_type = $content_type_map[$format]; 456 457 header('Content-Type: ' . $content_type); 458 if (array_key_exists($format, $download_extension_map)) 459 header('Content-Disposition: attachment; filename=export.' . $download_extension_map[$format]); 460 else if (array_key_exists($format, $inline_extension_map)) 461 header('Content-Disposition: inline; filename=export.' . $inline_extension_map[$format]); 462 377 463 echo $export; 378 464 } … … 380 466 return 0; 381 467 } 382 exit(main()); 468 if(main() || strlen($stdout) || strlen($stderr)) 469 { 470 header('Content-Type: text/html; charset=UTF-8'); 471 ?> 472 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 473 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 474 475 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 476 <head> 477 <title>image to text converter</title> 478 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 479 </head> 480 <body> 481 <form id="img2txtform" name="img2txtform" action="#" enctype="multipart/form-data" method="post"> 482 <label for="file">Image:</label> 483 <input id="file" name="file" type="file" /> 484 <br /> 485 <label for="args">Options:</label> 486 <input id="args" name="args" type="text" value="<?= htmlspecialchars($args) ?>" size="80" /> 487 <br /> 488 <input type="submit" /> 489 </form> 490 <?php 491 ; 492 if(strlen($stderr)) 493 { 494 ?><pre xml:space="preserve"><em><?= htmlspecialchars($stderr) ?></em></pre><?php 495 ; 496 } 497 if(strlen($stdout)) 498 { 499 ?><pre xml:space="preserve"><?= htmlspecialchars($stdout) ?></pre><?php 500 ; 501 } 502 ?> 503 </body> 504 </html> 505 <?php 506 ; 507 } 383 508 ?>
Note: See TracChangeset
for help on using the changeset viewer.