IsImage.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Validate
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Validate_File_MimeType
  23. */
  24. require_once 'Zend/Validate/File/MimeType.php';
  25. /**
  26. * Validator which checks if the file already exists in the directory
  27. *
  28. * @category Zend
  29. * @package Zend_Validate
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Validate_File_IsImage extends Zend_Validate_File_MimeType
  34. {
  35. /**
  36. * @const string Error constants
  37. */
  38. const FALSE_TYPE = 'fileIsImageFalseType';
  39. const NOT_DETECTED = 'fileIsImageNotDetected';
  40. const NOT_READABLE = 'fileIsImageNotReadable';
  41. /**
  42. * @var array Error message templates
  43. */
  44. protected $_messageTemplates = array(
  45. self::FALSE_TYPE => "The file '%value%' is no image, '%type%' detected",
  46. self::NOT_DETECTED => "The mimetype of file '%value%' has not been detected",
  47. self::NOT_READABLE => "The file '%value%' can not be read"
  48. );
  49. /**
  50. * Sets validator options
  51. *
  52. * @param string|array $mimetype
  53. * @return void
  54. */
  55. public function __construct($mimetype = array())
  56. {
  57. if (empty($mimetype)) {
  58. $mimetype = array(
  59. 'image/x-quicktime',
  60. 'image/jp2',
  61. 'image/x-xpmi',
  62. 'image/x-portable-bitmap',
  63. 'image/x-portable-greymap',
  64. 'image/x-portable-pixmap',
  65. 'image/x-niff',
  66. 'image/tiff',
  67. 'image/png',
  68. 'image/x-unknown',
  69. 'image/gif',
  70. 'image/x-ms-bmp',
  71. 'application/dicom',
  72. 'image/vnd.adobe.photoshop',
  73. 'image/vnd.djvu',
  74. 'image/x-cpi',
  75. 'image/jpeg',
  76. 'image/x-ico',
  77. 'image/x-coreldraw',
  78. 'image/svg+xml'
  79. );
  80. }
  81. $this->setMimeType($mimetype);
  82. }
  83. /**
  84. * Defined by Zend_Validate_Interface
  85. *
  86. * Returns true if and only if the file is compression with the set compression types
  87. *
  88. * @param string $value Real file to check for compression
  89. * @param array $file File data from Zend_File_Transfer
  90. * @return boolean
  91. */
  92. public function isValid($value, $file = null)
  93. {
  94. // Is file readable ?
  95. require_once 'Zend/Loader.php';
  96. if (!Zend_Loader::isReadable($value)) {
  97. return $this->_throw($file, self::NOT_READABLE);
  98. }
  99. if ($file !== null) {
  100. if (class_exists('finfo', false) && defined('MAGIC')) {
  101. $mime = new finfo(FILEINFO_MIME);
  102. $this->_type = $mime->file($value);
  103. unset($mime);
  104. } elseif (function_exists('mime_content_type') && ini_get('mime_magic.magicfile')) {
  105. $this->_type = mime_content_type($value);
  106. } elseif ($this->_headerCheck) {
  107. $this->_type = $file['type'];
  108. }
  109. }
  110. if (empty($this->_type)) {
  111. return $this->_throw($file, self::NOT_DETECTED);
  112. }
  113. $compressions = $this->getMimeType(true);
  114. if (in_array($this->_type, $compressions)) {
  115. return true;
  116. }
  117. $types = explode('/', $this->_type);
  118. $types = array_merge($types, explode('-', $this->_type));
  119. foreach($compressions as $mime) {
  120. if (in_array($mime, $types)) {
  121. return true;
  122. }
  123. }
  124. return $this->_throw($file, self::FALSE_TYPE);
  125. }
  126. }