IsImage.php 3.9 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-2010 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-2010 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 => "File '%value%' is no image, '%type%' detected",
  46. self::NOT_DETECTED => "The mimetype of file '%value%' could not been detected",
  47. self::NOT_READABLE => "File '%value%' can not be read",
  48. );
  49. /**
  50. * Sets validator options
  51. *
  52. * @param string|array|Zend_Config $mimetype
  53. * @return void
  54. */
  55. public function __construct($mimetype = array())
  56. {
  57. if ($mimetype instanceof Zend_Config) {
  58. $mimetype = $mimetype->toArray();
  59. }
  60. $temp = array();
  61. $default = array(
  62. 'image/x-quicktime',
  63. 'image/jp2',
  64. 'image/x-xpmi',
  65. 'image/x-portable-bitmap',
  66. 'image/x-portable-greymap',
  67. 'image/x-portable-pixmap',
  68. 'image/x-niff',
  69. 'image/tiff',
  70. 'image/png',
  71. 'image/x-unknown',
  72. 'image/gif',
  73. 'image/x-ms-bmp',
  74. 'application/dicom',
  75. 'image/vnd.adobe.photoshop',
  76. 'image/vnd.djvu',
  77. 'image/x-cpi',
  78. 'image/jpeg',
  79. 'image/x-ico',
  80. 'image/x-coreldraw',
  81. 'image/svg+xml'
  82. );
  83. if (is_array($mimetype)) {
  84. $temp = $mimetype;
  85. if (array_key_exists('magicfile', $temp)) {
  86. unset($temp['magicfile']);
  87. }
  88. if (array_key_exists('headerCheck', $temp)) {
  89. unset($temp['headerCheck']);
  90. }
  91. if (empty($temp)) {
  92. $mimetype += $default;
  93. }
  94. }
  95. if (empty($mimetype)) {
  96. $mimetype = $default;
  97. }
  98. parent::__construct($mimetype);
  99. }
  100. /**
  101. * Throws an error of the given type
  102. * Duplicates parent method due to OOP Problem with late static binding in PHP 5.2
  103. *
  104. * @param string $file
  105. * @param string $errorType
  106. * @return false
  107. */
  108. protected function _throw($file, $errorType)
  109. {
  110. $this->_value = $file['name'];
  111. switch($errorType) {
  112. case Zend_Validate_File_MimeType::FALSE_TYPE :
  113. $errorType = self::FALSE_TYPE;
  114. break;
  115. case Zend_Validate_File_MimeType::NOT_DETECTED :
  116. $errorType = self::NOT_DETECTED;
  117. break;
  118. case Zend_Validate_File_MimeType::NOT_READABLE :
  119. $errorType = self::NOT_READABLE;
  120. break;
  121. }
  122. $this->_error($errorType);
  123. return false;
  124. }
  125. }