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.
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 $
23 * @see Zend_Validate_File_MimeType
25 require_once 'Zend/Validate/File/MimeType.php';
28 * Validator which checks if the file already exists in the directory
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
38 * @const string Error constants
40 const FALSE_TYPE
= 'fileIsImageFalseType';
41 const NOT_DETECTED
= 'fileIsImageNotDetected';
42 const NOT_READABLE
= 'fileIsImageNotReadable';
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"
54 * Sets validator options
56 * @param string|array $mimetype
59 public function __construct($mimetype = array())
61 if (empty($mimetype)) {
66 'image/x-portable-bitmap',
67 'image/x-portable-greymap',
68 'image/x-portable-pixmap',
76 'image/vnd.adobe.photoshop',
86 $this->setMimeType($mimetype);
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
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);
111 } elseif (function_exists('mime_content_type') && ini_get('mime_magic.magicfile')) {
112 $this->_type
= mime_content_type($value);
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)) {
127 $types = explode('/', $this->_type
);
128 $types = array_merge($types, explode('-', $this->_type
));
129 foreach($compressions as $mime) {
130 if (in_array($mime, $types)) {
135 return $this->_throw($file, self
::FALSE_TYPE
);