| | 463 | |
| | 464 | // Can this box handle pdf, png, jpeg (also acdepts jpg), gif, bmp, ico... |
| | 465 | |
| | 466 | // Mainly used to check for PDF support. |
| | 467 | |
| | 468 | // NOTE: this call is a performance hit, especially with netpbm and ghostscript available. |
| | 469 | // So we cache the result for 5 minutes. Keep that in mind if you make configuration changes, install |
| | 470 | // ghostscript, etc. and don't see an immediate difference. |
| | 471 | |
| | 472 | static public function supportsInput($extension) |
| | 473 | { |
| | 474 | $hint = aImageConverter::getHint("input:$extension"); |
| | 475 | if (!is_null($hint)) |
| | 476 | { |
| | 477 | return $hint; |
| | 478 | } |
| | 479 | |
| | 480 | $result = false; |
| | 481 | if (sfConfig::get('app_aimageconverter_netpbm', true)) |
| | 482 | { |
| | 483 | if (aImageConverter::supportsInputNetpbm($extension)) |
| | 484 | { |
| | 485 | $result = true; |
| | 486 | } |
| | 487 | } |
| | 488 | if (!$result) |
| | 489 | { |
| | 490 | $result = aImageConverter::supportsInputGd($extension); |
| | 491 | } |
| | 492 | aImageConverter::setHint("input:$extension", $result); |
| | 493 | return $result; |
| | 494 | } |
| | 495 | |
| | 496 | static public function supportsInputNetpbm($extension) |
| | 497 | { |
| | 498 | $types = array('gif' => 'gif', 'png' => 'png', 'jpg' => 'jpeg', 'jpeg' => 'jpeg', 'bmp' => 'bmp', 'ico' => 'ico'); |
| | 499 | $path = sfConfig::get("app_aimageconverter_path", ""); |
| | 500 | if (strlen($path)) { |
| | 501 | if (!preg_match("/\/$/", $path)) { |
| | 502 | $path .= "/"; |
| | 503 | } |
| | 504 | } |
| | 505 | if ($extension === 'pdf') |
| | 506 | { |
| | 507 | $cmd = 'gs'; |
| | 508 | } |
| | 509 | elseif (!isset($types[$extension])) |
| | 510 | { |
| | 511 | if (!preg_match('/^\w+$/', $extension)) |
| | 512 | { |
| | 513 | return false; |
| | 514 | } |
| | 515 | $cmd = $extension . 'topnm'; |
| | 516 | } |
| | 517 | else |
| | 518 | { |
| | 519 | $cmd = $types[$extension] . 'topnm'; |
| | 520 | } |
| | 521 | $in = popen("(PATH=$path:\$PATH; export PATH; which $cmd)", "r"); |
| | 522 | $result = stream_get_contents($in); |
| | 523 | pclose($in); |
| | 524 | if (strlen($result)) |
| | 525 | { |
| | 526 | return true; |
| | 527 | } |
| | 528 | return false; |
| | 529 | } |
| | 530 | |
| | 531 | static public function supportsInputGd($extension) |
| | 532 | { |
| | 533 | $types = array('gif' => 'gif', 'png' => 'png', 'jpg' => 'jpeg', 'jpeg' => 'jpeg', 'bmp' => 'bmp', 'ico' => 'ico'); |
| | 534 | if (!isset($types[$extension])) |
| | 535 | { |
| | 536 | return false; |
| | 537 | } |
| | 538 | $f = 'imagecreatefrom' . $types[$extension]; |
| | 539 | return is_callable($f); |
| | 540 | } |
| | 541 | |
| | 542 | static public function getHint($hint) |
| | 543 | { |
| | 544 | $cache = aImageConverter::getHintCache(); |
| | 545 | $key = 'apostrophe:imageconverter:' . $hint; |
| | 546 | return $cache->get($key, null); |
| | 547 | } |
| | 548 | |
| | 549 | static public function setHint($hint, $value) |
| | 550 | { |
| | 551 | $cache = aImageConverter::getHintCache(); |
| | 552 | // The lifetime should be short to avoid annoying developers who are |
| | 553 | // trying to fix their configuration and test with new possibilities |
| | 554 | $key = 'apostrophe:imageconverter:' . $hint; |
| | 555 | $cache->set($key, $value, 300); |
| | 556 | } |
| | 557 | static public function getHintCache() |
| | 558 | { |
| | 559 | $cacheClass = sfConfig::get('app_a_hint_cache_class', 'sfFileCache'); |
| | 560 | $cache = new $cacheClass(sfConfig::get('app_a_hint_cache_options', array('cache_dir' => aFiles::getWritableDataFolder(array('a_hint_cache'))))); |
| | 561 | return $cache; |
| | 562 | } |