*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Validate / File / IsImage.php
blobacbe2f124af8ef844c39d0dc3393650090ce50bd
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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: IsImage.php 16971 2009-07-22 18:05:45Z mikaelkael $
22 /**
23 * @see Zend_Validate_File_MimeType
25 require_once 'Zend/Validate/File/MimeType.php';
27 /**
28 * Validator which checks if the file already exists in the directory
30 * @category Zend
31 * @package Zend_Validate
32 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
33 * @license http://framework.zend.com/license/new-bsd New BSD License
35 class Zend_Validate_File_IsImage extends Zend_Validate_File_MimeType
37 /**
38 * @const string Error constants
40 const FALSE_TYPE = 'fileIsImageFalseType';
41 const NOT_DETECTED = 'fileIsImageNotDetected';
42 const NOT_READABLE = 'fileIsImageNotReadable';
44 /**
45 * @var array Error message templates
47 protected $_messageTemplates = array(
48 self::FALSE_TYPE => "The file '%value%' is no image, '%type%' detected",
49 self::NOT_DETECTED => "The mimetype of file '%value%' has not been detected",
50 self::NOT_READABLE => "The file '%value%' can not be read"
53 /**
54 * Sets validator options
56 * @param string|array $mimetype
57 * @return void
59 public function __construct($mimetype = array())
61 if (empty($mimetype)) {
62 $mimetype = array(
63 'image/x-quicktime',
64 'image/jp2',
65 'image/x-xpmi',
66 'image/x-portable-bitmap',
67 'image/x-portable-greymap',
68 'image/x-portable-pixmap',
69 'image/x-niff',
70 'image/tiff',
71 'image/png',
72 'image/x-unknown',
73 'image/gif',
74 'image/x-ms-bmp',
75 'application/dicom',
76 'image/vnd.adobe.photoshop',
77 'image/vnd.djvu',
78 'image/x-cpi',
79 'image/jpeg',
80 'image/x-ico',
81 'image/x-coreldraw',
82 'image/svg+xml'
86 $this->setMimeType($mimetype);
89 /**
90 * Defined by Zend_Validate_Interface
92 * Returns true if and only if the file is compression with the set compression types
94 * @param string $value Real file to check for compression
95 * @param array $file File data from Zend_File_Transfer
96 * @return boolean
98 public function isValid($value, $file = null)
100 // Is file readable ?
101 require_once 'Zend/Loader.php';
102 if (!Zend_Loader::isReadable($value)) {
103 return $this->_throw($file, self::NOT_READABLE);
106 if ($file !== null) {
107 if (class_exists('finfo', false) && defined('MAGIC')) {
108 $mime = new finfo(FILEINFO_MIME);
109 $this->_type = $mime->file($value);
110 unset($mime);
111 } elseif (function_exists('mime_content_type') && ini_get('mime_magic.magicfile')) {
112 $this->_type = mime_content_type($value);
113 } else {
114 $this->_type = $file['type'];
118 if (empty($this->_type)) {
119 return $this->_throw($file, self::NOT_DETECTED);
122 $compressions = $this->getMimeType(true);
123 if (in_array($this->_type, $compressions)) {
124 return true;
127 $types = explode('/', $this->_type);
128 $types = array_merge($types, explode('-', $this->_type));
129 foreach($compressions as $mime) {
130 if (in_array($mime, $types)) {
131 return true;
135 return $this->_throw($file, self::FALSE_TYPE);