Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
27404d5d
Commit
27404d5d
authored
Nov 04, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1138 from nukkumatti/master
Proposal for #827 Add ImageValidator
parents
3dee98d9
11c78cb4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
204 additions
and
3 deletions
+204
-3
FileValidator.php
framework/yii/validators/FileValidator.php
+5
-3
ImageValidator.php
framework/yii/validators/ImageValidator.php
+197
-0
Validator.php
framework/yii/validators/Validator.php
+2
-0
No files found.
framework/yii/validators/FileValidator.php
View file @
27404d5d
...
...
@@ -26,6 +26,7 @@ class FileValidator extends Validator
* separated by space or comma (e.g. "gif, jpg").
* Extension names are case-insensitive. Defaults to null, meaning all file name
* extensions are allowed.
* @see wrongType
*/
public
$types
;
/**
...
...
@@ -46,6 +47,7 @@ class FileValidator extends Validator
* @var integer the maximum file count the given attribute can hold.
* It defaults to 1, meaning single file upload. By defining a higher number,
* multiple uploads become possible.
* @see tooMany
*/
public
$maxFiles
=
1
;
/**
...
...
@@ -76,9 +78,10 @@ class FileValidator extends Validator
public
$tooSmall
;
/**
* @var string the error message used when the uploaded file has an extension name
* that is not listed in [[
extension
s]]. You may use the following tokens in the message:
* that is not listed in [[
type
s]]. You may use the following tokens in the message:
*
* - {attribute}: the attribute name
* - {file}: the uploaded file name
* - {extensions}: the list of the allowed extensions.
*/
public
$wrongType
;
...
...
@@ -87,7 +90,6 @@ class FileValidator extends Validator
* You may use the following tokens in the message:
*
* - {attribute}: the attribute name
* - {file}: the uploaded file name
* - {limit}: the value of [[maxFiles]]
*/
public
$tooMany
;
...
...
@@ -166,7 +168,7 @@ class FileValidator extends Validator
* @param string $attribute the attribute being validated
* @param UploadedFile $file uploaded file passed to check against a set of rules
*/
p
rotected
function
validateFile
(
$object
,
$attribute
,
$file
)
p
ublic
function
validateFile
(
$object
,
$attribute
,
$file
)
{
switch
(
$file
->
error
)
{
case
UPLOAD_ERR_OK
:
...
...
framework/yii/validators/ImageValidator.php
0 → 100644
View file @
27404d5d
<?php
/**
* Image validator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace
yii\validators
;
use
Yii
;
use
yii\web\UploadedFile
;
use
yii\helpers\FileHelper
;
/**
* ImageValidator verifies if an attribute is receiving a valid image.
*
* @author Taras Gudz <gudz.taras@gmail.com>
* @since 2.0
*/
class
ImageValidator
extends
FileValidator
{
/**
* @var string the error message used when the uploaded file is not an image.
* You may use the following tokens in the message:
*
* - {attribute}: the attribute name
* - {file}: the uploaded file name
*/
public
$notImage
;
/**
* @var integer the minimum width in pixels.
* Defaults to null, meaning no limit.
* @see underWidth
*/
public
$minWidth
;
/**
* @var integer the maximum width in pixels.
* Defaults to null, meaning no limit.
* @see overWidth
*/
public
$maxWidth
;
/**
* @var integer the minimum height in pixels.
* Defaults to null, meaning no limit.
* @see underHeight
*/
public
$minHeight
;
/**
* @var integer the maximum width in pixels.
* Defaults to null, meaning no limit.
* @see overWidth
*/
public
$maxHeight
;
/**
* @var array|string a list of file mime types that are allowed to be uploaded.
* This can be either an array or a string consisting of file mime types
* separated by space or comma (e.g. "image/jpeg, image/png").
* Mime type names are case-insensitive. Defaults to null, meaning all mime types
* are allowed.
* @see wrongMimeType
*/
public
$mimeTypes
;
/**
* @var string the error message used when the image is under [[minWidth]].
* You may use the following tokens in the message:
*
* - {attribute}: the attribute name
* - {file}: the uploaded file name
* - {limit}: the value of [[minWidth]]
*/
public
$underWidth
;
/**
* @var string the error message used when the image is over [[maxWidth]].
* You may use the following tokens in the message:
*
* - {attribute}: the attribute name
* - {file}: the uploaded file name
* - {limit}: the value of [[maxWidth]]
*/
public
$overWidth
;
/**
* @var string the error message used when the image is under [[minHeight]].
* You may use the following tokens in the message:
*
* - {attribute}: the attribute name
* - {file}: the uploaded file name
* - {limit}: the value of [[minHeight]]
*/
public
$underHeight
;
/**
* @var string the error message used when the image is over [[maxHeight]].
* You may use the following tokens in the message:
*
* - {attribute}: the attribute name
* - {file}: the uploaded file name
* - {limit}: the value of [[maxHeight]]
*/
public
$overHeight
;
/**
* @var string the error message used when the file has an mime type
* that is not listed in [[mimeTypes]].
* You may use the following tokens in the message:
*
* - {attribute}: the attribute name
* - {file}: the uploaded file name
* - {mimeTypes}: the value of [[mimeTypes]]
*/
public
$wrongMimeType
;
/**
* Initializes the validator.
*/
public
function
init
()
{
parent
::
init
();
if
(
$this
->
notImage
===
null
)
{
$this
->
notImage
=
Yii
::
t
(
'yii'
,
'The file "{file}" is not an image.'
);
}
if
(
$this
->
underWidth
===
null
)
{
$this
->
underWidth
=
Yii
::
t
(
'yii'
,
'The file "{file}" is too small. The width cannot be smaller than {limit} pixels.'
);
}
if
(
$this
->
underHeight
===
null
)
{
$this
->
underHeight
=
Yii
::
t
(
'yii'
,
'The file "{file}" is too small. The height cannot be smaller than {limit} pixels.'
);
}
if
(
$this
->
overWidth
===
null
)
{
$this
->
overWidth
=
Yii
::
t
(
'yii'
,
'The file "{file}" is too large. The width cannot be larger than {limit} pixels.'
);
}
if
(
$this
->
overHeight
===
null
)
{
$this
->
overHeight
=
Yii
::
t
(
'yii'
,
'The file "{file}" is too large. The height cannot be larger than {limit} pixels.'
);
}
if
(
$this
->
wrongMimeType
===
null
)
{
$this
->
wrongMimeType
=
Yii
::
t
(
'yii'
,
'Only files with these mimeTypes are allowed: {mimeTypes}.'
);
}
if
(
!
is_array
(
$this
->
mimeTypes
))
{
$this
->
mimeTypes
=
preg_split
(
'/[\s,]+/'
,
strtolower
(
$this
->
mimeTypes
),
-
1
,
PREG_SPLIT_NO_EMPTY
);
}
}
/**
* Internally validates a file object.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
* @param UploadedFile $file uploaded file passed to check against a set of rules
*/
public
function
validateFile
(
$object
,
$attribute
,
$file
)
{
parent
::
validateFile
(
$object
,
$attribute
,
$file
);
if
(
!
$object
->
hasErrors
(
$attribute
))
{
$this
->
validateImage
(
$object
,
$attribute
,
$file
);
}
}
/**
* Internally validates a file object.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
* @param UploadedFile $image uploaded file passed to check against a set of rules
*/
public
function
validateImage
(
$object
,
$attribute
,
$image
)
{
if
(
!
empty
(
$this
->
mimeTypes
)
&&
!
in_array
(
FileHelper
::
getMimeType
(
$image
->
tempName
),
$this
->
mimeTypes
,
true
))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
wrongMimeType
,
[
'file'
=>
$image
->
name
,
'mimeTypes'
=>
implode
(
', '
,
$this
->
mimeTypes
)]);
}
if
(
false
===
(
$imageInfo
=
getimagesize
(
$image
->
tempName
)))
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
notImage
,
[
'file'
=>
$image
->
name
]);
return
;
}
list
(
$width
,
$height
,
$type
)
=
$imageInfo
;
if
(
$width
==
0
||
$height
==
0
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
notImage
,
[
'file'
=>
$image
->
name
]);
return
;
}
if
(
$this
->
minWidth
!==
null
&&
$width
<
$this
->
minWidth
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
underWidth
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
minWidth
]);
}
if
(
$this
->
minHeight
!==
null
&&
$height
<
$this
->
minHeight
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
underHeight
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
minHeight
]);
}
if
(
$this
->
maxWidth
!==
null
&&
$width
>
$this
->
maxWidth
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
overWidth
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
maxWidth
]);
}
if
(
$this
->
maxHeight
!==
null
&&
$height
>
$this
->
maxHeight
)
{
$this
->
addError
(
$object
,
$attribute
,
$this
->
overHeight
,
[
'file'
=>
$image
->
name
,
'limit'
=>
$this
->
maxHeight
]);
}
}
}
framework/yii/validators/Validator.php
View file @
27404d5d
...
...
@@ -31,6 +31,7 @@ use yii\base\NotSupportedException;
* - `exist`: [[ExistValidator]]
* - `file`: [[FileValidator]]
* - `filter`: [[FilterValidator]]
* - `image`: [[ImageValidator]]
* - `in`: [[RangeValidator]]
* - `integer`: [[NumberValidator]]
* - `match`: [[RegularExpressionValidator]]
...
...
@@ -59,6 +60,7 @@ abstract class Validator extends Component
'exist'
=>
'yii\validators\ExistValidator'
,
'file'
=>
'yii\validators\FileValidator'
,
'filter'
=>
'yii\validators\FilterValidator'
,
'image'
=>
'yii\validators\ImageValidator'
,
'in'
=>
'yii\validators\RangeValidator'
,
'integer'
=>
[
'class'
=>
'yii\validators\NumberValidator'
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment