| | 357 | |
|---|
| | 358 | // Retrieves what you really want to know about an image file, PDFs included, |
|---|
| | 359 | // before making calls such as the above based on good information. |
|---|
| | 360 | |
|---|
| | 361 | // Returns as follows: |
|---|
| | 362 | |
|---|
| | 363 | // array('format' => 'file extension: gif, jpg, png or pdf', 'width' => width in pixels, 'height' => height in pixels); |
|---|
| | 364 | |
|---|
| | 365 | // $format is the recommended file extension based on the actual file type, not the user's (possibly totally false or absent) |
|---|
| | 366 | // claimed file extension. |
|---|
| | 367 | |
|---|
| | 368 | // If the file does not have a valid header identifying it as one of these types, false is returned. |
|---|
| | 369 | |
|---|
| | 370 | static public function getInfo($file) |
|---|
| | 371 | { |
|---|
| | 372 | $result = array(); |
|---|
| | 373 | $in = fopen($file, "rb"); |
|---|
| | 374 | $data = fread($in, 4); |
|---|
| | 375 | fclose($in); |
|---|
| | 376 | if ($data === '%PDF') |
|---|
| | 377 | { |
|---|
| | 378 | $result['format'] = 'pdf'; |
|---|
| | 379 | $path = sfConfig::get("app_pkimageconverter_path", ""); |
|---|
| | 380 | if (strlen($path)) { |
|---|
| | 381 | if (!preg_match("/\/$/", $path)) { |
|---|
| | 382 | $path .= "/"; |
|---|
| | 383 | } |
|---|
| | 384 | } |
|---|
| | 385 | # Bounding box goes to stderr, not stdout! Charming |
|---|
| | 386 | $cmd = "(PATH=$path:\$PATH; export PATH; gs -sDEVICE=bbox -dNOPAUSE -dFirstPage=1 -dLastPage=1 -r100 -q " . escapeshellarg($file) . " -c quit) 2>&1"; |
|---|
| | 387 | $in = popen($cmd, "r"); |
|---|
| | 388 | $data = stream_get_contents($in); |
|---|
| | 389 | pclose($in); |
|---|
| | 390 | // Actual nonfatal errors in the bbox output mean it's not safe to just |
|---|
| | 391 | // read this naively with fscanf, look for the good part |
|---|
| | 392 | if (preg_match("/%%BoundingBox: \d+ \d+ (\d+) (\d+)/", $data, $matches)) |
|---|
| | 393 | { |
|---|
| | 394 | $result['width'] = $matches[1]; |
|---|
| | 395 | $result['height'] = $matches[2]; |
|---|
| | 396 | } |
|---|
| | 397 | else |
|---|
| | 398 | { |
|---|
| | 399 | // Bad PDF |
|---|
| | 400 | return false; |
|---|
| | 401 | } |
|---|
| | 402 | return $result; |
|---|
| | 403 | } |
|---|
| | 404 | else |
|---|
| | 405 | { |
|---|
| | 406 | $formats = array( |
|---|
| | 407 | IMAGETYPE_JPEG => "jpg", |
|---|
| | 408 | IMAGETYPE_PNG => "png", |
|---|
| | 409 | IMAGETYPE_GIF => "gif" |
|---|
| | 410 | ); |
|---|
| | 411 | $data = getimagesize($file); |
|---|
| | 412 | if (count($data) < 3) |
|---|
| | 413 | { |
|---|
| | 414 | return false; |
|---|
| | 415 | } |
|---|
| | 416 | if (!isset($formats[$data[2]])) |
|---|
| | 417 | { |
|---|
| | 418 | return false; |
|---|
| | 419 | } |
|---|
| | 420 | $format = $formats[$data[2]]; |
|---|
| | 421 | $result['width'] = $data[0]; |
|---|
| | 422 | $result['height'] = $data[1]; |
|---|
| | 423 | $result['format'] = $format; |
|---|
| | 424 | return $result; |
|---|
| | 425 | } |
|---|
| | 426 | } |
|---|